本文关于spring Bean生命周期,做了一个简单的Demo(Annotation风格),spring版本:5.2.0.RELEASE。
运行结果
build.gradle添加spring-context依赖
// https://mvnrepository.com/artifact/org.springframework/spring-context
compile group: 'org.springframework', name: 'spring-context', version: '5.2.0.RELEASE'
测试类BeanLifeCycleTest
package com.tbryant.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class BeanLifeCycleTest implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, BeanPostProcessor, DisposableBean {
@Override
public void setBeanName(String name) {
System.out.println("=====1=====调用BeanNameAware.setBeanName(name:" + name + ")");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("=====2=====调用BeanFactoryAware.setBeanFactory(beanFactory:" + beanFactory + ")");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("=====3=====调用ApplicationContextAware.setApplicationContext(applicationContext:" + applicationContext + ")");
}
@PostConstruct
public void beanLifeCycleTestInit() {
System.out.println("=====4=====@PostConstruct注解的方法beanLifeCycleTestInit()");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("=====5=====调用InitializingBean.afterPropertiesSet()");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("=====6=====调用BeanPostProcessor.postProcessBeforeInitialization(bean:" + bean + "beanName:" + beanName + ")");
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("=====7=====调用BeanPostProcessor.postProcessAfterInitialization(bean:" + bean + "beanName:" + beanName + ")");
return null;
}
@PreDestroy
public void beanLifeCycleTestDestroy() {
System.out.println("=====9=====@PreDestroy注解的方法beanLifeCycleTestDestroy()");
}
@Override
public void destroy() throws Exception {
System.out.println("=====10=====调用DisposableBean.destroy()");
}
}
配置类AppConfig
package com.tbryant.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.tbryant.spring")
public class AppConfig {
}
启动类Application
package com.tbryant.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
BeanLifeCycleTest beanLifeCycleTest = (BeanLifeCycleTest) annotationConfigApplicationContext.getBean("beanLifeCycleTest");
System.out.println("=====8===== Use Bean =====8=====" + beanLifeCycleTest);
annotationConfigApplicationContext.close();
}
}