这篇文章讲述的是Spring框架中的IOC容器及bean管理,如有错误或者不当之处,还望各位大神批评指正。
什么是IOC容器?
- IOC即反转控制,创建对象的权利交给容器来完成,而程序要做的仅仅是使用对象。
- Spring 容器是 Spring 框架的核心。容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁。Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件。这些对象被称为 Spring Beans
Spring中的IOC容器的使用
如何在IOC容器中配置bean?
<bean id="student" class="com.cn.cmc.beans.Student" >
<property name="name" value="叶清逸"></property>
</bean>
如何获取IOC容器?
- Spring提供两种IOC容器:
- ApplicationContext容器
- BeanFactory容器(已废弃)
获取ApplicationContext容器
/**
* ApplicationContext代表IOC容器
* ClassPathXmlApplicationContext从类路径下读取配置文件
* FileSystemXmlApplicationContext从文件系统路径下读取配置文件
* WebXmlApplicationContext:在web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
*/
ApplicationContext context = new FileSystemXmlApplicationContext("config/beans.xml") ;
获取BeanFactory容器(已废弃)
/**
* XmlBeanFactory生成工厂bean
* ClassPathResource加载配置文件
*/
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config/beans.xml")) ;
bean管理
从IOC容器中获取bean
方式一:通过id获取bean
Student student = context.getBean(Student.class);
System.out.println(student.getName());
方式二:通过.class获取bean
Student s = (Student)context.getBean("student") ;
System.out.println(s.getName());
方式三:通过工厂方法来获取bean
方法类
package com.cn.cmc.beans;
import java.util.HashMap;
import java.util.Map;
public class StaticStudentFactory {
private static Map<String,Student> map ;
static {
map = new HashMap<String, Student>();
map.put("叶清逸", new Student(100001,"叶清逸",'M',25)) ;
map.put("张三", new Student(100002,"张三",'F',22)) ;
}
static Student getBean(String name){
return map.get(name) ;
}
}
xml中的配置:
<bean id="student1" class="com.cn.cmc.beans.StaticStudentFactory" factory-method="getBean">
<constructor-arg value="叶清逸"></constructor-arg>
</bean>
bean的属性注入
方式一:属性注入(通过setter方法)
<property name="name" value="叶清逸"></property>
方式二:构造方法注入
<bean id="student" class="com.cn.cmc.beans.Student" >
<constructor-arg name="id" value="100001"></constructor-arg>
<constructor-arg name="name">
<value>叶清逸</value>
</constructor-arg>
<constructor-arg name="sex" value="M" index="2" type="char"></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
</bean>
注:若传入值有特殊字符,可以使用<[CDATA[]]包裹起来>
注入的一些其他细节
<bean id="student" class="com.cn.cmc.beans.Student" >
<constructor-arg name="id" value="100001"></constructor-arg>
<constructor-arg name="name">
<value>叶清逸</value>
</constructor-arg>
<constructor-arg name="sex" value="M" index="2" type="char"></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
<constructor-arg name="phone" ref="phone"></constructor-arg>
</bean>
<bean id="phone" class="com.cn.cmc.beans.Phone">
<property name="id" value="200001"></property>
<property name="name" value="苹果手机"></property>
</bean>
<bean id="student" class="com.cn.cmc.beans.Student" >
<constructor-arg name="id" value="100001"></constructor-arg>
<constructor-arg name="name">
<value>叶清逸</value>
</constructor-arg>
<constructor-arg name="sex" value="M" index="2" type="char"></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
<constructor-arg name="phone">
<bean class="com.cn.cmc.beans.Phone">
<property name="id" value="200001"></property>
<property name="name" value="苹果手机"></property>
</bean>
</constructor-arg>
</bean>
注:内部 bean不能被外部引用
<bean id="student" class="com.cn.cmc.beans.Student" >
<constructor-arg name="id" value="100001"></constructor-arg>
<constructor-arg name="name">
<value>叶清逸</value>
</constructor-arg>
<constructor-arg name="sex" value="M" index="2" type="char"></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
<constructor-arg name="phone" ref="phone"></constructor-arg>
<property name="phone.name" value="小米手机"></property>
</bean>
<bean id="phone" class="com.cn.cmc.beans.Phone">
<property name="id" value="200001"></property>
<property name="name" value="苹果手机"></property>
</bean>
- Collection的注入(set,map,list,property)
<property name="phoneSet">
<set>
<ref bean="phone1"/>
<ref bean="phone2"/>
<bean class="com.cn.cmc.beans.Phone">
<property name="id" value="200003"></property>
<property name="name" value="华为手机"></property>
</bean>
</set>
</property>
<property name="phoneList">
<list>
<ref bean="phone1"/>
<ref bean="phone2"/>
<bean class="com.cn.cmc.beans.Phone">
<property name="id" value="200003"></property>
<property name="name" value="华为手机"></property>
</bean>
<ref bean="phone1"/>
<ref bean="phone2"/>
</list>
</property>
<property name="phoneMap">
<map>
<entry key="1" value-ref="phone1"></entry>
<entry key="2" value-ref="phone2"></entry>
<entry key="3">
<bean class="com.cn.cmc.beans.Phone">
<property name="id" value="200003"></property>
<property name="name" value="华为手机"></property>
</bean>
</entry>
</map>
</property>
<property name="phoneProp">
<props>
<prop key="1">cmc</prop>
<prop key="2">root</prop>
</props>
</property>
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<util:list id="phonelist">
<ref bean="phone1"/>
<ref bean="phone2"/>
</util:list>
<bean id="student2" class="com.cn.cmc.beans.Student" p:id="100002" p:name="张三"
p:age="22" p:sex="F"></bean>
bean的自动装配
IOC容器可以自动装配bean,我们要做的仅仅是在bean的autowire属性中配置装配方式:
- byType 按类型自动装配
- byName 按名称自动装配
- byConstructor 按构造器自动装配(不推荐)
byName 按名称自动装配bean
<bean id="student2" class="com.cn.cmc.beans.Student" p:id="100002" p:name="张三"
p:age="22" p:sex="F" autowire="byName"></bean>
注:通过byName自动装配,属性名必须和bean中定义的属性名一致,否则装配为空
byType 按类型自动装配bean
<bean id="student3" class="com.cn.cmc.beans.Student" p:id="100002" p:name="张三"
p:age="22" p:sex="F" autowire="byType"></bean>
注:通过byType自动装配,已配置的 bean中只有一个该类型的 bean时会装配成功,有多个时会报错
bean之间的关系(继承,依赖)
bean之间的关系有:
- 继承:子bean会继承父bean中的属性
- 依赖:实例化bean时要配置依赖的bean,否则无法实例化该bean
继承与依赖的例子
<bean id="phone" class="com.cn.cmc.beans.Phone"
p:id="200001" p:name="苹果手机"></bean>
<bean id="student" class="com.cn.cmc.beans.Student"
p:id="100003" p:name="李四" p:age="24" p:sex="M" abstract="true"></bean>
<bean id="student1" class="com.cn.cmc.beans.Student"
parent="student" p:name="王五" p:id="100004" p:phone-ref="phone"
depends-on="phone"></bean>
bean的作用域配置
Spring中可以使用scope属性来配置bean的作用域:
- singleton:单例,在初始化配置文件时生成单例bean对象
- prototype:原型的,在初始化配置文件时不生成bean对象,使用时返回不同的bean对象
- request:web环境下每一个request请求都会返回一个不同的bean,只在本次请求中有效
- session:web环境下每一个request请求都会返回一个不同的bean,在session中有效
<bean id="student" class="com.cn.cmc.beans.Student"
p:id="100001" p:name="叶清逸" p:age="25" p:sex="M" scope="singleton"></bean>
IOC容器中bean的生命周期
通过init-method和destroy-method
<bean id="student" class="com.cn.cmc.beans.Student"
p:id="100001" p:name="叶清逸" p:age="25" p:sex="M" scope="singleton"
init-method="init" destroy-method="destroy"></bean>
七月 08, 2018 4:21:13 下午 org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@11028347: startup date [Sun Jul 08 16:21:13 CST 2018]
七月 08, 2018 4:21:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [C:\Users\Mr_Chuan\workspace\HelloSpring\config\bean-cycle.xml]
bean初始化...
七月 08, 2018 4:21:14 下午 org.springframework.context.support.FileSystemXmlApplicationContext doClose
信息: Closing org.springframework.context.support.FileSystemXmlApplicationContext@11028347: startup date [Sun Jul 08 16:21:13 CST 2018]
bean销毁...
由上例可以看出bean的生命周期为
1. 通过构造方法生成bean的实例
2. 为bean注入属性
3. 调用初始化方法
4. bean的使用
5. IOC容器关闭时,调用销毁方法
通过bean的后置处理器BeanPostProcessor
- 新建处理器类实现BeanPostProcessor接口
package com.cn.cmc.test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* postProcessBeforeInitialization在init-method之后调用
* 参数arg0表示实例bean本身
* 参数arg1表示IOC容器中已配置bean的名字
* 返回值实际上为返回给用户的bean
*/
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("postProcessAfterInitialization"+arg0+" "+arg1);
return arg0;
}
/**
* postProcessAfterInitialization在init-method之前调用
*/
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("postProcessBeforeInitialization"+arg0+" "+arg1);
return arg0;
}
}
- 在配置文件中配置BeanPostProcessor
<bean class="com.cn.cmc.test.MyBeanPostProcessor"></bean>
由上例可以看出加了后置处理器之后执行流程为:
1. 通过构造方法生成bean的实例
2. 为bean注入属性
3. 将bean传给后置处理器的postProcessBeforeInitialization方法
4. 调用初始化方法
5. 将bean传给后置处理器的postProcessAfterInitialization方法
6. bean的使用
7. IOC容器关闭时,调用销毁方法
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)