【Spring源码解析】组件注册-@Scope设置组件作用域

前言

接上一篇 Spring源码解析之@ComponentScan自定义TypeFilter指定过滤规则

实现

  1. 配置
1
2
3
4
5
6
7
8
9
10
@Configuration
public class BeanConfig2 {

// singleton prototype
@Scope("singleton")
@Bean
public Person person() {
return new Person("config", 18);
}
}
  1. 测试
1
2
3
4
5
6
7
8
9
10
11
@Test
public void test02() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig2.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String name: beanDefinitionNames) {
System.out.println(name);
}
Person bean = applicationContext.getBean(Person.class);
Person bean2 = applicationContext.getBean(Person.class);
System.out.println(bean2 == bean);
}

最后

本篇到此结束,欢迎大家关注公众号【当我遇上你】。