BeanConfigAndLifeCycle
package com.study.spring.config;
import com.study.spring.bean.Cat;
import com.study.spring.bean.Dog;
import com.study.spring.bean.Pet;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfigAndLifeCycle {
/**
* 执行顺序: Construct(构造器) > @postConstruct > InitializingBean接口 > @Bean(initMethod) > BeanPostProcessor.postProcessBeforeInitialization
* 执行顺序: @preDestroy >DisposableBean接口 > @Bean(destroyMethod)
* 1)、指定初始化和销毁方法;
* 通过@Bean指定init-method和destroy-method;
* 2)、通过让Bean实现InitializingBean(定义初始化逻辑),
* DisposableBean(定义销毁逻辑);
* 3)、可以使用JSR250;
* 通过@PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
* 通过@PreDestroy:在容器销毁bean之前通知我们进行清理工作
* 4)、BeanPostProcessor【interface】:bean的后置处理器;
* 在bean初始化前后进行一些处理工作;
* postProcessBeforeInitialization:在初始化之前工作
* postProcessAfterInitialization:在初始化之后工作
*
* * Spring底层对 BeanPostProcessor 的使用;
* * bean赋值,注入其他组件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;
* @return
*/
//@Bean(initMethod = "running", destroyMethod = "back")
@Bean(initMethod = "running", destroyMethod = "back")
public Dog dog() {
return new Dog();
}
//@Bean
public Cat cat() {
return new Cat();
}
}
Dog
package com.study.spring.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Dog implements Pet,BeanPostProcessor, InitializingBean, DisposableBean {
public Dog() {
System.out.println("dog is Construct ing...");
}
@Override
public void running() {
System.out.println("dog is running...");
}
@Override
public void back() {
System.out.println("dog is back...");
}
@Override
public void destroy() throws Exception {
System.out.println("dog is destroy()");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("dog is afterPropertiesSet()...");
}
@PostConstruct
public void postConstruct() throws Exception {
System.out.println("dog is postConstruct()");
}
@PreDestroy
public void preDestroy() throws Exception {
System.out.println("dog is preDestroy()");
}
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
System.out.println("dog is postProcessBeforeInitialization()" + o);
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("dog is postProcessAfterInitialization()" + o);
return o;
}
}
初始化执行顺序:
Construct(构造器) > @postConstruct > InitializingBean接口 > @Bean(initMethod) > BeanPostProcessor.postProcessBeforeInitialization > BeanPostProcessor.postProcessAfterInitialization
销毁方法执行顺序:
@preDestroy >DisposableBean接口 > @Bean(destroyMethod)
InitializingBean接口 > @Bean(initMethod)执行顺序原因:
AbstractAutowireCapableBeanFactory
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
throws Throwable {
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
((InitializingBean) bean).afterPropertiesSet();
return null;
}
}, getAccessControlContext());
}
catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
((InitializingBean) bean).afterPropertiesSet();
}
}
if (mbd != null) {
String initMethodName = mbd.getInitMethodName();
if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
invokeCustomInitMethod(beanName, bean, mbd);
}
}
}