scope 定义 Bean 的作用范围

  • singleton(默认值):单例模式的 bean 对象生命周期与容器相同
    对象出生:当创建容器时,对象就被创建了
    对象活着:只要容器在,对象一直活着
    对象死亡:当销毁容器时,对象就被销毁了
  • prototype:多例模式的 bean 对象,spring 框架只负责创建,不负责销毁
    对象出生:当使用对象时,创建新的对象实例
    对象活着:只要对象在使用中,就一直活着
    对象死亡:当对象⻓时间不用时,被 Java 的垃圾回收器回收了

Spring Bean 的生命周期

测试一下效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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......");
}
}
1
2
3
4
5
6
7
8
@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……