您的位置:首页 > 编程语言 > Java开发

【Spring】学习笔记_体系结构与基本思想

2019-07-30 22:37 120 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/huahuohui/article/details/97823643

1.Spring的体系结构:

    1-1.核心容器:

        Spring-core模块:控制反转与依赖控制功能

        Spring-beans模块:提供了BeanFactory

        Spring-context模块:提供了ApplicationContext接口的访问方式

        Spring-context-support模块:第三方库的支持(高速缓存与任务调度)

        Spring-expresion模块

 

2.Spring IoC(Inversion of Control控制反转)容器:

    2-1.BeanFactory(不常用):

        由org.springframework.beans.factory.BeanFactory接口定义,是一个管理Bean的工厂

    2-2.ApplicationContex(应用上下文):

        由org.springframework.xontext.ApplicationContext接口定义

        实现方式:

        2-2-1.通过ClassPathXmlApplicationContext创建:

[code]public static void main(String[] args){
ApplicationContext appcont =
new ClassPathXmlApplicationContext("applicationContext.xml");
TestDao tt = (TestDao)appcont.getBean("test");
tt.dosomething();
}

        2-2-2.通过Web服务器实例化ApplicationContext容器:

            基于org.springframework.web.context.ContextLoaderListener实现:

[code]<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

    2-3.依赖注入(IoC)的类型:

        2-3-1:通过构造方法注入(不常用):

            Dao + DaoImpl层

            Service + ServiceImpl层(构造方法):

[code]public class MyServiceImpl implements MyService{
private MyDao myDao;
//构造方法
public MyServiceImpl(MyDao myDao){
super();
this.myDao = myDao
}
}

            applicationContext.xml的配置:

[code]<bean id = "myDao" class = "dao.MyDaoImpl"/>
<bean id = "myService" class = "service.MyServiceImpl">
<constructor-arg index = "0" ref = "myDao"/>
</bean>

            controller层

        2-3-2:通过属性setter方法注入:

            Dao + DaoImpl层

            Service + ServiceImpl层(setter方法):

[code]public class MyServiceImpl implements MyService{
private MyDao myDao;
//setter方法
public void setMyDao(MyDao myDao){
this.myDao = myDao;
}
}

            applicationContext.xml配置:

[code]<bean id = "myDao" class = "dao.MyDaoImpl"/>
<bean id = "myService" class = "service.MyServiceImpl">
<property name = "myDao" ref = "myDao"/>
</bean>

            controller层

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: