Spring中@Scope注解用法解析
@Scope 定義以及作用
@Scope注解主要作用是調(diào)節(jié)Ioc容器中的作用域
在Spring IoC容器中主要有以下五種作用域:
基本作用域:singleton(單例)、prototype(多例);Web 作用域(reqeust、session、globalsession),自定義作用域。
@Scope 作用域類型
1 @Scope("singleton")
單實(shí)例屬于默認(rèn)作用域,IOC容器啟動(dòng)的時(shí)候就會(huì)調(diào)用方法創(chuàng)建對(duì)象,以后每次獲取都是從Spring容器當(dāng)中拿同一個(gè)對(duì)象(map當(dāng)中)。
2 @Scope("prototype")
多實(shí)例,在IOC容器啟動(dòng)創(chuàng)建的時(shí)候,并不會(huì)直接創(chuàng)建對(duì)象放在容器中去,當(dāng)你需要調(diào)用的時(shí)候,才會(huì)從容器當(dāng)中獲取該對(duì)象然后進(jìn)行創(chuàng)建。
3 @Scope("request")
同一個(gè)請(qǐng)求創(chuàng)建一個(gè)實(shí)例
4 @Scope("session")
同一個(gè)session創(chuàng)建一個(gè)實(shí)例
5 @Scope("globalsession")
同一個(gè)globalsession創(chuàng)建一個(gè)實(shí)例
示例演示
1 新建Person.java
package com.spring.bean;
public class Person {
private String name;
private Integer age;
private String address;
public Person(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public Person() {
}
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", address='" + address + '\'' +
'}';
}
}2 新建配置類 TestScopeConfig.java
package com.spring.config;
import com.spring.bean.Person;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class TestScopeConfig {
@Bean
@Scope("singleton")
//@Scope("prototype")
public Person person() {
System.out.println("容器添加Person對(duì)象......");
return new Person("小孫", 28, "西安");
}
}3 新建測(cè)試類 TestScope.java
package com.spring.test;
import com.spring.bean.Person;
import com.spring.config.TestBeanConfig;
import com.spring.config.TestScopeConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestScope {
public static void main(String[] args) {
//配置文件方式
AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestScopeConfig.class);
Object person1 = annotationContext.getBean("person");
Object person2 = annotationContext.getBean("person");
System.out.println(person1);
System.out.println(person2);
boolean flag = person1 == person2;
if (flag) {
System.out.println("是同一個(gè)對(duì)象");
} else {
System.out.println("不是同一個(gè)對(duì)象");
}
}
}4、輸出效果
@Scope("prototype")
輸出結(jié)果:
容器添加Person對(duì)象......
Person{name='小孫', age='28', address='西安'}
Person{name='小孫', age='28', address='西安'}
是同一個(gè)對(duì)象
@Scope("prototype")
輸出結(jié)果:
容器添加Person對(duì)象......
容器添加Person對(duì)象......
Person{name='小孫', age='28', address='西安'}
Person{name='小孫', age='28', address='西安'}
不是同一個(gè)對(duì)象
5、@Scope注解的使用場(chǎng)景
目前有90%以上的業(yè)務(wù)系統(tǒng)都使用singleton單實(shí)例,因此spring也默認(rèn)的類型也是singleton,singleton雖然保證了全局是一個(gè)實(shí)例,對(duì)性能有所提高,但是如果實(shí)例中有非靜態(tài)變量時(shí),可能會(huì)導(dǎo)致線程安全、共享資源的競(jìng)爭(zhēng)等問(wèn)題。
當(dāng)設(shè)置為prototype多實(shí)例時(shí):每次連接請(qǐng)求,都會(huì)重新生成一個(gè)新的bean實(shí)例,這也會(huì)導(dǎo)致一個(gè)問(wèn)題,當(dāng)請(qǐng)求數(shù)越多,性能會(huì)降低,因?yàn)轭l繁創(chuàng)建的新的實(shí)例,會(huì)導(dǎo)致GC頻繁,GC回收時(shí)長(zhǎng)增加。要根據(jù)實(shí)際情況選擇哪一種方式。
到此這篇關(guān)于Spring中@Scope注解用法解析的文章就介紹到這了,更多相關(guān)@Scope注解用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)壓縮字符串和java字符串過(guò)濾
這篇文章主要介紹了java實(shí)現(xiàn)壓縮字符串和java字符串過(guò)濾,需要的朋友可以參考下2014-04-04
java中orElse和orElseGet方法區(qū)別小結(jié)
這篇文章主要給大家介紹了關(guān)于java中orElse和orElseGet方法區(qū)別的相關(guān)資料,兩者之間的區(qū)別細(xì)微,但是卻在某些場(chǎng)景下顯的很重要,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
Spring Boot 配置 Quartz 定時(shí)任務(wù)的方法
這篇文章主要介紹了Spring Boot 配置 Quartz 定時(shí)任務(wù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
mvc架構(gòu)實(shí)現(xiàn)商品的購(gòu)買(二)
這篇文章主要為大家詳細(xì)介紹了mvc架構(gòu)實(shí)現(xiàn)商品購(gòu)買功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
SpringBoot加載不出來(lái)application.yml文件的解決方法
這篇文章主要介紹了SpringBoot加載不出來(lái)application.yml文件的解決方法,文中通過(guò)示例代碼講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作有一定的幫助,需要的朋友跟著小編來(lái)一起來(lái)學(xué)習(xí)吧2023-12-12
Java中==與equals()及hashcode()三者之間的關(guān)系詳解
最近也是在讀Hollis的《深入理解Java核心技術(shù)》里面一節(jié)講到了equals()和hashcode()的關(guān)系,對(duì)于這個(gè)高頻面試點(diǎn),咱們需要認(rèn)真理清一下幾者之間的關(guān)系2022-10-10
springboot集成nacos報(bào)錯(cuò):get data from Nacos
這篇文章給大家介紹了springboot集成nacos報(bào)錯(cuò):get data from Nacos error,dataId:null.yaml的原因及解決方法,如果又遇到相同問(wèn)題的朋友可以參考閱讀本文2023-10-10

