Spring生命周期的整体回顾
一,spring的注入bean的生命周期的集中方式已经在前面的文章里面描述了,这里借用大佬的图片再次描述的清楚些;
上面描述的是spring容器初始化的一个流程。
这里实现BeanPostProcessor重写postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName)方法
- postProcessBeforeInitialization(Object bean, String beanName)在afterPropertiesSet() 之前执行
- postProcessAfterInitialization(Object bean, String beanName)在myInit() 方法之后执行
后置处理器实例
public class Category { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Category{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}'; } }
2.我的后置处理器
配置文件
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="category" class="base.domain.Category"> <property name="id" value="1"/> <property name="name" value="于红亮"/> </bean> <bean id="product" class="base.lifecycle.Product"/> <bean id="myBeanPostProcessor" class="base.beanpostProcessor.MyBeanPostProcessor"/> </beans>
3.测试
@Test public void test21() { ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext4.xml"); Category category = (Category)ctx.getBean("category"); System.out.println(category); }
4.其他说明
会对所有的bean进行后置处理,避免这个情况的发生要善于使用Object bean, String beanName方法参数,进行instanceof或者使用beanName进行某些判断
3.总结
后置处理器是在bean创建后,在初始化前后进行的 处理,通过上面的图片应该能够深入的理解其所在的位置。为了嘉庆理解下面在放一次。
实现BeanPostProcessor重写postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName)方法
- postProcessBeforeInitialization(Object bean, String beanName)在afterPropertiesSet() 之前执行
- postProcessAfterInitialization(Object bean, String beanName)在myInit() 方法之后执行
2.后置处理器实例
- 实体类
package base.domain;
/**
* @author yuhl
* @Date 2020/11/2 21:48
* @Classname Category
* @Description TODO
*/
public class Category {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 我的后置处理器
package base.beanpostProcessor;
import base.domain.Category;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author yuhl
* @Date 2020/11/2 21:45
* @Classname MyBeanPostProcessor
* @Description 此bean的后置处理器会对所有的bean进行加工。所以针对某一个类型的bean进行加工,或者对某一个beanName进行加工,可以进行控制哦!
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Category) {
Category category = (Category) bean;
category.setName("yuhl");
}
return bean;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 配置文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="category" class="base.domain.Category">
<property name="id" value="1"/>
<property name="name" value="于红亮"/>
</bean>
<bean id="product" class="base.lifecycle.Product"/>
<bean id="myBeanPostProcessor" class="base.beanpostProcessor.MyBeanPostProcessor"/>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 测试
@Test
public void test21() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext4.xml");
Category category = (Category)ctx.getBean("category");
System.out.println(category);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 其他说明
会对所有的bean进行后置处理,避免这个情况的发生要善于使用Object bean, String beanName方法参数,进行instanceof或者使用beanName进行某些判断
3.总结
后置处理器是在bean创建后,在初始化前后进行的 处理,通过上面的图片应该能够深入的理解其所在的位置。为了嘉庆理解下面在放一次。
有志者、事竟成,破釜沉舟,百二秦关终属楚;
苦心人、天不负,卧薪尝胆,三千越甲可吞吴.
加油吧,致每个正在奋斗路上的你!!!