spring boot bean生命周期
@Component public class BussinessPerson implements Person, BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean { private Animal animal = null; @Override public void service() { this.animal.use(); } @Override @Autowired @Qualifier("dog") public void setAnimal(Animal animal) { System.out.println("setAnimal "); this.animal = animal; } @Override public void setBeanName(String s) { System.out.println(this.getClass().getSimpleName()+":setBeanName"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println(this.getClass().getSimpleName()+":setBeanFactory"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println(this.getClass().getSimpleName()+":setApplicationContext"); } @Override public void afterPropertiesSet() throws Exception { System.out.println(this.getClass().getSimpleName()+":afterPropertiesSet"); } @PostConstruct public void init() { System.out.println(this.getClass().getSimpleName()+":PostConstruct"); } @PreDestroy public void destroy1() throws Exception { System.out.println(this.getClass().getSimpleName()+":PreDestroy"); } @Override public void destroy() throws Exception { System.out.println(this.getClass().getSimpleName()+":destory"); } }
@Component public class BeanPostProcessorExample implements BeanPostProcessor { @Nullable @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization"+bean.getClass().getSimpleName()+":"+beanName); return bean; } @Nullable @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization"+bean.getClass().getSimpleName()+":"+beanName); return bean; } }
setAnimal
BussinessPerson:setBeanName
BussinessPerson:setBeanFactory
BussinessPerson:setApplicationContext
postProcessBeforeInitializationBussinessPerson:bussinessPerson
BussinessPerson:PostConstruct
BussinessPerson:afterPropertiesSet
postProcessAfterInitializationBussinessPerson:bussinessPerson
BussinessPerson:PreDestroy
BussinessPerson:destory