面試突擊79:Bean 作用域是啥?它有幾種類型?
Spring 框架作為一個管理 Bean 的 IoC 容器,那麼 Bean 自然是 Spring 中的重要資源了,那 Bean 的作用域是什麼意思?又有幾種類型呢?接下來我們一起來看。
PS:Java 中的公共類可稱之為 Bean 或 Java Bean。
1.作用域
Bean 的作用域是指 Bean 在 Spring 整個框架中的某種行為模式。比如 singleton 單例作用域,就表示 Bean 在整個 Spring 中只有一份,它是全局共享的,當有人修改了這個值之後,那麼另一個人讀取到的就是被修改後的值。
舉個例子,比如我們在 Spring 中定義了一個單例的 Bean 對象 user(默認作用域為單例),具體實現程式碼如下:
@Component
public class UserBean {
@Bean
public User user() {
User user = new User();
user.setId(1);
user.setName("Java"); // 此行為重點:用戶名稱為 Java
return user;
}
}
然後,在 A 類中使用並修改了 user 對象,具體實現程式碼如下:
@Controller
public class AController {
@Autowired
private User user;
public User getUser() {
User user = user;
user.setName("MySQL"); // 此行為重點:將 user 名稱修改了
return user;
}
}
最後,在 B 類中也使用了 user 對象,具體實現程式碼如下:
@Controller
public class BController {
@Autowired
private User user;
public User getUser() {
User user = user;
return user;
}
}
此時我們訪問 B 對象中的 getUser 方法,就會發現此時的用戶名為 A 類中修改的「MySQL」,而非原來的「Java」,這就說明 Bean 對象 user 默認就是單例的作用域。如果有任何地方修改了這個單例對象,那麼其他類再調用就會得到一個修改後的值。
2.作用域分類
在 Spring 中,Bean 的常見作用域有以下 5 種:
- singleton:單例作用域;
- prototype:原型作用域(多例作用域);
- request:請求作用域;
- session:會話作用域;
- application:全局作用域。
注意:後 3 種作用域,只適用於 Spring MVC 框架。
2.1 singleton
-
官方說明:(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
-
描述:該作用域下的 Bean 在 IoC 容器中只存在一個實例:獲取 Bean(即通過 applicationContext.getBean等方法獲取)及裝配 Bean(即通過 @Autowired 注入)都是同一個對象。
-
場景:通常無狀態的 Bean 使用該作用域。無狀態表示 Bean 對象的屬性狀態不需要更新。
-
備註:Spring 默認選擇該作用域。
2.2 prototype
-
官方說明:Scopes a single bean definition to any number of object instances.
-
描述:每次對該作用域下的 Bean 的請求都會創建新的實例:獲取 Bean(即通過 applicationContext.getBean 等方法獲取)及裝配 Bean(即通過 @Autowired 注入)都是新的對象實例。
-
場景:通常有狀態的 Bean 使用該作用域。
2.3 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 請求會創建新的 Bean 實例,類似於 prototype。
-
場景:一次 Http 的請求和響應的共享 Bean。
-
備註:限定 Spring MVC 框架中使用。
2.4 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, 比如:記錄一個用戶的登陸資訊。
-
備註:限定 Spring MVC 框架中使用。
2.5 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 應用的上下文資訊,比如:記錄一個應用的共享資訊。
-
備註:限定 Spring MVC 框架中使用。
3.作用域設置
我們可以通過 @Scope 註解來設置 Bean 的作用域,它的設置方式有以下兩種:
- 直接設置作用域的具體值,如:@Scope(“prototype”);
- 設置 ConfigurableBeanFactory 和 WebApplicationContext 提供的 SCOPE_XXX 變數,如 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)。
具體設置程式碼如下:
總結
Bean 的作用域是指 Bean 在 Spring 整個框架中的某種行為模式。Bean 的常見作用域有 5 種:singleton(單例作用域)、prototype(原型作用域)、request(請求作用域)、session(請求作用域)、application(全局作用域),注意後 3 種作用域只適用於 Spring MVC 框架。
是非審之於己,毀譽聽之於人,得失安之於數。
公眾號:Java面試真題解析