1. Spring中bean的多種作用域
在默認情況下,Spring應用上下文中所有的bean都是以單例(singleton)的形式創建的,即不管給定的一個bean被注入到其他bean多少次,每次所注入的都是同一個實例。
Spring定義了多種作用域,可以基於這些作用域創建bean:
- 單例(Singleton):在整個應用中,只創建bean的一個實例。
- 原型(Prototype):每次注入或者通過Spring應用上下文獲取的時候,都會創建一個新的bean實例。
- 會話(Session):在Web應用中,為每個會話創建一個bean實例。
- 請求(Request):在Web應用中,為每個請求創建一個bean實例。
單例是默認的作用域,如果要使用其它的作用域創建bean,需要使用@Scope
注解,該注解可以和@Component
,@Service
,@Bean
等注解一起使用。
2. 示例
為了更好的理解,我們通過具體的代碼示例來理解下Singleton與ProtoType的區別。
2.1 新建Singleton類型的bean
package chapter03.scope;
import org.springframework.stereotype.Service;
@Service
public class DemoSingletonService {
}
因為Spring默認配置的Scope是Singleton,因此以上代碼等價於以下代碼:
package chapter03.scope;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class DemoSingletonService {
}
上面的代碼也可以寫成@Scope("singleton")
。
2.2 新建ProtoType類型的bean
如果是自動裝配bean,語法為:
package chapter03.scope;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DemoPrototypeService {
}
上面的代碼也可以寫成@Scope("prototype")
。
如果是在Java配置類中聲明bean,語法為:
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public DemoPrototypeService demoPrototypeService() {
return new DemoPrototypeService();
}
如果是在xml中聲明bean,語法為:
<bean id="demoPrototypeService" class="chapter03.scope.DemoPrototypeService"
scope="prototype"/>
2.3 新建配置類
package chapter03.scope;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("chapter03.scope")
public class ScopeConfig {
}
2.4 測試
新建一個Main類,在main()方法中,分別從Spring容器中獲取2次Bean,然后判斷Bean的實例是否相等:
package chapter03.scope;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class);
DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
DemoSingletonService s2 = context.getBean(DemoSingletonService.class);
DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class);
System.out.println("s1 與 s2 是否相等:" + s1.equals(s2));
System.out.println("p1 與 p2 是否相等:" + p1.equals(p2));
context.close();
}
}
運行結果如下:
從運行結果也可以看出,默認情況下即Singleton類型的Bean,不管調用多少次,只會創建一個實例。
3. 注意事項
Singleton類型的Bean,@Scope注解的值必須是:singleton。
ProtoType類型的Bean,@Scope注解的值必須是:prototype。
即使是大小寫不一致也不行,如我們將DemoPrototypeService類的代碼修改為:
package chapter03.scope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("PROTOTYPE")
public class DemoPrototypeService {
}
此時運行代碼,就會報錯:
4. 源碼及參考
源碼地址:https://github.com/zwwhnly/spring-action.git,歡迎下載。
汪雲飛《Java EE開發的顛覆者:Spring Boot實戰》
原創不易,如果覺得文章能學到東西的話,歡迎點個贊、評個論、關個注,這是我堅持寫作的最大動力。
如果有興趣,歡迎添加我的微信:zwwhnly,等你來聊技術、職場、工作等話題(PS:我是一名奮斗在上海的程序員)。