标签:ica autowire run system get text public 结果 spring
一 .概述
在spring之中常用的Bean的生命周期常见的有单例模型和多例模型.
我们可以使用@Scope完成声明.
二 .@Scope
[1]创建组件
public class Car {
}
public class Bike {
}
[2]创建配置类
@Configuration
public class ScopeConfig {
@Bean
public Car car() {
return new Car();
}
@Bean
@Scope("prototype")
public Bike bike() {
return new Bike();
}
}
[3] 测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ScopeConfig.class)
public class ScopeTest {
@Autowired
private ApplicationContext context;
@Test
public void test1() {
System.out.println(context.getBean("car") == context.getBean("car"));
System.out.println(context.getBean("bike") == context.getBean("bike"));
}
查看运行结果:
true false
我们通过结果就知道Bean的Scope了.
标签:ica autowire run system get text public 结果 spring
原文地址:https://www.cnblogs.com/trekxu/p/9094866.html