Scope的解释是:Scope用来声明容器中的对象所应该处的限定场景或者说该对象的存活时间,即容器在对象进入其相应的scope之前生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象。
在Spring注解开发中@Scope注解可以用于设置组件的作用域,通过@Scope源码,可以发现@Scope注解有四种作用域,即
SINGLETON:单例模式,默认模式,不写的时候默认是SINGLETON
PROTOTYPE:原型模式
REQUEST:同一次请求则只创建一次实例
SESSION:同一个session只创建一次实例
@Scope源码:
/**
- Specifies the name of the scope to use for the annotated component/bean.
-
Defaults to an empty string ({@code ""}) which implies
- {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SINGLETON}.
- @since 4.2
- @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
- @see ConfigurableBeanFactory#SCOPE_SINGLETON
- @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
- @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
- @see #value
*/
@AliasFor(“value”)
String scopeName() default “”;
1、SINGLETON
SINGLETON是IOC容器默认的作用域,所以写和不写没有区别。
当作用域为SINGLETON的时候,在Spring的IOC容器从创建到退出都只会存在一个实例,
所有对该对象的引用将共享这个实例。该实例从容器启动,并因为第一次被请求而初始化后,
将一直存活到容器退出,也就是说,它与IoC容器的寿命“几乎”相同。
1)、新建一个实体类
package com.xinyi.bean;
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Person() {
}
}
2)、新建一个配置类:
package com.xinyi.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import com.xinyi.bean.Person;
@Configuration
public class MyConfig1 {
//默认单实例,可以不写
@Scope("singleton")
@Bean("person")
public Person person() {
System.out.println("IOC容器中注入person实例");
return new Person("李青",18);
}
}
3)、新建一个测试类:
package com.xinyi.test;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.xinyi.config.MyConfig1;
public class IOCTest {
@Test
public void test2() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig1.class);
}
}
Object person1 = applicationContext.getBean(“person”);
Object person2 = applicationContext.getBean(“person”);
System.out.println(person1 == person2);
@Test
public void test2() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig1.class);
System.out.println("IOC容器创建完成");
}
@Configuration
public class MyConfig1 {
//默认单实例
@Bean("person")
@Lazy
public Person person() {
System.out.println("IOC容器中注入person实例");
return new Person("李青",18);
}
}
@Test
public void test2() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig1.class);
System.out.println("IOC容器创建完成");
Object person1 = applicationContext.getBean("person");
Object person2 = applicationContext.getBean("person");
System.out.println(person1 == person2);
}
public class MyConfig1 {
//将作用域single设置为原型模式
@Scope("prototype")
@Bean("person")
public Person person() {
System.out.println("IOC容器中注入person实例");
return new Person("李青",18);
}
}
执行测试类代码,并无输出结果,则说明在IOC容器启动的时候并不会调用方法创建实例放入到IOC容器中,则再添加根据id获取对象的方法:
Object person1 = applicationContext.getBean("person");
Object person2 = applicationContext.getBean("person");
System.out.println(person1 == person2);