依賴注入的概念在上一篇文章中有介紹
關於bean的名稱,這裡有條預設的規則,就是類的名稱,然後第一個字母小寫
依賴注入 XML方式
通過setter方法注入依賴
<bean>元素的< property >子項目指明了使用它們的set方法來注入。可以注入任何東西,從基本類型到集合類,甚至是應用系統的bean。
- 配置bean的簡單屬性,基礎資料型別 (Elementary Data Type)和String。
<bean id="personService" class="com.test.bean.impl.PersonServiceImpl"> <!-- 基本類型,string類型 --> <propertyname="age"value="20"></property> <propertyname="name" value="張無忌"></property> </bean>
<bean id="person" class="com.test.bean.Person" /> <bean id="personService" class="com.test.bean.impl.PersonServiceImpl"> <!-- 參考型別 --> <propertyname="person" ref="person" /> </bean>
<bean id="personService"class="com.test.bean.impl.PersonServiceImpl"> <!-- 內部bean注入 --> <property name="personClass"><bean class="com.test.bean.PersonClass" /></propert> </bean>
這種方式的缺點是你無法在其它地方重用這個personClass執行個體,原因是它是專門為personService而用。
<!-- 裝配list --><property name="lists"><list><value>list1</value></list></property><!--裝配set --><property name="sets"><set><value>set1</value></set></property><!-- 裝配map --><property name="maps"><map><entry key="key1" value="v1" /></map></property><!--裝配Properties --><property name="props"><props><prop key="01">prop1</prop></props></property><!--裝配null --><property name="listnull"><null /></property><!--通過參數的類型 --><constructor-arg><value>56</value></constructor-arg>
spring 範圍
scope用來配置spring bean的範圍,它標識bean的範圍。
singleton(單例)、prototype,session、request、global session
1、singleton範圍
當一個bean的範圍設定為singleton, 那麼Spring IOC容器中只會存在一個共用的bean執行個體,並且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一執行個體。換言之,當把一個bean定義設定為singleton範圍時,Spring IOC容器只會建立該bean定義的唯一執行個體。這個單一執行個體會被儲存到單例緩衝(singleton
cache)中,並且所有針對該bean的後續請求和引用都將返回被緩衝的對象執行個體,這裡要注意的是singleton範圍和GOF設計模式中的單例是完全不同的,單例設計模式表示一個ClassLoader中只有一個class存在,而這裡的singleton則表示一個容器對應一個bean,也就是說當一個bean被標識為singleton時候,spring的IOC容器中只會存在一個該bean。
配置執行個體:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="singleton"/>
或者
<bean id="role" class="spring.chapter2.maryGame.Role" singleton="true"/>
2、prototype
prototype範圍部署的bean,每一次請求(將其注入到另一個bean中,或者以程式的方式調用容器的getBean()方法)都會產生一個新的bean執行個體,相當與一個new的操作,對於prototype範圍的bean,有一點非常重要,那就是Spring不能對一個prototype bean的整個生命週期負責,容器在初始化、配置、裝飾或者是裝配完一個prototype執行個體後,將它交給用戶端,隨後就對該prototype執行個體不聞不問了。不管何種範圍,容器都會調用所有對象的初始化生命週期回調方法,而對prototype而言,任何配置好的析構生命週期回調方法都將不會被調用。清除prototype範圍的對象並釋放任何prototype
bean所持有的昂貴資源,都是用戶端代碼的職責。(讓Spring容器釋放被singleton範圍bean佔用資源的一種可行方式是,通過使用bean的後置處理器,該處理器持有要被清除的bean的引用。)
配置執行個體:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="prototype"/>
或者
<beanid="role" class="spring.chapter2.maryGame.Role" singleton="false"/>
3、request
request表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP request內有效,配置執行個體:
request、session、global session使用的時候首先要在初始化web的web.xml中做如下配置:
如果你使用的是Servlet 2.4及以上的web容器,那麼你僅需要在web應用的XML聲明檔案web.xml中增加下述ContextListener即可:
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
接著既可以配置bean的範圍了:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="request"/>
4、session
session範圍表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP session內有效,配置執行個體
配置執行個體:
和request配置執行個體的前提一樣,配置好web開機檔案就可以如下配置:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="session"/>
5、global session
global session範圍類似於標準的HTTP Session範圍,不過它僅僅在基於portlet的web應用中才有意義。Portlet規範定義了全域Session的概念,它被所有構成某個portlet web應用的各種不同的portlet所共用。在global session範圍中定義的bean被限定於全域portlet
Session的生命週期範圍內。如果你在web中使用global session範圍來標識bean,那麼web會自動當成session類型來使用。
配置執行個體:
和request配置執行個體的前提一樣,配置好web開機檔案就可以如下配置:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="global session"/>
6、自訂bean裝配範圍
範圍是可以任意擴充的,你可以自訂範圍,甚至你也可以重新定義已有的範圍(但是你不能覆蓋singleton和prototype),spring的範圍由介面org.springframework.beans.factory.config.Scope來定義,自訂自己的範圍只要實現該介面即可,下面給個執行個體:
我們建立一個線程的scope,該scope在表示一個線程中有效,代碼如下:
publicclass MyScope implements Scope {
private final ThreadLocal threadScope = new ThreadLocal() {
protected Object initialValue() {
returnnew HashMap();
}
};
public Object get(String name, ObjectFactory objectFactory) {
Map scope = (Map) threadScope.get();
Object object = scope.get(name);
if(object==null) {
object = objectFactory.getObject();
scope.put(name, object);
}
return object;
}
public Object remove(String name) {
Map scope = (Map) threadScope.get();
return scope.remove(name);
}
public void registerDestructionCallback(String name, Runnable callback) {
}
public String getConversationId() {
return null;
}
}
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
StudentDaoImpl stu=(StudentDaoImpl) ctx.getBean("studentDao");