Spring的實例化Bean有三種方式:
?使用類構造器直接實例化
?使用靜態工廠的方法實例化
?使用實例工廠方法實例化
?
三種方式對應的配置如下
- <? xml ? version = "1.0" ? encoding = "UTF-8" ?> ??
- < 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" ??
- ???????? xmlns:tx = "http://www.springframework.org/schema/tx" ??
- ???????? xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ??
- ????????????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-2.5.xsd ??
- ????????????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > ??
- ???????? <!--?使用類構造器直接實例化?--> ?? ??
- ???????? < bean ? id = "userBean1" ? class = "com.szy.spring.implbean.UserBean" ? /> ??
- ???????? <!--?使用靜態工廠的方法實例化?--> ??
- ???????? < bean ? id = "userBean2" ? class = "com.szy.spring.factory.BeanFactory" ? factory-method = "UserBeanService" ? /> ??
- ???????? <!--?使用實例工廠方法實例化?--> ??
- ???????? < bean ? id = "factory" ? class = "com.szy.spring.factory.BeanFactory" ? /> ??
- ???????? < bean ? id = "userBean3" ? factory-bean = "factory" ? factory-method = "getUserBeanService" ? /> ??
- </ beans > ??
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 使用類構造器直接實例化 --> <bean id="userBean1" class="com.szy.spring.implbean.UserBean" /> <!-- 使用靜態工廠的方法實例化 --> <bean id="userBean2" class="com.szy.spring.factory.BeanFactory" factory-method="UserBeanService" /> <!-- 使用實例工廠方法實例化 --> <bean id="factory" class="com.szy.spring.factory.BeanFactory" /> <bean id="userBean3" factory-bean="factory" factory-method="getUserBeanService" /> </beans>
?
?
其中BeanFactory類的代碼如下
- package ?com.szy.spring.factory; ??
- ??
- import ?com.szy.spring.implbean.UserBean; ??
- import ?com.szy.spring.interfacebean.PersonBean; ??
- ??
- public ? class ?BeanFactory ??
- { ??
- ???? //使用靜態工廠的方法實例化使用 ??
- ???? public ? static ?PersonBean?UserBeanService() ??
- ????{ ??
- ???????? return ? new ?UserBean(); ??
- ????} ??
- ???? ??
- ???? public ?PersonBean?getUserBeanService() ??
- ????{ ??
- ???????? return ? new ?UserBean(); ??
- ????} ??
- }??
package com.szy.spring.factory; import com.szy.spring.implbean.UserBean; import com.szy.spring.interfacebean.PersonBean; public class BeanFactory { //使用靜態工廠的方法實例化使用 public static PersonBean UserBeanService() { return new UserBean(); } public PersonBean getUserBeanService() { return new UserBean(); } }
?
在這三種方式中我們最常用的還是第一種。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
