BeanFactory 中 Bean的生命周期
![在这里插入图片描述](https://img-blog.csdnimg.cn/6eef8d47ccd3475c8e0e951b3234a34a.png)
带★的表示为容需要实现的接口,不带星号的表示是bean要实现的接口,因为是BeanFactory,所以只有在getbean方法调用时才会走生命周期
接口 |
返回值 |
方法 |
参数 |
作用 |
InstantiationAwareBeanPostProcessor |
Object |
postProcessBeforeInstantiation |
Class beanClass, String beanName |
在bean实例化之前调用,返回的Object必须与beanClass相同,返回的object就是当前的bean |
|
boolean |
postProcessAfterInstantiation |
Object bean, String beanName |
在bean实例化之后调用,如果返回false,则不会进行第四步postProcessProperties和第五步设置初始值 |
|
PropertyValues |
postProcessProperties |
PropertyValues pvs, Object bean, String beanName |
在bean实例化之后,在bean赋值前调用,pvs存放的是配置文件里的信息,可以在赋值前进行最后修改 |
|
|
postProcessPropertyValues(新版本已废弃) |
|
在bean实例化之后,在bean赋值前调用 |
接口 |
返回值 |
方法 |
参数 |
作用 |
BeanNameAware |
void |
setBeanName |
name |
关联bean名 |
BeanFactoryAware |
void |
setBeanFactory |
BeanFactory beanFactory |
关联bean工厂 |
InitializingBean |
void |
afterPropertiesSet |
无 |
初始化bean方法 |
DisposableBean |
void |
destroy |
无 |
销毁bean方法 |
接口 |
返回值 |
方法 |
参数 |
作用 |
BeanPostProcessor |
Object |
postProcessBeforeInitialization |
Object bean, String beanName |
不用bean实现的初始化前置方法 |
|
Object |
postProcessAfterInitialization |
Object bean, String beanName |
不用bean实现的初始化后置方法 |
接口 |
方法 |
作用 |
Ordered |
getOrder |
多个InstantiationAwareBeanPostProcessor 或 BeanPostProcessor 时指定顺序 |
测试代码
配置文件
<?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="dog" class="com.ke.Dog" init-method="myInit" destroy-method="myDestroy" >
<property name="name" value="dsbgs"/>
</bean>
</beans>
InstantiationAwareBeanPostProcessor 接口实现类
public class FactoryLife implements InstantiationAwareBeanPostProcessor{
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("postProcessBeforeInstantiation方法执行");
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInstantiation方法执行");
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
MutablePropertyValues p = (MutablePropertyValues) pvs;
//如果bean有age这个字段则可以设置值,设置不存在的属性会报错
//p.add("age",16);
System.out.println("postProcessProperties方法执行");
return pvs;
}
}
bean相关生命周期设置代码
public class Dog implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog() {
System.out.println("初始化");
}
public void show(){
System.out.println("Hello,"+ name );
}
//BeanNameAware接口实现
@Override
public void setBeanName(String name) {
System.out.println("setBeanName方法执行");
}
//BeanNameAware接口实现
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
BeanFactory beanFactoryTo = beanFactory;
System.out.println("setBeanFactory方法执行");
}
//InitializingBean接口实现
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet方法执行");
}
//DisposableBean接口实现
@Override
public void destroy() throws Exception {
System.out.println("destroy方法执行");
}
//配置文件配置的初始化方法
public void myInit()
{
System.out.println("myInit方法执行");
}
//配置文件配置的销毁方法
public void myDestroy()
{
System.out.println("myDestroy方法执行");
}
}
BeanPostProcessor 接口相关实现
public class BeanLife implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization方法执行");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization方法执行");
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}
使用
public class test {
public static void main(String[] args) throws IOException {
ResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource resource = pathMatchingResourcePatternResolver.getResource("beans.xml");
DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);
xmlBeanDefinitionReader.loadBeanDefinitions(resource);
//注入生命周期接口实现类
defaultListableBeanFactory.addBeanPostProcessor(new FactoryLife());
defaultListableBeanFactory.addBeanPostProcessor(new BeanLife());
Dog hello = (Dog)defaultListableBeanFactory.getBean("dog");
//销毁bean
defaultListableBeanFactory.destroySingleton("dog");
}
}
输出
![在这里插入图片描述](https://img-blog.csdnimg.cn/e629a36ef81144ce8c349fcda74bc5f8.png)
结论
- InstantiationAwareBeanPostProcessor 接口主要用于容器级别的生命周期控制
- BeanPostProcessor接口主要用于整体bean的控制
- Bean类实现的接口,可以针对每个bean进行处理
- InitDestroyAnnotationBeanPostProcessor 接口可以实现用注解 @PostConstruct 和 @PreDestroy 在Bean 实例创建完成和销毁之前
如果让bean实现接口,不是一个很好的方法,这样类就依赖spring,在其他环境下就无法使用。
BeanNameAware和BeanFactoryAware可以用属性注入的方法来替代。InitializingBean,DisposableBean用配置文件的形式也是相同。BeanPostProcessor接口可以在后面实现AOP的功能。
ApplicationContext中 Bean的生命周期
![在这里插入图片描述](https://img-blog.csdnimg.cn/e840abc7a1f146749f6f00d2ccccc638.png)
ApplicationContext相比beanFactory多了两个接口
BeanFactoryPostProcessor接口和ApplicationContextAware接口,而且不用再用代码注入这些,spring会通过反射自动加载,所以写在配置文件中即可。
baen.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="hello" class="com.ke.Hello">
<property name="name" value="dsbcql"/>
<property name="age" value="18"/>
</bean>
<!--注入对应的生命周期接口,id随意,class实现对应接口就能注入-->
<bean id="myBeanFactoryPostProcessor1" class="com.ke.life.ApplicationContextLife"/>
<bean id="myInstantiationAwareBeanPostProcessor" class="com.ke.life.FactoryLife"/>
<bean id="myBeanPostProcessor" class="com.ke.life.BeanLife"/>
</beans>
bean(实现ApplicationContextAware ,可以得到applicationContext)
public class Hello implements ApplicationContextAware {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public Hello() {
System.out.println("初始化");
}
public void show(){
System.out.println("Hello,"+ name );
System.out.println("Hello,"+ age );
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("实现ApplicationContextAware");
}
}
BeanFactoryPostProcessor 实现类
public class ApplicationContextLife implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition hello = beanFactory.getBeanDefinition("hello");
MutablePropertyValues propertyValues = hello.getPropertyValues();
propertyValues.add("age",6666);
}
}
使用
public class test {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = classPathXmlApplicationContext.getBean("hello",Hello.class);
hello.show();
}
}