栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring注解@Scope

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

    @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
public class BeanConfig {


    

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

        
        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
public class BeanConfig {


    

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

        
        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;



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

        
        System.out.println(persion1 == persion2);

    }

}

4、测试结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671224.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号