观察IOC中容器初始化某个Bean顺序,现先一个JAVABean类,看看控制台输出:
package com.google.aop.exception.ioc;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class JAVABean implements InitializingBean, DisposableBean,
BeanFactoryAware, BeanPostProcessor, BeanFactoryPostProcessor {
// private MyBean myBean;
//
// public MyBean getMyBean() {
// return myBean;
// }
//
// public void setMyBean(MyBean myBean) {
// System.out.println("JAVABean 中初始化 属性myBean");
// this.myBean = myBean;
// }
public JAVABean() {
System.out.println("JAVABean 初始化成功");
}
public void init() {
System.out.println("JAVABean init");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("JAVABean afterPropertiesSet");
}
@Override
public void destroy() throws Exception {
System.out.println("JAVABean destroy");
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("JAVABean setBeanFactory");
}
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("JAVABean postProcessAfterInitialization");
return null;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("JAVABean postProcessBeforeInitialization");
return null;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
System.out.println("JAVABean postProcessBeanFactory");
}
}
配置文件iocContext.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" 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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="javabean" class="com.google.aop.exception.ioc.JAVABean"
init-method="init">
<!-- <property name="myBean" ref="MyBean"></property> -->
</bean>
<!--
<bean id="MyBean" class="com.google.aop.exception.ioc.MyBean"
init-method="init"> <property name="javabean"
ref="javabean"></property> </bean>
-->
</beans>
控制台输出:
JAVABean 初始化成功
JAVABean setBeanFactory
JAVABean afterPropertiesSet
JAVABean init
JAVABean postProcessBeanFactory
可见是先实例化类->初始化属性->设置创建这个类的beanFactory引用->调用afterPropertiesSet->调用init初始化方法->调用postProcessBeanFactory
现在让JAVABean和MyBean互相引用,看IOC是怎么解决这个问题
package com.google.aop.exception.ioc;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class JAVABean implements InitializingBean, DisposableBean,
BeanFactoryAware, BeanPostProcessor, BeanFactoryPostProcessor {
private MyBean myBean;
public MyBean getMyBean() {
return myBean;
}
public void setMyBean(MyBean myBean) {
System.out.println("JAVABean 中初始化 属性myBean");
this.myBean = myBean;
}
public JAVABean() {
System.out.println("JAVABean 初始化成功");
}
public void init() {
System.out.println("JAVABean init");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("JAVABean afterPropertiesSet");
}
@Override
public void destroy() throws Exception {
System.out.println("JAVABean destroy");
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("JAVABean setBeanFactory");
}
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("JAVABean postProcessAfterInitialization");
return null;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("JAVABean postProcessBeforeInitialization");
return null;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
System.out.println("JAVABean postProcessBeanFactory");
}
}
MyBean:
package com.google.aop.exception.ioc;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBean implements InitializingBean, DisposableBean,
BeanFactoryAware, BeanPostProcessor, BeanFactoryPostProcessor {
private JAVABean javabean;
public MyBean() {
System.out.println("MyBean 初始化成功");
}
public String get() {
return "Mybean";
}
public void init(){
System.out.println("MyBean init");
}
public JAVABean getJavabean() {
return javabean;
}
public void setJavabean(JAVABean javabean) {
this.javabean = javabean;
System.out.println("MyBean 属性设置成功");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("MyBean afterPropertiesSet");
}
@Override
public void destroy() throws Exception {
System.out.println("MyBean destroy");
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("MyBean setBeanFactory");
}
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("MyBean postProcessAfterInitialization");
return null;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("MyBean postProcessBeforeInitialization");
return null;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
System.out.println("MyBean postProcessBeanFactory");
}
}
iocContext.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" 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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="javabean" class="com.google.aop.exception.ioc.JAVABean"
init-method="init">
<property name="myBean" ref="MyBean"></property>
</bean>
<bean id="MyBean" class="com.google.aop.exception.ioc.MyBean"
init-method="init">
<property name="javabean" ref="javabean"></property>
</bean>
</beans>
控制台输出:
JAVABean 初始化成功
MyBean 初始化成功
MyBean 属性设置成功
MyBean setBeanFactory
MyBean afterPropertiesSet
MyBean init
JAVABean 中初始化 属性myBean
JAVABean setBeanFactory
JAVABean afterPropertiesSet
JAVABean init
JAVABean postProcessBeanFactory
MyBean postProcessBeanFactory
由结果可知,当IOC容器初始化JAVABean后进行属性初始化,发现MyBean这类属性还没初始化,则先初始化Mybean类直到调用他的init方法,之后再回到JAVABean类初始化,将Mybean引用赋给JavaBean