Spring的@Scope注解作用解析
一、@Scope注解
1、@Scope注解作用
@Scope注解用于設(shè)置實例的作用域。
默認(rèn)值是單實例,即當(dāng)IOC容器啟動后就調(diào)用該方法創(chuàng)建對象放到IOC容器中,以后每次獲取就是直接從容器中獲取。
2、@Socpe注解的值
// 多實例:IOC容器啟動并不會調(diào)用方法創(chuàng)建對象放在容器中。每次獲取的時候才會調(diào)用方法創(chuàng)建對象 @see ConfigurableBeanFactory#SCOPE_PROTOTYPE -> prototype // 單實例(默認(rèn)值):IOC容器啟動后就調(diào)用該方法創(chuàng)建對象放到IOC容器中,以后每次獲取就是直接從容器中獲取 @see ConfigurableBeanFactory#SCOPE_SINGLETON -> singleton //下面兩個值用于web應(yīng)用: // 同一次請求創(chuàng)建一個實例 @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST -> request // 同一個session創(chuàng)建一個實例 @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION -> session
3、標(biāo)注位置
? 可標(biāo)注在類和方法上

4、源碼查看

二、@Scope注解單實例案例
1、項目結(jié)構(gòu)

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容器啟動并不會調(diào)用方法創(chuàng)建對象放在容器中。每次獲取的時候才會調(diào)用方法創(chuàng)建對象
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE -> prototype
*
* 單實例(默認(rèn)值):IOC容器啟動后就調(diào)用該方法創(chuàng)建對象放到IOC容器中,以后每次獲取就是直接從容器中獲取
* @see ConfigurableBeanFactory#SCOPE_SINGLETON -> singleton
*
* 同一次請求創(chuàng)建一個實例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST -> request
*
* 同一個session創(chuàng)建一個實例
* @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 注解的默認(rèn)值(單實例) :IOC容器啟動后就調(diào)用該方法創(chuàng)建對象放到IOC容器中,以后每次獲取就是直接從容器中獲取
*/
System.out.println(persion1 == persion2);
}
}5、測試結(jié)果

三、@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容器啟動并不會調(diào)用方法創(chuàng)建對象放在容器中。每次獲取的時候才會調(diào)用方法創(chuàng)建對象
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE -> prototype
*
* 單實例(默認(rèn)值):IOC容器啟動后就調(diào)用該方法創(chuàng)建對象放到IOC容器中,以后每次獲取就是直接從容器中獲取
* @see ConfigurableBeanFactory#SCOPE_SINGLETON -> singleton
*
* 同一次請求創(chuàng)建一個實例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST -> request
*
* 同一個session創(chuàng)建一個實例
* @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 注解設(shè)置多實例:IOC容器啟動并不會調(diào)用方法創(chuàng)建對象放在容器中。每次獲取的時候才會調(diào)用方法創(chuàng)建對象
*/
System.out.println(persion1 == persion2);
}
}3、測試結(jié)果

四、使用注解
@ComponentScan開啟包掃描,在類上標(biāo)注注解@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容器啟動并不會調(diào)用方法創(chuàng)建對象放在容器中。每次獲取的時候才會調(diào)用方法創(chuàng)建對象
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE -> prototype
*
* 單實例(默認(rèn)值):IOC容器啟動后就調(diào)用該方法創(chuàng)建對象放到IOC容器中,以后每次獲取就是直接從容器中獲取
* @see ConfigurableBeanFactory#SCOPE_SINGLETON -> singleton
*
* 同一次請求創(chuàng)建一個實例
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST -> request
*
* 同一個session創(chuàng)建一個實例
* @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 注解設(shè)置多實例:IOC容器啟動并不會調(diào)用方法創(chuàng)建對象放在容器中。每次獲取的時候才會調(diào)用方法創(chuàng)建對象
*/
System.out.println(persion1 == persion2);
}
}4、測試結(jié)果

到此這篇關(guān)于Spring的@Scope注解作用解析的文章就介紹到這了,更多相關(guān)@Scope注解作用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot執(zhí)行有返回值的異步任務(wù)問題
這篇文章主要介紹了SpringBoot執(zhí)行有返回值的異步任務(wù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
使用Mybatis-plus實現(xiàn)對數(shù)據(jù)庫表的內(nèi)部字段進(jìn)行比較
這篇文章主要介紹了使用Mybatis-plus實現(xiàn)對數(shù)據(jù)庫表的內(nèi)部字段進(jìn)行比較方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

