Spring中的ioc
来源:互联网 发布:照片批量修改大小软件 编辑:程序博客网 时间:2024/06/06 01:33
IOC:Inversion of Control 控制反转
Spring的核心容器也可以成为ioc容器,它主要负责各个对象的创建,初始化,销毁等(即对象的生命周期)
目的:削减程序的耦合性
分类:分为依赖注入(DI)和依赖查找(DL),其中依赖注入应用比较广泛
DI:表示让调用类对某一接口实现类的依赖关系由容器注入,以消除调用类对某一接口实现的依赖关系。
DL:容器创建对象并提供 回调接口和 上下文环境,需要时通过接口从容器中查找其他对象。
一、Spring ioc 的API
1.BeanFactory接口:
主要用来处理Bean类的初始化和重置,建立对象之间的依赖关系。
接口中的方法:
//根据指定名称返回一个Bean实例
Object getBean(String name);
//判断名称为name的Bean是否是原型,即是否总是返回一个新实例
boolean isPrototype(String name);
//判断名称为name的Bean是否是单例
boolean isSingleton(String name);
//判断容器中是否包含给定名称的Bean实例
boolean containsBean(String name);
//如果名称为name的Bean有别名则返回所有别名
String[] getAliases(String name);
2.ApplicationContext接口:
该接口继承于BeanFactory,增强了BeanFactory,增加了事务处理AOP,国际化,事件传递等功能
所以在代码中我们一般会使用ApplicationContext接口,以及这个接口相应的实现类来创建spring的容器对象
例如:
String path = "com/briup/ioc/set/set.xml";
ApplicationContext container = new ClassPathXmlApplicationContext(path);
二、配置文件:
Spring通过读取配置文件中的数据来对应用中各个对象进行实例化
格式:xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
</beans>
注意事项:
1.这个文件的头部声明可以在下载的spring文档中的示例找到
2.这里面用到了俩个schema文件(.xsd文件),分别是spring-beans-3.2.xsd和spring-context-3.2.xsd,它们也可以在下载的spring文档中找到
3.在Eclipse中把xml文件和schema文件关联后,xml中就可以有标签代码的提示了(注意关联时别配置错了)
4.spring框架是模块化的,之后使用其他模块的时候,还可以在该xml的根元素中继续引用其他模块中相应的schema文件,然后就可以使用引入新模块中的标签代码了
三、ioc的相关功能:
以下数据的注入都需要通过xml文件来配置,但是通过ApplicationContext的对象的getBean("id|name")方法就可以得到注入好数据的具体对象
1.set注入(必须依靠set方法,即bean类中必须有setXxx()方法才可以使用)
可以注入的内容:
1).基本数据类型
2).引用数据类型
3).集合
a)基本数据类型的xml注入:set.xml
<bean id="helloBean" class="ioc.HelloBean">
<property name="name">
<value>tom</value>
</property>
<property name="age" value="20">
</property>
</bean>
b)引用数据类型的注入:
对象类型的装配:
1.<ref local=" "/> 用于涉及的对象的id在本配置文件中
2.<ref bean=" "/> 用于涉及的对象的id不在本配置文件中
3.使用property的ref属性引用
例如:
配置applicationContext.xml
<bean id="someBean" class="ioc.SomeBean">
<property name="ob">
<ref bean="otherBean" />
</property>
</bean>
配置other.xml文件
<bean id="otherBean" class="ioc.OtherBean">
<property name="str1">
<value>hello</value>
</property>
</bean>
c)集合类型的注入:
集合的装配
方式:配置元素<list> <set> <map> <props>
<bean id="someBean" class="ioc.SomeBean">
<property name="listProperty">
<list>
<value>list1</value>
<value>list1</value>
<value>list3</value>
</list>
</property>
<property name="setProperty">
<set>
<value>set1</value>
<value>set1</value>
<value>set3</value>
</set>
</property>
<property name="mapProperty">
<map>
<entry key="key1">
<value>value1</value>
</entry>
<entry key="key2">
<value>value2</value>
</entry>
</map>
</property>
<property name="property">
<props>
<prop key="key1">prop1</prop>
<prop key="key2">prop2</prop>
<prop key="key3">prop3</prop>
</props>
</property>
</bean>
2.基于构造器注入:
构造器注入有俩种形式 一个是根据参数类型 一个是根据参数位置的下标
<constructor-arg type="int" value="">
<constructor-arg index="0" value="">
例如:
<bean name="student" class="com.briup.bean.Student">
<constructor-arg type="int" value="25">
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>tom</value>
</constructor-arg>
<constructor-arg type="long" value="100">
</constructor-arg>
</bean>
3.自动注入:
容器按照一定的规则去自动装配bean中的属性
注:自动注入只对引用类型数据起作用,对基本数据类型不起作用
a)byName方式
例如:
4.继承:
xml文件中,一个bean的配置可以继承另一个bean的配置
bean标签中的俩个属性:
abstract属性
<bean name=".." class=".." abstract="true">
注:bean中一旦定义为了abstract="true",则说明该bean是用来被继承的,不能通过该bean获得对象了
parent属性,指定继承哪个bean的配置
<bean name=".." parent="另一个bean的名字">
例如:
Spring的核心容器也可以成为ioc容器,它主要负责各个对象的创建,初始化,销毁等(即对象的生命周期)
目的:削减程序的耦合性
分类:分为依赖注入(DI)和依赖查找(DL),其中依赖注入应用比较广泛
DI:表示让调用类对某一接口实现类的依赖关系由容器注入,以消除调用类对某一接口实现的依赖关系。
DL:容器创建对象并提供 回调接口和 上下文环境,需要时通过接口从容器中查找其他对象。
一、Spring ioc 的API
1.BeanFactory接口:
主要用来处理Bean类的初始化和重置,建立对象之间的依赖关系。
接口中的方法:
//根据指定名称返回一个Bean实例
Object getBean(String name);
//判断名称为name的Bean是否是原型,即是否总是返回一个新实例
boolean isPrototype(String name);
//判断名称为name的Bean是否是单例
boolean isSingleton(String name);
//判断容器中是否包含给定名称的Bean实例
boolean containsBean(String name);
//如果名称为name的Bean有别名则返回所有别名
String[] getAliases(String name);
2.ApplicationContext接口:
该接口继承于BeanFactory,增强了BeanFactory,增加了事务处理AOP,国际化,事件传递等功能
所以在代码中我们一般会使用ApplicationContext接口,以及这个接口相应的实现类来创建spring的容器对象
例如:
String path = "com/briup/ioc/set/set.xml";
ApplicationContext container = new ClassPathXmlApplicationContext(path);
二、配置文件:
Spring通过读取配置文件中的数据来对应用中各个对象进行实例化
格式:xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
</beans>
注意事项:
1.这个文件的头部声明可以在下载的spring文档中的示例找到
2.这里面用到了俩个schema文件(.xsd文件),分别是spring-beans-3.2.xsd和spring-context-3.2.xsd,它们也可以在下载的spring文档中找到
3.在Eclipse中把xml文件和schema文件关联后,xml中就可以有标签代码的提示了(注意关联时别配置错了)
4.spring框架是模块化的,之后使用其他模块的时候,还可以在该xml的根元素中继续引用其他模块中相应的schema文件,然后就可以使用引入新模块中的标签代码了
三、ioc的相关功能:
以下数据的注入都需要通过xml文件来配置,但是通过ApplicationContext的对象的getBean("id|name")方法就可以得到注入好数据的具体对象
1.set注入(必须依靠set方法,即bean类中必须有setXxx()方法才可以使用)
可以注入的内容:
1).基本数据类型
2).引用数据类型
3).集合
a)基本数据类型的xml注入:set.xml
<bean id="helloBean" class="ioc.HelloBean">
<property name="name">
<value>tom</value>
</property>
<property name="age" value="20">
</property>
</bean>
b)引用数据类型的注入:
对象类型的装配:
1.<ref local=" "/> 用于涉及的对象的id在本配置文件中
2.<ref bean=" "/> 用于涉及的对象的id不在本配置文件中
3.使用property的ref属性引用
例如:
配置applicationContext.xml
<bean id="someBean" class="ioc.SomeBean">
<property name="ob">
<ref bean="otherBean" />
</property>
</bean>
配置other.xml文件
<bean id="otherBean" class="ioc.OtherBean">
<property name="str1">
<value>hello</value>
</property>
</bean>
c)集合类型的注入:
集合的装配
方式:配置元素<list> <set> <map> <props>
<bean id="someBean" class="ioc.SomeBean">
<property name="listProperty">
<list>
<value>list1</value>
<value>list1</value>
<value>list3</value>
</list>
</property>
<property name="setProperty">
<set>
<value>set1</value>
<value>set1</value>
<value>set3</value>
</set>
</property>
<property name="mapProperty">
<map>
<entry key="key1">
<value>value1</value>
</entry>
<entry key="key2">
<value>value2</value>
</entry>
</map>
</property>
<property name="property">
<props>
<prop key="key1">prop1</prop>
<prop key="key2">prop2</prop>
<prop key="key3">prop3</prop>
</props>
</property>
</bean>
2.基于构造器注入:
构造器注入有俩种形式 一个是根据参数类型 一个是根据参数位置的下标
<constructor-arg type="int" value="">
<constructor-arg index="0" value="">
例如:
<bean name="student" class="com.briup.bean.Student">
<constructor-arg type="int" value="25">
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>tom</value>
</constructor-arg>
<constructor-arg type="long" value="100">
</constructor-arg>
</bean>
3.自动注入:
容器按照一定的规则去自动装配bean中的属性
注:自动注入只对引用类型数据起作用,对基本数据类型不起作用
a)byName方式
例如:
例如
例如:
4.继承:
xml文件中,一个bean的配置可以继承另一个bean的配置
bean标签中的俩个属性:
abstract属性
<bean name=".." class=".." abstract="true">
注:bean中一旦定义为了abstract="true",则说明该bean是用来被继承的,不能通过该bean获得对象了
parent属性,指定继承哪个bean的配置
<bean name=".." parent="另一个bean的名字">
例如:
阅读全文
0 0
- Spring中的IOC原理
- Spring中的IOC
- spring中的ioc
- Spring中的IOC原理
- Spring中的IOC
- Spring中的IOC实现
- Spring中的IOC
- Spring中的Ioc
- spring中的ioc
- Spring中的IoC
- Spring中的IOC
- Spring中的IOC原理
- spring中的IOC
- Spring中的ioc
- Spring中的IOC
- spring中的ioc
- Spring框架中的Ioc
- spring中的AOP与IOC
- JavaScript作用域、上下文、执行期上下文、作用域链、闭包
- 如何设置IntelliJ IDEA无法智能感知支持Jsp内置对象
- 编译原理与编译构造 二义文法的处理、语义
- Javascript基础:typeof返回的数据类型
- solr相关
- Spring中的ioc
- pyspark aggregate函数使用问题(aggregate重写,aggregate中的函数参数限制)
- Spring中关于xml导入其他xml问题
- spring容器创建bean对象的方式
- MySQL索引背后的数据结构及算法原理
- PAT (Basic Level) Practise (中文)1028. 人口普查(20)
- shiro的quick-start
- Material Design控件之TabLayout
- 指针初学,简单应用