Spring注解驱动开发学习总结2:组件注册之@Scope、@Lazy详解
1、使用@Scope设置组件作用域
给容器中的组件添加@Scope注解,可以设置组件的作用域。查看@Scope注解,可以看到value值可以设置以下4个:
1))SCOPE_PROTOTYPE=“prototype”:多实例。ioc容器启动时不会去调用方法,创建对象并注入容器,而是在每次获取改实例时候,才会调用方法并创建对象注入容器。
2)SCOPE_SINGLETON=“singleton”:单实例(默认值)。ioc容器在启动时就会调用方法,创建对象并注入到容器中。
3)SCOPE_REQUEST="request":每次请求实例化一个实例;
4)SCOPE_SESSION="session":同一个session共享一个实例;
public @interface Scope {
/**
* Specifies the name of the scope to use for the annotated component/bean.
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
* @see ConfigurableBeanFactory#SCOPE_SINGLETON
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
*/
@AliasFor("value")
String scopeName() default "";
}
接着上篇,还是以Person类为bean来进行举例说明
1.1 scope设置为默认值singleton
新建配置类com/example/config/MainConfig2.java,该配置只注入一个Person的bean,并且不填写scope的值,那么默认就是singleton。
package com.example.config;
import com.example.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class MainConfig2 {
@Scope()
@Bean
public Person person() {
System.out.println("给容器添加Person");
return new Person("lisi", 20);
}
}
然后在测试方法中新建测试方法,从ioc容器中取出两个person实例,查看下这2个实例是否是同一个。
@Test
public void testScope() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
System.out.println("\nioc容器已创建完成\n");
Person person1 = applicationContext.getBean(Person.class);
Person person2 = applicationContext.getBean(Person.class);
System.out.println("从容器中获取的两个Person是否相等:");
System.out.println(person1==person2);
}
运行testScope,可以得到下图。
可以看到,当scope默认为singleton时,person1和person2是同一个实例,并且该实例在容器创建完成之前就已经注入容器中。
1.2 scope设置为prototype
修改配置类com/example/config/MainConfig2.java,将scope的值填写为多实例的:prototype.
package com.example.config;
import com.example.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class MainConfig2 {
@Scope("prototype")
@Bean
public Person person() {
System.out.println("给容器添加Person");
return new Person("lisi", 20);
}
}
然后再次运行testScope,可以得到下图。
可以看到,在prototype的作用域时,person1和person2不相等,它们是2个实例。并且它们都是在ioc容器创建后,在需要使用时才会真正注入到容器中。
2、使用@Lazy使得bean懒加载
从上一小节中,可以看到到scope作用域为默认的singleton时,ioc容器创建时,bean实例就已经创建并注入到容器中了。
而对单实例的bean添加@Lazy注解的话,那么在ioc容器创建时不会创建对象。只有在第一次使用/获取时才会创建对象并注入容器中。
因此可以继续修改配置类com/example/config/MainConfig2.java,将scope的值填写为默认值,同时添加上@Lazy注解:
package com.example.config;
import com.example.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
@Configuration
public class MainConfig2 {
@Lazy
@Scope()
@Bean
public Person person() {
System.out.println("给容器添加Person");
return new Person("lisi", 20);
}
}
此时,再次运行testScope测试方法,可以得到下图。
可以看到,由于scope是单实例的,所以person1和person2实例仍然是同一个。
但是由于添加了@Lazy注解,因此person实例是在ioc容器创建完成后,并且在使用时才创建的。(可以和1.1的结果做一个对比)