當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Bean作用域实例
生活随笔
收集整理的這篇文章主要介紹了
Spring Bean作用域实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Spring中,bean作用域用于確定哪種類型的 bean 實例應該從Spring容器中返回給調用者。bean支持的5種范圍域: 單例?-?每個Spring?IoC 容器返回一個bean實例 原型-?當每次請求時返回一個新的bean實例 請求?-?返回每個HTTP請求的一個Bean實例 會話?-?返回每個HTTP會話的一個bean實例 全局會話-?返回全局HTTP會話的一個bean實例 在大多數情況下,可能只處理了 Spring 的核心作用域?-?單例和原型,默認作用域是單例。 注:意味著只有在一個基于web的Spring?ApplicationContext情形下有效! 單例VS原型 這里有一個例子來說明,bean的作用域單例和原型之間的不同: package com.yiibai.customer.services;public class CustomerService
{String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
1.單例例子 如果 bean 配置文件中沒有指定 bean 的范圍,默認為單例。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="customerService" class="com.yiibai.customer.services.CustomerService" /></beans>
執行結果:
package com.yiibai.common;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.customer.services.CustomerService;public class App {public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});CustomerService custA = (CustomerService)context.getBean("customerService");custA.setMessage("Message by custA");System.out.println("Message : " + custA.getMessage());//retrieve it againCustomerService custB = (CustomerService)context.getBean("customerService");System.out.println("Message : " + custB.getMessage());} }輸出結果
Message : Message by custA Message : Message by custA?由于?bean?的?“CustomerService'?是單例作用域,第二個通過提取”custB“將顯示消息由?”custA'?設置,即使它是由一個新的?getBean()方法來提取。在單例中,每個Spring?IoC容器只有一個實例,無論多少次調用 getBean()方法獲取它,它總是返回同一個實例。
2.原型例子 如果想有一個新的 “CustomerService”bean 實例,每次調用它的時候,需要使用原型(prototype)來代替。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="customerService" class="com.yiibai.customer.services.CustomerService" scope="prototype"/></beans>運行-執行
Message : Message by custA Message : null 在原型作用域,必須為每個 getBean()方法中調用返回一個新的實例。 3.?Bean作用域注釋 還可以使用注釋來定義 bean 的作用域。 package com.yiibai.customer.services;import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service;@Service @Scope("prototype") public class CustomerService {String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;} } 啟用自動組件掃描 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.yiibai.customer" /></beans> 下載代碼 –?http://pan.baidu.com/s/1o7jxMrg http://www.yiibai.com/spring/spring-bean-scopes-examples.html總結
以上是生活随笔為你收集整理的Spring Bean作用域实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 祛湿气的中药配方泡脚(祛湿气的中药配方)
- 下一篇: local(loca)