scope 定义 Bean 的作用范围
- singleton(默认值):单例模式的 bean 对象生命周期与容器相同
对象出生:当创建容器时,对象就被创建了
对象活着:只要容器在,对象一直活着
对象死亡:当销毁容器时,对象就被销毁了 - prototype:多例模式的 bean 对象,spring 框架只负责创建,不负责销毁
对象出生:当使用对象时,创建新的对象实例
对象活着:只要对象在使用中,就一直活着
对象死亡:当对象⻓时间不用时,被 Java 的垃圾回收器回收了
Spring Bean 的生命周期
测试一下效果
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("lazyResult".equalsIgnoreCase(beanName)) {
System.out.println("MyBeanPostProcessor before 方法拦截处理lazyResult");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if ("lazyResult".equalsIgnoreCase(beanName)) {
System.out.println("MyBeanPostProcessor after 方法拦截处理lazyResult");
}
return bean;
}
}
public class Result implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String status;
private String message;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Result{" +
"status='" + status + '\'' +
", message='" + message + '\'' +
'}';
}
public void initMethod() {
System.out.println("initMethod........");
}
@PostConstruct
public void postConstruct() {
System.out.println("postConstruct........");
}
@Override
public void setBeanName(String name) {
System.out.println("注册我成为bean时定义的id:" + name);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("管理我的beanFactory为" + beanFactory);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("高级容器接口ApplicationContext:" + applicationContext);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet........");
}
@PreDestroy
public void PreDestroy(){
System.out.println("PreDestroy......");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy......");
}
}
@Test
public void test() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
Result result = (Result) applicationContext.getBean("lazyResult");
System.out.println(result);
applicationContext.close();
}
输出:
- 注册我成为bean时定义的id:lazyResult
- 管理我的beanFactory为org.springframework.beans.factory.support.DefaultListableBeanFactory@1d9b7cce: defining beans [jdbcAccountDaoImpl,transactionProxyFactory,myBeanPostProcessor,transferServiceImpl,connectionUtils,transactionManager,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,dataSource,lazyResult]; root of factory hierarchy
- 高级容器接口ApplicationContext:org.springframework.context.support.ClassPathXmlApplicationContext@5b464ce8, started on Mon May 30 11:26:32 CST 2022
- MyBeanPostProcessor before 方法拦截处理lazyResult
- postConstruct…
- afterPropertiesSet…
- initMethod…
- MyBeanPostProcessor after 方法拦截处理lazyResult
- Result{status=‘s’, message=‘123’}
- PreDestroy…
- destroy…