如何使用Spring 框架實(shí)現(xiàn)注入和替換?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的路橋網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
首先配置 XML:
<bean id="author" class="net.deniro.spring4.bean.Author" scope="prototype"/> <bean id="book" class="net.deniro.spring4.bean.Book" p:name="面紗"> </bean>
bean author 的 scope 設(shè)置為 prototype。
Book 類實(shí)現(xiàn) BeanFactoryAware 接口:
public class Book implements BeanFactoryAware { ... @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.factory = beanFactory; } public Author getPrototypeAuthor() { return (Author) factory.getBean("author"); } }
單元測(cè)試:
ApplicationContext context; @BeforeMethod public void setUp() throws Exception { context = new ClassPathXmlApplicationContext("beans5-5.xml"); } @Test public void test(){ Book book= (Book) context.getBean("book"); System.out.println(book.getAuthor().hashCode()); System.out.println(book.getAuthor().hashCode()); System.out.println(book.getPrototypeAuthor().hashCode()); System.out.println(book.getPrototypeAuthor().hashCode());
測(cè)試結(jié)果
從結(jié)果中可以發(fā)現(xiàn),只有從 BeanFactory 中獲取得到的 Author 實(shí)例是不同的。
這種實(shí)現(xiàn)把應(yīng)用與 Spring 框架綁定在了一起,是否有更好的解決方案呢?有,就是注入方法。
1 注入方法
Spring 容器依賴于 CGLib 庫(kù),所以可以在運(yùn)行期動(dòng)態(tài)操作 Class 的字節(jié)碼,比如動(dòng)態(tài)地創(chuàng)建 Bean 的子類或?qū)崿F(xiàn)類。
BookInterface 接口:
public interface BookInterface { Author getAuthor(); }
XML 配置:
<!-- 方法注入--> <bean id="author" class="net.deniro.spring4.bean.Author" scope="prototype" p:name="毛姆" /> <bean id="book2" class="net.deniro.spring4.bean.BookInterface"> <lookup-method name="getAuthor" bean="author"/> </bean>
單元測(cè)試:
BookInterface book= (BookInterface) context.getBean("book2"); Assert.assertEquals("毛姆",book.getAuthor().getName()); Assert.assertTrue(book.getAuthor().hashCode()!=book.getAuthor().hashCode());
通過這種配置方式,就可以為接口提供動(dòng)態(tài)實(shí)現(xiàn)啦,而且這樣返回的 Bean 都是新的實(shí)例。
所以,如果希望在一個(gè) singleton Bean 中獲取一個(gè) prototype Bean 時(shí),就可以使用 lookup 來實(shí)現(xiàn)注入方法。
2 替換方法
在 Spring 中,可以使用某個(gè) Bean 的方法去替換另一個(gè) Bean 的方法。
假設(shè) Book 中有一個(gè) getName() 方法,用于獲取書名:
/** * 書名 */ private String name; public String getName() { return name; }
我們現(xiàn)在新建一個(gè) Bean,它實(shí)現(xiàn)了 MethodReplacer 接口,用于替換 Book 中的 getName() 方法:
public class Book4 implements MethodReplacer { @Override public Object reimplement(Object obj, Method method, Object[] args) throws Throwable { return "活著"; } }
配置:
<bean id="book3" class="net.deniro.spring4.bean.Book" p:name="燦爛千陽"> <replaced-method name="getName" replacer="book4"/> </bean> <bean id="book4" class="net.deniro.spring4.bean.Book4"/>
測(cè)試:
Book book= (Book) context.getBean("book3"); assertEquals("活著", book.getName());
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
分享標(biāo)題:如何使用Spring框架實(shí)現(xiàn)注入和替換
當(dāng)前路徑:http://www.yijiale78.com/article8/pdshop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、全網(wǎng)營(yíng)銷推廣、微信公眾號(hào)、關(guān)鍵詞優(yōu)化、網(wǎng)站營(yíng)銷、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)