Spring Bean 生命周期测试
Bean的生命周期是开始创建到销毁的过程。需要实现相关的类BeanNameAware ,DisposableBean, InitializingBean ,并注册InstantiationAwareBeanPostProcessor。
Bean类实现BeanNameAware ,DisposableBean, InitializingBean 接口
package com.bean.life.entity; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*; import org.springframework.stereotype.Component; @Component public class User implements BeanFactoryAware , BeanNameAware , InitializingBean , DisposableBean { @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("BeanFactoryAware setBeanFactory"); } @Override public void setBeanName(String s) { System.out.println("BeanNameAware接口: setBeanName = " + s); } @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean接口: afterPropertiesSet"); } @Override public void destroy() throws Exception { System.out.println("DisposableBean接口: destroy"); }
@PostConstruct public void init(){ System.out.println("PostConstruct"); } @PreDestroy public void destory(){ System.out.println("PreDestroy"); } }
注册InstantiationAwareBeanPostProcessor接口。
这里的InstantiationAwareBeanPostProcessorAdapter是 InstantiationAwareBeanPostProcessor 的子孙类。
package com.bean.life.entity; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; import org.springframework.stereotype.Component; import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; @Component public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { public MyInstantiationAwareBeanPostProcessor() { super(); } @Override public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException { if(beanName.equals("user")){ System.out.println("MyInstantiationAwareBeanPostProcessor接口: determineCandidateConstructors: " + beanName); } return super.determineCandidateConstructors(beanClass, beanName); } @Override public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException { if(beanName.equals("user")){ System.out.println("MyInstantiationAwareBeanPostProcessor接口: getEarlyBeanReference: " + beanName); } return super.getEarlyBeanReference(bean, beanName); } @Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { if(beanName.equals("user")){ System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInstantiation: " + beanName); } return super.postProcessBeforeInstantiation(beanClass, beanName); } @Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { if(beanName.equals("user")){ System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInstantiation : " + beanName); } return super.postProcessAfterInstantiation(bean, beanName); } @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { if(beanName.equals("user")){ System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessPropertyValues: " + beanName); } return super.postProcessPropertyValues(pvs, pds, bean, beanName); } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if(beanName.equals("user")){ System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInitialization: " + beanName); } return super.postProcessBeforeInitialization(bean, beanName); } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(beanName.equals("user")){ System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInitialization: " + beanName); } return super.postProcessAfterInitialization(bean, beanName); } }
输出
MyBeanFactoryPostProcessor --- BeanFactoryPostProcessor MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInstantiation: user MyInstantiationAwareBeanPostProcessor接口: determineCandidateConstructors: user MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInstantiation : user MyInstantiationAwareBeanPostProcessor接口: postProcessPropertyValues: user BeanNameAware接口: setBeanName = user BeanFactoryAware setBeanFactory MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInitialization: user PostConstruct InitializingBean接口: afterPropertiesSet MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInitialization: user PreDestroy DisposableBean接口: destroy
生命周期图解
Spring 实现按 init-method 和destory-method的三种方式
- 方式1:xml配置方式
package com.bean.life.entity;
public class User
{
public void init(){
System.out.println("PostConstruct");
}
public void destory(){
System.out.println("PreDestroy");
}
}
<bean id="user" class="com.bean.life.entity" init-method="init" destroy-method="destroy">
</bean>
- 方式2:注解配置
添加@PostConstruct 和@PreDestroy进行指定
package com.bean.life.entity; @Component public class User { @PostConstruct public void init(){ System.out.println("PostConstruct"); } @PreDestroy public void destory(){ System.out.println("PreDestroy"); } }
- 方式3: 使用注解@Bean
public @interface Bean { @AliasFor("name") String[] value() default {}; @AliasFor("value") String[] name() default {}; Autowire autowire() default Autowire.NO; String initMethod() default ""; String destroyMethod() default "(inferred)"; }
@Configuration public class UserConfig{ @Bean(initMethod="init",destoryMethod="destory") public User user(){ return new User(); } }
推荐:《Java常用技术和书籍推荐》
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
如果,您希望更容易地发现我的新博客,不妨关注一下。因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客。
本文版权归博客园-冬眠的山谷(https://www.cnblogs.com/lgjlife/)所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出。