Spring注解:@scope设置组件作用域

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);

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值