關于Java中Bean的作用域詳解
1.作用域定義
限定程序中變量的可?范圍叫做作?域,或者說在源代碼中定義變量的某個區(qū)域就叫做作?域。
? Bean 的作?域是指 Bean 在 Spring 整個框架中的某種?為模式,?如 singleton 單例作?域,就表示 Bean 在整個 Spring 中只有?份,它是全局共享的,那么當其他?修改了這個值之后,那么另?個?讀取到的就是被修改的值。
2.Bean 的 6 種作用域
Spring 容器在初始化?個 Bean 的實例時,同時會指定該實例的作?域。
Spring常見的有 6 種作?域,最后四種是基于 Spring MVC ?效的:
- singleton:單例作?域
- prototype:原型作?域(多例作?域)
- request:請求作?域
- session:會話作?域
- application:全局作?域
- websocket:HTTP WebSocket 作?域
注意后 4 種狀態(tài)是 Spring MVC 中的值,在普通的 Spring 項?中只有前兩種
singleton
- 官?說明:(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
- 描述:該作?域下的Bean在IoC容器中只存在?個實例:獲取Bean(即通過applicationContext.getBean等?法獲?。┘把b配Bean(即通過@Autowired注?)都是同?對象。
- 場景:通常?狀態(tài)的Bean使?該作?域。?狀態(tài)表示Bean對象的屬性狀態(tài)不需要更新
- 備注:Spring默認選擇該作?域
prototype
- 官?說明:Scopes a single bean definition to any number of object instances.
- 描述:每次對該作?域下的Bean的請求都會創(chuàng)建新的實例:獲取Bean(即通過applicationContext.getBean等?法獲取)及裝配Bean(即通過@Autowired注?)都是新的對象實例。
- 場景:通常有狀態(tài)的Bean使?該作?域
request
- 官?說明:Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
- 描述:每次http請求會創(chuàng)建新的Bean實例,類似于prototype
- 場景:?次http的請求和響應的共享Bean
- 備注:限定SpringMVC中使?
session
- 官?說明:Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
- 描述:在?個http session中,定義?個Bean實例
- 場景:?戶回話的共享Bean, ?如:記錄?個?戶的登陸信息
- 備注:限定SpringMVC中使?
application(了解)
- 官?說明:Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
- 描述:在?個http servlet Context中,定義?個Bean實例
- 場景:Web應?的上下?信息,?如:記錄?個應?的共享信息
- 備注:限定SpringMVC中使?
websocket(了解)
- 官?說明:Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
- 描述:在?個HTTP WebSocket的?命周期中,定義?個Bean實例
- 場景:WebSocket的每次會話中,保存了?個Map結構的頭信息,將?來包裹客戶端消息頭。第?次初始化后,直到WebSocket結束都是同?個Bean。
- 備注:限定Spring WebSocket中使?
單例作?域(singleton) VS 全局作?域(application)
- singleton 是 Spring Core 的作?域;application 是 Spring Web 中的作?域;
- singleton 作?于 IoC 的容器,? application 作?于 Servlet 容器。
3. 設置作?域
使? @Scope 標簽就可以?來聲明 Bean 的作?域,?如設置 Bean 的作?域,如下代碼所示:
@Component public class Users { @Scope("prototype") @Bean(name = "u1") public User user1() { User user = new User(); user.setId(1); user.setName("Java"); // 【重點:名稱是 Java】 return user; } }
@Scope 標簽既可以修飾?法也可以修飾類,@Scope 有兩種設置?式:
- 直接設置值:@Scope("prototype")
- 使?枚舉設置:@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
到此這篇關于關于Java中Bean的作用域詳解的文章就介紹到這了,更多相關Bean的作用域內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot @Configuration @bean注解作用解析
這篇文章主要介紹了springboot @Configuration @bean注解作用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02SpringBoot整合Netty服務端的實現(xiàn)示例
Netty提供了一套完整的API,用于處理網絡IO操作,如TCP和UDP套接字,本文主要介紹了SpringBoot整合Netty服務端的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-07-07