public class TestBean implements InitializingBean, DisposableBean {
public TestBean() { System.out.println("1: constructor ----> 构造方法"); }
@PostConstruct public void postConstruct() { System.out.println("3: @PostConstruct/postConstruct "); }
@PreDestroy public void PreDestroy() { System.out.println("7: @PreDestroy/PreDestroy "); }
public void initMethod() { System.out.println("5: initMethod指定的initMethod方法"); }
public void destroyMethod() { System.out.println("9: destroyMethod指定的destroyMethod方法"); }
@Override public void destroy() throws Exception { System.out.println("8: DisposableBean/destroy"); }
@Override public void afterPropertiesSet() throws Exception { System.out.println("4: InitializingBean/afterPropertiesSet......"); } } public class TestBeanPostPorcesser implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if(beanName.equals("testBean")) {
System.out.println("2: BeanPostProcessor/postProcessBeforeInitialization: " + beanName);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if(beanName.equals("testBean")) {
System.out.println("6: BeanPostProcessor/postProcessAfterInitialization: " + beanName);
}
return bean;
}
} @Configuration public class TestConfig {
@Bean(initMethod = "initMethod",destroyMethod = "destroyMethod")
public TestBean testBean() {
return new TestBean();
}
@Bean
public TestBeanPostPorcesser testBeanPostPorcesser() {
return new TestBeanPostPorcesser();
}
} ******************************************************************** 1: constructor ----> 构造方法 2: BeanPostProcessor/postProcessBeforeInitialization: testBean 3: @PostConstruct/postConstruct 4: InitializingBean/afterPropertiesSet...... 5: initMethod指定的initMethod方法 6: BeanPostProcessor/postProcessAfterInitialization: testBean 2021-03-20 22:53:18.404 INFO 7492 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 7: @PreDestroy/PreDestroy 8: DisposableBean/destroy 9: destroyMethod指定的destroyMethod方法 Process finished with exit code 130 |