Spring的@Scope注解作用解析
作者:大树下躲雨
这篇文章主要介绍了Spring的@Scope注解作用解析,@Scope注解用于设置实例的作用域,默认值是单实例,即当IOC容器启动后就调用该方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获取,需要的朋友可以参考下
一、@Scope注解
1、@Scope注解作用
@Scope注解用于设置实例的作用域。
默认值是单实例,即当IOC容器启动后就调用该方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获取。
2、@Socpe注解的值
// 多实例:IOC容器启动并不会调用方法创建对象放在容器中。每次获取的时候才会调用方法创建对象 @see ConfigurableBeanFactory#SCOPE_PROTOTYPE -> prototype // 单实例(默认值):IOC容器启动后就调用该方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获取 @see ConfigurableBeanFactory#SCOPE_SINGLETON -> singleton //下面两个值用于web应用: // 同一次请求创建一个实例 @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST -> request // 同一个session创建一个实例 @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION -> session
3、标注位置
可标注在类和方法上

4、源码查看

二、@Scope注解单实例案例
1、项目结构

2、Persion实体
public class Persion {
private String name;
private int age;
public Persion(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}3、Bean注册配置类
import com.dashu.bean.Persion;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;
/**
* @Configuration 注解:告诉Spring这是一个配置类
*
* 配置类 == 配置文件(beans.xml文件)
*
*/
@Configuration
public class BeanConfig {
/**
*
* 多实例:IOC容器启动并不会调用方法创建对象放在容器中。每次获取的时候才会调用方法创建对象
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE -> prototype
*
* 单实例(默认值):IOC容器启动后就调用该方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获取
* @see ConfigurableBeanFactory#SCOPE_SINGLETON -> singleton
*
* 同一次请求创建一个实例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST -> request
*
* 同一个session创建一个实例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION -> session
*/
@Scope
@Bean
public Persion persion(){
return new Persion("张三",20);
}
}4、测试类
import com.dashu.bean.Persion;
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
Persion persion1 = (Persion) annotationConfigApplicationContext.getBean("persion");
Persion persion2 = (Persion) annotationConfigApplicationContext.getBean(Persion.class);
/**
*
* 两次获取Persion的实例,都是同一个
*
* @Scope 注解的默认值(单实例) :IOC容器启动后就调用该方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获取
*/
System.out.println(persion1 == persion2);
}
}5、测试结果

三、@Scope注解多实例案例
1、Bean注册配置类
import com.dashu.bean.Persion;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;
/**
* @Configuration 注解:告诉Spring这是一个配置类
*
* 配置类 == 配置文件(beans.xml文件)
*
*/
@Configuration
public class BeanConfig {
/**
*
* 多实例:IOC容器启动并不会调用方法创建对象放在容器中。每次获取的时候才会调用方法创建对象
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE -> prototype
*
* 单实例(默认值):IOC容器启动后就调用该方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获取
* @see ConfigurableBeanFactory#SCOPE_SINGLETON -> singleton
*
* 同一次请求创建一个实例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST -> request
*
* 同一个session创建一个实例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION -> session
*/
@Scope("prototype")
@Bean
public Persion persion(){
return new Persion("张三",20);
}
}2、测试类
import com.dashu.bean.Persion;
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
Persion persion1 = (Persion) annotationConfigApplicationContext.getBean("persion");
Persion persion2 = (Persion) annotationConfigApplicationContext.getBean(Persion.class);
/**
*
* 两次获取Persion的实例不相同
*
* @Scope 注解设置多实例:IOC容器启动并不会调用方法创建对象放在容器中。每次获取的时候才会调用方法创建对象
*/
System.out.println(persion1 == persion2);
}
}3、测试结果

四、使用注解
@ComponentScan开启包扫描,在类上标注注解@Scope(多实例)案例
1、Persion实体
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
*
* 多实例:IOC容器启动并不会调用方法创建对象放在容器中。每次获取的时候才会调用方法创建对象
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE -> prototype
*
* 单实例(默认值):IOC容器启动后就调用该方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获取
* @see ConfigurableBeanFactory#SCOPE_SINGLETON -> singleton
*
* 同一次请求创建一个实例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST -> request
*
* 同一个session创建一个实例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION -> session
*/
@Scope("prototype")
@Component
public class Persion {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}2、Bean注册配置类
import com.dashu.bean.Persion;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;
/**
* @Configuration 注解:告诉Spring这是一个配置类
*
* 配置类 == 配置文件(beans.xml文件)
*
*/
@Configuration
@ComponentScan(value = "com.dashu")
public class BeanConfig {
}3、测试类
import com.dashu.bean.Persion;
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
Persion persion1 = (Persion) annotationConfigApplicationContext.getBean("persion");
Persion persion2 = annotationConfigApplicationContext.getBean(Persion.class);
/**
*
* 两次获取Persion的实例不相同
*
* @Scope 注解设置多实例:IOC容器启动并不会调用方法创建对象放在容器中。每次获取的时候才会调用方法创建对象
*/
System.out.println(persion1 == persion2);
}
}4、测试结果

到此这篇关于Spring的@Scope注解作用解析的文章就介绍到这了,更多相关@Scope注解作用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
