Spring 的几个接口
1.InitializingBean接口,在初始化Bean时容器会调用前者的afterPropertiesSet()方法
2.DisposableBean接口,在析构Bean时容器会调用destroy()方法,在下面的例子中好像没有体现出来(革命尚未成功,同志仍需努力)
3.BeanFactoryAware接口,当它被BeanFactory创建后,它会拥有一个指向创建它的BeanFactory的引用
4.BeanPostProcessor接口,这个接口两个方法,postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName) 在其他Bean构造前后执行
5.BeanFactoryPostProcessor接口,Spring IoC容器允许BeanFactoryPostProcessor在容器实际实例化任何其它的bean之前读取配置元数据,并有可能修改它
下面来看一下例子
- packagecom.test;
- publicclassUser{
- privateStringname;
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- System.out.println("User类setName方法");
- this.name=name;
- }
- publicvoidinit()throwsException{
- System.out.println("User类init方法");
- }
- publicUser(){
- System.out.println("User类构造方法");
- }
- }
- packagecom.test;
- importorg.springframework.beans.BeansException;
- importorg.springframework.beans.factory.BeanFactory;
- importorg.springframework.beans.factory.BeanFactoryAware;
- importorg.springframework.beans.factory.DisposableBean;
- importorg.springframework.beans.factory.InitializingBean;
- importorg.springframework.beans.factory.config.BeanFactoryPostProcessor;
- importorg.springframework.beans.factory.config.BeanPostProcessor;
- importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;
- publicclassJavaBeanimplementsInitializingBean,DisposableBean,BeanFactoryAware,BeanPostProcessor,BeanFactoryPostProcessor{
- privateStringname;
- publicJavaBean(){
- System.out.println("JavaBean类构造方法");
- }
- @Override
- publicvoidafterPropertiesSet()throwsException{
- System.out.println("InitializingBean接口afterPropertiesSet方法");
- }
- @Override
- publicvoiddestroy()throwsException{
- System.out.println("DisposableBean接口destroy方法");
- }
- publicvoidinit()throwsException{
- System.out.println("JavaBean类init方法");
- }
- @Override
- publicvoidsetBeanFactory(BeanFactorybeanFactory)throwsBeansException{
- System.out.println("BeanFactoryAware接口setBeanFactory方法");
- }
- @Override
- publicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)
- throwsBeansException{
- System.out.println(beanName+":BeanPostProcessor接口postProcessAfterInitialization方法");
- returnbean;
- }
- @Override
- publicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)
- throwsBeansException{
- System.out.println(beanName+":BeanPostProcessor接口postProcessBeforeInitialization方法");
- returnbean;
- }
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- System.out.println("JavaBean类setName方法");
- this.name=name;
- }
- @Override
- publicvoidpostProcessBeanFactory(
- ConfigurableListableBeanFactorybeanFactory)throwsBeansException{
- System.out.println("BeanFactoryPostProcessor接口postProcessBeanFactory方法");
- }
- }
- packagecom.test;
- importorg.springframework.context.ApplicationContext;
- importorg.springframework.context.support.ClassPathXmlApplicationContext;
- publicclassTest{
-
-
- publicstaticvoidmain(String[]args){
- System.out.println("加载Spring配置文件");
- ApplicationContextcontext=
- newClassPathXmlApplicationContext("spring.xml");
- System.out.println("加载Spring配置文件结束");
- JavaBeanbean=(JavaBean)context.getBean("javaBean");
- System.out.println("获取name属性:"+bean.getName());
- System.out.println("程序结束");
- }
- }
Spring配置文件
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <beanid="javaBean"class="com.test.JavaBean"init-method="init">
- <propertyname="name"value="zhjb"></property>
- </bean>
- <beanid="user"class="com.test.User"init-method="init">
- <propertyname="name"value="zhjb"></property>
- </bean>
- </beans>
运行结果如下:
加载Spring配置文件
JavaBean类构造方法
JavaBean类setName 方法
BeanFactoryAware 接口 setBeanFactory方法
InitializingBean 接口 afterPropertiesSet方法
JavaBean类init 方法
BeanFactoryPostProcessor 接口 postProcessBeanFactory 方法
User类构造方法
User类 setName方法
user:BeanPostProcessor 接口 postProcessBeforeInitialization 方法
User类init 方法
user:BeanPostProcessor 接口 postProcessAfterInitialization方法
加载Spring配置文件结束
获取name属性:zhjb
程序结束
从结果进行分析,Bean的初始化顺序应该是
1.构造函数
2.初始化属性
3.如果实现了BeanFactoryAware 接口执行setBeanFactory方法
4..如果实现了InitializingBean 接口执行afterPropertiesSet方法
5.如果在配置文件中指定了init-method,那么执行该方法
6..如果实现了BeanFactoryPostProcessor 接口在 “new”其他类之前执行 postProcessBeanFactory 方法(通过这个方法可以改变配置文件里面的属性值的配置)
7.如果实现了BeanFactoryPostProcessor 接口,那么会在其他bean初始化方法之前执行postProcessBeforeInitialization 方法,之后执行postProcessAfterInitialization方法
感觉奇怪的地方就是没有执行destroy方法,目前还不知道原因在什么地方