当前位置:   article > 正文

关于Spring几个特殊类的说明与备忘(BeanNameAware\ApplicationContextAware\InitializingBean\DisposableBean)_setapplicationcontext afterpropertiesset 顺序

setapplicationcontext afterpropertiesset 顺序
关于Spring几个特殊类的说明与备忘

BeanNameAware\ApplicationContextAware\InitializingBean\

DisposableBean\FactoryBean\BeanPostProcessor\BeanFactoryPostProcessor


applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
<bean id="s" class="cn.net.zhaozhiwen. spring.Test"/>
<bean id="b" class="java.io.IOException"/>
<bean id="ss" class="java.lang.String"/>
</beans>


package cn.net.zhaozhiwen.spring;
public class Test implements BeanFactoryPostProcessor, BeanPostProcessor,InitializingBean, DisposableBean, ApplicationContextAware{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("==postProcessBeforeInitialization=========================");
return bean;
}


@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("==postProcessAfterInitialization=========================");
return bean;
}


@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
System.out.println("==postProcessBeanFactory=========================");
}


@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("==destroy=========================");
}


@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
System.out.println("== setApplicationContext========================");
}


@Override
public void afterPropertiesSet() throws Exception {
System.out.println("== initiailBean afterPropertiesSet=========================");

}
}
总结:
实现了BeanNameAware接口类,在注入时,会调用setBeanName方法
实现了ApplicationContextAware接口类,在注入时,会调用setApplicationContext方法
实现了InitializingBean接口的类,在实例初始化时,会调用afterPropertiesSet方法
实现了DisposableBean接口的类,在容器关闭(applicationContext.close())时,会调用destroy方法
实现了FactoryBean接口的类,在获取Bean(applicationContext.getBean())时,会调用其getObject方法
实现了BeanFactoryPostProcessor的类,在容器启动时,只调用一次postProcessBeanFactory方法
同时实现了BeanFactoryPostProcessor和BeanPostProcessor的类,postProcessBeforeInitialization/postProcessAfterInitialization方法不会被调用
同时实现了BeanFactoryPostProcessor和InitializingBean的类,afterPropertiesSet方法先于postProcessBeanFactory方法被调用

同时实现了BeanNameAware/ApplicationContextAware/InitializingBean的类,调用顺序:setBeanName>setApplicationContext>afterPropertiesSet


-----------------------Testing--------------------------------

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
applicationContext.start();
applicationContext.close();

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/658183
推荐阅读
相关标签
  

闽ICP备14008679号