javascript
Spring的依赖注入和管理Bean
采用Spring管理Bean和依賴注入
1.實例化spring容器 和 從容器獲取Bean對象
實例化Spring容器常用的兩種方式:
方法一:
在類路徑下尋找配置文件來實例化容器 [推薦使用]
ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:/META-INF/spring/applicationContext-memcached.xml");
?MemCachedManager cache = appContext.getBean(MemCachedManager.class);
方法二:
在文件系統路徑下尋找配置文件來實例化容器 [這種方式可以在開發階段使用]
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});
Spring的配置文件可以指定多個,可以通過String數組傳入。
?
當spring容器啟動后,因為spring容器可以管理bean對象的創建,銷毀等生命周期,
所以我們只需從容器直接獲取Bean對象就行,而不用編寫一句代碼來創建bean對象。
從容器獲取bean對象的代碼如下:
ApplicationContext ctx = new ClassPathXmlApplicationContext(“beans.xml”);
OrderService service = (OrderService)ctx.getBean("personService");
?
2.Spring實例化Bean的三種方式
以下是三種方式的例子:
1.使用類構造器實例化??[默認的類構造器]<bean?id=“orderService"?class="cn.itcast.OrderServiceBean"/>2.使用靜態工廠方法實例化<bean?id="personService"?class="cn.itcast.service.OrderFactory"?factory-method="createOrder"/> public?class?OrderFactory?{public?static?OrderServiceBean?createOrder(){???//?注意這里的這個方法是?static?的!return?new?OrderServiceBean();} }3.使用實例工廠方法實例化:<bean?id="personServiceFactory"?class="cn.itcast.service.OrderFactory"/> <bean?id="personService"?factory-bean="personServiceFactory"?factory-method="createOrder"/>public?class?OrderFactory?{????public?OrderServiceBean?createOrder(){????????return?new?OrderServiceBean();} }.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
?
3.Bean的生命周期 (Bean的作用域)
bean的scope 屬性
The scope of this bean: typically "singleton" (one shared instance, which will be returned by all calls?
to getBean with the given id), or "prototype" (independent instance resulting from each call to?
getBean).?Default is "singleton". Singletons are most commonly used, and are ideal for multi-?
threaded service objects.?Further scopes, such as "request" or "session", might be supported by?
extended bean factories (e.g. in a web environment).?Note: This attribute will not be inherited by?
child bean definitions.?Hence, it needs to be specified per concrete bean definition. Inner bean?
definitions inherit the singleton status of their containing bean definition, unless explicitly specified:?
The inner bean will be a singleton if the containing bean is a singleton, and a prototype if the?
containing bean has any other scope.
?
.singleton??[單例]?
eg:<bean id="personService" class="com.yinger.service.impl.PersonServiceBean" scope="singleton"></bean>
在每個Spring IoC容器中一個bean定義只有一個對象實例。
請注意Spring的singleton bean概念與“四人幫”(GoF)模式一書中定義的Singleton模式是完全不同的。
經典的GoF Singleton模式中所謂的對象范圍是指在每一個ClassLoader中指定class創建的實例有且僅有一個。
把Spring的singleton作用域描述成一個container對應一個bean實例最為貼切。亦即,假如在單個Spring容器內定義了某個指定class的bean,
那么Spring容器將會創建一個且僅有一個由該bean定義指定的類實例。
?
默認情況下會在容器啟動時初始化bean,但我們可以指定Bean節點的lazy-init=“true”來延遲初始化bean,這時候,只有第一次獲取bean會才初始化bean。
如:<bean id="xxx" class="cn.itcast.OrderServiceBean"?lazy-init="true"/>
如果想對所有bean都應用延遲初始化,可以在根節點beans設置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
.prototype?[原型]
每次從容器獲取bean都是新的對象。
對于prototype作用域的bean,有一點非常重要,那就是Spring不能對一個prototype bean的整個生命周期負責:容器在初始化、配置、裝飾或者是
裝配完一個prototype實例后,將它交給客戶端,隨后就對該prototype實例不聞不問了。不管何種作用域,容器都會調用所有對象的初始化生命周期回調方法。
但對prototype而言,任何配置好的析構生命周期回調方法都將不會被調用。清除prototype作用域的對象并釋放任何prototype bean所持有的昂貴資源,
都是客戶端代碼的職責。(讓Spring容器釋放被prototype作用域bean占用資源的一種可行方式是,通過使用bean的后置處理器,該處理器持有要被清除的bean的引用。)
以下的三種scope只是在web應用中才可以使用
.request
.session
.global session
使用這三種配置之前要先初始化Web配置
?
?
5.xml配置方法和參數
? ?
? ? <context:component-scan base-package="com.ibs.gbplarform.common.memcached" />
? ??
<bean id="memCachedPool" class="com.whalin.memcached.SockIOPool"
factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>127.0.0.1:11211</value>
</list>
</property>
<property name="initConn">
<value>20</value>
</property>
?
<property name="minConn">
<value>10</value>
</property>
?
<property name="maxConn">
<value>50</value>
</property>
?
<property name="maintSleep">
<value>3000</value>
</property>
?
<property name="nagle">
<value>false</value>
</property>
?
<property name="socketTO">
<value>3000</value>
</property>
</bean>
?
<bean id="memCachedClient" class="com.whalin.memcached.MemCachedClient">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
</bean>
?
?
轉載于:https://www.cnblogs.com/hackerxian/p/10871710.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Spring的依赖注入和管理Bean的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java.io.CharConversi
- 下一篇: 那些年借“云”出海的日子