Spring-BeanPostProcessor PostConstruct init InitializingBean 执行顺序

执行顺序探究

新建一个对象用于测试

java 复制代码
@Component
public class Student implements InitializingBean {
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * InitializingBean 提供的在属性设置之后执行的方法
	 *
	 * @throws Exception
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("====== Student afterPropertiesSet");
	}

	/**
	 * 初始化方法用于配置 bean inti
	 */
	public void init() {
		System.out.println("====== afterPropertiesSet init");
	}

	/**
	 * PostConstruct
	 */
	@PostConstruct
	public void post() {
		System.out.println("====== Student PostConstruct");
	}

}

新建一个TestBeanPostProcessor 实现 BeanPostProcessor

java 复制代码
@Component
public class TestBeanPostProcessor implements BeanPostProcessor {
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) {
		System.out.println("======== Student postProcessAfterInitialization");
		return bean;
	}
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("=============Student postProcessBeforeInitialization");
		return bean;
	}
}

通过注解的 方式进行测试

java 复制代码
@Test
public void test3() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.liyong.learn");
Student bean = (Student) context.getBean("student");
System.out.println(String.format("ben is name{%s} age is {%s}", bean.getName(), bean.getAge()));
}

得到的结果如下:

总结PostProcessBeforeInitialization -> PostConstruct -> InitializingBean -> PostProcessAfterInitialization
通过Bean.xml的方式

xml 复制代码
<bean id="student" class="com.liyong.learn.Student" init-method="init">
	<property name="name" value="english"></property>
	<property name="age" value="18"></property>
</bean>
<bean id="testBeanPostProcessor" class="com.liyong.learn.TestBeanPostProcessor"></bean>
java 复制代码
@Test
public void test () {
// 指定XML路径
String path = "beans.xml";
// 创建DefaultListableBeanFactory工厂,这也就是Spring的基本容器
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
// 创建BeanDefinition阅读器
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
// 进行BeanDefinition注册流程
reader.loadBeanDefinitions(path);
beanFactory.addBeanPostProcessor((BeanPostProcessor) beanFactory.getBean("testBeanPostProcessor"));
// Bean实例创建流程
Student student = (Student) beanFactory.getBean("student");
System.out.println(String.format("bean attrs name {%s} age {%s}", student.getName(), student.getAge()));
}

执行结果如下:

总结PostProcessBeforeInitialization -> InitializingBean -> initMethod -> PostProcessAfterInitialization

相关推荐
杨充3 分钟前
13.观察者模式设计思想
java·redis·观察者模式
Lizhihao_5 分钟前
JAVA-队列
java·开发语言
喵叔哟15 分钟前
重构代码之移动字段
java·数据库·重构
喵叔哟15 分钟前
重构代码之取消临时字段
java·前端·重构
fa_lsyk17 分钟前
maven环境搭建
java·maven
Daniel 大东36 分钟前
idea 解决缓存损坏问题
java·缓存·intellij-idea
wind瑞43 分钟前
IntelliJ IDEA插件开发-代码补全插件入门开发
java·ide·intellij-idea
HappyAcmen43 分钟前
IDEA部署AI代写插件
java·人工智能·intellij-idea
马剑威(威哥爱编程)1 小时前
读写锁分离设计模式详解
java·设计模式·java-ee
鸽鸽程序猿1 小时前
【算法】【优选算法】前缀和(上)
java·算法·前缀和