成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring框架之Bean Scope

開發 架構
Spring容器默認的作用域,只有一個共享的單例bean實例被管理,id與bean定義的id匹配的bean請求,spring容器都會返回一個特定的bean實例。

Spring框架支持六個作用域,其中四個只有在使用web感知的ApplicationContext時才可用。

Spring支持以下6中bean scopes:

  • singleton:單例模式(默認值),在Spring容器中只會創建一個實例。
  • prototype:原型模式,每次通過Spring容器獲取bean時,容器都會新建一個實例。
  • request:每次HTTP請求都會創建一個實例,但只在http request范圍有效。
  • session:?在http session生命周期內,共享一個實例,不同session有不同的實例。
  • application:在ServletContext生命周期內,只有一個實例。
  • webSocket:在webSocket范圍內只有一個實例。

Singleton scope

Spring容器默認的作用域,只有一個共享的單例bean實例被管理,id與bean定義的id匹配的bean請求,spring容器都會返回一個特定的bean實例。換句話說,當您定義一個bean為單例時,Spring IoC容器只會創建該bean的一個實例。這個實例存儲在緩存中,所有后續對這個bean的請求和引用都返回緩存對象。

基于java configuration方式定義一個singleton的bean

@Configuration
public class AppConfiguration {
@Bean
@Scope("singleton") // default scope
public UserService userService(){
return new UserService();
}
}

prototype scope

原型模式會導致每次請求都會創新一個新的bean實例,就是說當一個bean被注入到另一個bean中,或者通過getBean()方法調用請求它時會新創建一個bean實例。通常,有狀態的bean使用prototype scope,無狀態的bean使用singleton scope。與其它scope不同,spring容器不管理原型scope的整個生命周期,容器實例化、配置和以其他方式組裝原型對象,都將其交給client,而無需進一步記錄該原型實例。

基于java configuration方式定義一個prototype的bean

@Configuration
public class AppConfiguration {
@Bean
@Scope("prototype")
public UserService userService(){
return new UserService();
}
}

具有原型bean依賴項的單例bean

當您將單例bean與原型bean的依賴項一起使用時,請注意,依賴項是在實例化時解析的。因此,如果依賴項將原型注入到單例bean中,則會實例化一個新的原型bean,然后將依賴項注入到單例bean中。原型實例是唯一一個提供給單例bean的實例。

Request、Session、Application和WebSocket作用域

Request、Session、Application、WebSocket作用域僅在使用web感知的Spring ApplicationContext實現(如XmlWebApplicationContext)時可用。如果將這些作用域與常規Spring IoC容器(如ClassPathXmlApplicationContext)一起使用,則會拋出一個IllegalStateException,未知bean作用域。

鑒于目前技術發展采用前后端分離模式開發,已很少單獨使用Spring web mvc模式,此處不在講述這四個作用域,大家可以參考官方文檔了解。

InitializingBean和DisposableBean

在Spring中,為了與容器bean的生命周期管理進行交互,可以實現InitializingBean和DisposableBean接口。容器初始化時執行AfterPropertieSet(),銷毀是調用destroy(),以便bean在初始化和銷毀bean時執行某些操作。

@PostConstruct和@PreDestroy注釋通常被認為是新版Spring應用程序中接收生命周期回調的最佳實踐。使用這些注釋意味著您的bean沒有耦合到特定于Spring的接口。

  1. 實現InitializingBean接口,在所有bean設置完properties后將運行afterPropertiesSet()。
  2. 實現DisposableBean接口,在Spring容器釋放bean后運行destroy()。

InitializingBean和DisposableBean示例

在本例中,我們將使用afterPropertiesSet()方法在應用程序啟動期間使用用戶對象填充內存中的列表數據結構。我們還將在應用程序關閉期間使用destroy()方法從列表中刪除用戶對象。

package com.demo.spring;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class DatabaseInitiaizer implements InitializingBean, DisposableBean {
private List < User > listOfUsers = new ArrayList < > ();
@Override
public void afterPropertiesSet() throws Exception {
User user = new User(1, "User");
User user1 = new User(2, "Admin");
User user2 = new User(3, "SuperAdmin");
listOfUsers.add(user);
listOfUsers.add(user1);
listOfUsers.add(user2);
System.out.println("-----------List of users added in init() method ------------");
for (Iterator < User > iterator = listOfUsers.iterator(); iterator.hasNext();) {
User user3 = (User) iterator.next();
System.out.println(user3.toString());
}
// save to database
}
@Override
public void destroy() {
// Delete from database
listOfUsers.clear();
System.out.println("-----------After of users removed from List in destroy() method ------------");
for (Iterator < User > iterator = listOfUsers.iterator(); iterator.hasNext();) {
User user3 = (User) iterator.next();
System.out.println(user3.toString());
}
System.out.println("List is clean up ..");
}
}
package com.demo.spring;
public class User {
private Integer id;
private String name;
public User() {}
public User(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
package com.demo.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.demo.spring")
public class AppConfig {
}

注意:@ComponentScan注解掃描指定包中所有包中包含@Component注解的類,basePackages指定包路徑。

package com.demo.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
context.close();
}
}

Output:

-----------List of users added in init() method ------------
User [id=1, name=User]
User [id=2, name=Admin]
User [id=3, name=SuperAdmin]
-----------After of users removed from List in destroy() method -------
List is clean up ..
責任編輯:姜華 來源: 今日頭條
相關推薦

2024-01-05 08:38:20

SpringBeanScope

2022-06-07 07:58:45

SpringSpring AOP

2024-02-23 10:33:34

SpringBean容器

2022-05-30 09:32:07

Spring容器

2022-02-19 07:41:36

Bean注解項目

2022-06-08 08:04:28

Springservicerepository

2021-07-05 08:43:46

Spring Beanscope作用域

2023-09-05 08:23:56

SpringScope方法

2020-10-14 06:23:54

SpringBean實例化

2022-07-20 07:32:46

Prototypevalue?容器

2011-03-18 09:27:00

Spring

2022-03-14 08:54:42

SpringBean生命周期

2024-11-26 17:43:51

2024-11-14 14:53:04

2022-05-30 11:17:44

Spring容器配置

2022-06-09 07:27:14

JavaSpring容器

2025-02-28 08:16:14

Spring框架注解

2022-03-03 07:34:31

注解容器作用域

2021-03-08 08:40:25

Spring Bean 創建單例對象

2009-06-17 17:20:14

BeanFactorySpring
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩欧美视频网站 | jizz视频| 在线观看免费毛片 | 美女视频一区二区三区 | 粉嫩一区二区三区四区公司1 | 国产乱码精品一区二区三区五月婷 | 国产免费自拍 | 四虎海外 | 日韩欧美国产精品一区二区 | 国内精品视频一区二区三区 | 在线免费观看毛片 | 久久成人免费 | 国产一区成人 | 麻豆成人在线视频 | 天天拍天天插 | 综合色在线 | www.99re5.com | 日韩免费av| 国产二区精品视频 | 色网站在线 | 91香蕉嫩草 | 性视频一区 | 精品一二区 | 免费看黄色小视频 | 国产成人91视频 | 成人欧美一区二区三区在线观看 | 日韩一级免费电影 | 日韩一区二区三区精品 | sese视频在线观看 | 欧美日韩中文国产一区发布 | 国产亚洲精品美女久久久久久久久久 | 日日天天 | 日日干日日射 | 国产成人网 | 一区二区三区视频在线观看 | 久久男女视频 | 九九热在线免费视频 | 国产欧美一区二区精品忘忧草 | 成在线人视频免费视频 | h视频在线免费 | 国产一区二区 |