当前位置: 首页 > article >正文

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);}}
4、测试结果

在这里插入图片描述

http://www.lryc.cn/news/2418314.html

相关文章:

  • BPMN 2.0规范详解
  • Mutual Information 互信息的应用
  • 【教程】如何为自己的小程序添加统计工具
  • Vlan和Trunk配置
  • 数据可视化(二):犯罪案件分析
  • 2022美赛题目
  • android_button onclick点击事件的5种写法
  • Matlab在自动控制领域中的应用
  • 网址导航
  • 合宙ESP32C3 Arduino 初探教程
  • 上网行为网络管理系统 (2024年最强行为管理软件科普)
  • Cloudflare + 远程浏览器隔离
  • 在自己的网页中iframe别人的电子地图
  • 开发者的职场成长路径
  • APACHE服务器httpd.exe进程占用cpu100%的解决方法
  • UWB芯片介绍
  • 永磁同步电机表贴式和嵌入式
  • 吴晓波:预见2021(跨年演讲 —— 02 “云上中国”初露峥嵘)
  • python爬虫进阶(二):动态网页爬虫
  • Win11系统提示找不到ngentasklauncher.dll文件的解决办法
  • 韩国反外挂分析
  • 5.Struts 2拦截器(Interceptor)
  • 游戏模型提取工具NinjaRipper
  • XCoder 项目使用教程
  • 节奏大师服务器不稳定,节奏大师无法登陆的原因及解决方法
  • MCSE2003 第一门考试感受
  • 躲猫猫是什么意思
  • 深入解析DDoS攻击:原理、影响与防御手段
  • Fedora16安装教程
  • 启动IIS出现0x8ffe2740错误的解决办法