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í)候就會調(diào)用方法創(chuàng)建對象,以后每次獲取都是從Spring容器當(dāng)中拿同一個(gè)對象(map當(dāng)中)。
2 @Scope("prototype")
多實(shí)例,在IOC容器啟動(dòng)創(chuàng)建的時(shí)候,并不會直接創(chuàng)建對象放在容器中去,當(dāng)你需要調(diào)用的時(shí)候,才會從容器當(dāng)中獲取該對象然后進(jìn)行創(chuàng)建。
3 @Scope("request")
同一個(gè)請求創(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對象......"); return new Person("小孫", 28, "西安"); } }
3 新建測試類 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è)對象"); } else { System.out.println("不是同一個(gè)對象"); } } }
4、輸出效果
@Scope("prototype")
輸出結(jié)果:
容器添加Person對象......
Person{name='小孫', age='28', address='西安'}
Person{name='小孫', age='28', address='西安'}
是同一個(gè)對象
@Scope("prototype")
輸出結(jié)果:
容器添加Person對象......
容器添加Person對象......
Person{name='小孫', age='28', address='西安'}
Person{name='小孫', age='28', address='西安'}
不是同一個(gè)對象
5、@Scope注解的使用場景
目前有90%以上的業(yè)務(wù)系統(tǒng)都使用singleton單實(shí)例,因此spring也默認(rèn)的類型也是singleton,singleton雖然保證了全局是一個(gè)實(shí)例,對性能有所提高,但是如果實(shí)例中有非靜態(tài)變量時(shí),可能會導(dǎo)致線程安全、共享資源的競爭等問題。
當(dāng)設(shè)置為prototype多實(shí)例時(shí):每次連接請求,都會重新生成一個(gè)新的bean實(shí)例,這也會導(dǎo)致一個(gè)問題,當(dāng)請求數(shù)越多,性能會降低,因?yàn)轭l繁創(chuàng)建的新的實(shí)例,會導(dǎo)致GC頻繁,GC回收時(shí)長增加。要根據(jù)實(shí)際情況選擇哪一種方式。
到此這篇關(guān)于Spring中@Scope注解用法解析的文章就介紹到這了,更多相關(guān)@Scope注解用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java命令設(shè)計(jì)模式優(yōu)雅解耦命令和執(zhí)行提高代碼可維護(hù)性
本文介紹了Java命令設(shè)計(jì)模式,它將命令請求封裝成對象,以達(dá)到解耦命令請求和執(zhí)行者的目的,從而提高代碼可維護(hù)性。本文詳細(xì)闡述了該模式的設(shè)計(jì)原則、實(shí)現(xiàn)方法和優(yōu)缺點(diǎn),并提供了實(shí)際應(yīng)用場景和代碼示例,幫助讀者深入理解和應(yīng)用該模式2023-04-04Spring?Boot?使用?Disruptor?做內(nèi)部高性能消息隊(duì)列
這篇文章主要介紹了Spring?Boot?使用?Disruptor?做內(nèi)部高性能消息隊(duì)列,工作中遇到項(xiàng)目使用Disruptor做消息隊(duì)列,對你沒看錯(cuò),不是Kafka,也不是rabbitmq。Disruptor有個(gè)最大的優(yōu)點(diǎn)就是快,還有一點(diǎn)它是開源的哦,下面做個(gè)簡單的記錄2022-06-06elasticsearch索引index數(shù)據(jù)功能源碼示例
這篇文章主要為大家介紹了elasticsearch索引index功能源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04MyBatis使用嵌套查詢collection和association的實(shí)現(xiàn)
本文詳細(xì)介紹了使用MyBatis框架進(jìn)行數(shù)據(jù)庫操作時(shí),如何利用collection標(biāo)簽實(shí)現(xiàn)一對多的嵌套查詢和使用association標(biāo)簽實(shí)現(xiàn)一對一的嵌套查詢,感興趣的可以了解一下2024-09-09Spring Boot 之HelloWorld開發(fā)案例
這篇文章主要介紹了Spring Boot 之HelloWorld開發(fā)案例,需要的朋友可以參考下2017-04-04