这篇文章讲述的是Spring框架中的IOC容器及bean管理,如有错误或者不当之处,还望各位大神批评指正。
<bean id="student" class="com.cn.cmc.beans.Student" >
<property name="name" value="叶清逸">property>
bean>
/**
* ApplicationContext代表IOC容器
* ClassPathXmlApplicationContext从类路径下读取配置文件
* FileSystemXmlApplicationContext从文件系统路径下读取配置文件
* WebXmlApplicationContext:在web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
*/
ApplicationContext context = new FileSystemXmlApplicationContext("config/beans.xml") ;
/**
* XmlBeanFactory生成工厂bean
* ClassPathResource加载配置文件
*/
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config/beans.xml")) ;
/*方式一:通过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());
方法类
package com.cn.cmc.beans;
import java.util.HashMap;
import java.util.Map;
public class StaticStudentFactory {
private static Map map ;
static {
map = new HashMap();
map.put("叶清逸", new Student(100001,"叶清逸",'M',25)) ;
map.put("张三", new Student(100002,"张三",'F',22)) ;
}
/*通过静态方法获取bean的实例*/
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>
<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>
<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">cmcprop>
<prop key="2">rootprop>
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>
IOC容器可以自动装配bean,我们要做的仅仅是在bean的autowire属性中配置装配方式:
<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中定义的属性名一致,否则装配为空
id="student3" class="com.cn.cmc.beans.Student" p:id="100002" p:name="张三"
p:age="22" p:sex="F" autowire="byType">
注:通过byType自动装配,已配置的 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>
Spring中可以使用scope属性来配置bean的作用域:
<bean id="student" class="com.cn.cmc.beans.Student"
p:id="100001" p:name="叶清逸" p:age="25" p:sex="M" scope="singleton">bean>
<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]; root of context hierarchy
七月 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]; root of context hierarchy
bean销毁...
由上例可以看出bean的生命周期为
1. 通过构造方法生成bean的实例
2. 为bean注入属性
3. 调用初始化方法
4. bean的使用
5. IOC容器关闭时,调用销毁方法
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 {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization"+arg0+" "+arg1);
return arg0;
}
/**
* postProcessAfterInitialization在init-method之前调用
*/
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization"+arg0+" "+arg1);
return arg0;
}
}
<bean class="com.cn.cmc.test.MyBeanPostProcessor">bean>
由上例可以看出加了后置处理器之后执行流程为:
1. 通过构造方法生成bean的实例
2. 为bean注入属性
3. 将bean传给后置处理器的postProcessBeforeInitialization方法
4. 调用初始化方法
5. 将bean传给后置处理器的postProcessAfterInitialization方法
6. bean的使用
7. IOC容器关闭时,调用销毁方法