Spring基础组件的使用——@Scope和@Lazy及其验证
@Scope和@Lazy及其验证 @Scope @Lazy @Scope 我们大家都知道Spring容器中注册了一个Bean,在默认情况下都是单实例的,我们可以来验证一下,继续沿用我们之前的例子。下面是我们的配置类 @Configuration @ComponentScan ( value = "com.test" ) public class TestConfig { @Scope @Bean public Dog dog ( ) { return new Dog ( "金毛" , 3 ) ; } } 然后我们在测试类中进行测试 public class TestMain { @Test public void test ( ) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext ( TestConfig . class ) ; Object dog1 = applicationContext . getBean ( "dog" ) ; Object dog2 = applicationContext . getBean ( "dog" ) ; System . out . println ( dog1 == dog2 ) ; } }