Spring注解@Scope
Spring注解@Scope
一、@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;}@Overridepublic 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@Beanpublic 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")@Beanpublic 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;}@Overridepublic 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);}}