@Scope使用示例:
1.创建配置类MainConfig2,注册组件。
@Scope
@Bean("person")
public Person person01(){
System.out.println("给容器中添加person.......");
return new Person("张良",23);
}
2.创建JUnit测试。
@Test
public void test02(){
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
System.out.println("IOC容器创建完成");
Object bean1 = context.getBean("person");
Object bean2 = context.getBean("person");
System.out.println(bean1 == bean2);
}
3.测试结果
由上面的示例,可以知道Scope默认是单实例,获取的两次bean是相等的。
总结:@Scope有4个取值:prototype、singleton、request、session.
- prototype:多实