無狀態 Bean 的作用域是 singleton 單實例,如果我們向 singleton 的 Bean A 注入 prototype 的 Bean B,并希望每次調用 Bean A 的 getBeanB() 時都能返回一個新的 Bean B ,這樣的要求使用傳統的注入方式是無法實現的 。 因為 singleton 的 Bean 注入關聯 Bean 的動作只發生一次,雖然 Bean B 的作用域是 prototype 類型,但通過 getBeanB() 返回的對象還是最開始注入的那個 bean B。
所以如果希望每次調用 BeanA 的 getBeanB() 時都能返回一個新的 BeanB 的一種可選的方案是:讓 Bean A 實現 BeanFactoryAware 接口,從而能夠訪問容器,然后以下面這種方式來實現。
首先配置 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 設置為 prototype。
Book 類實現 BeanFactoryAware 接口:
public class Book implements BeanFactoryAware { ...@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.factory = beanFactory;}public Author getPrototypeAuthor() { return (Author) factory.getBean("author"); }}
單元測試:
ApplicationContext context;@BeforeMethodpublic void setUp() throws Exception { context = new ClassPathXmlApplicationContext("beans5-5.xml");}@Testpublic 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());
測試結果
從結果中可以發現,只有從 BeanFactory 中獲取得到的 Author 實例是不同的。
這種實現把應用與 Spring 框架綁定在了一起,是否有更好的解決方案呢?有,就是注入方法。
1 注入方法
Spring 容器依賴于 CGLib 庫,所以可以在運行期動態操作 Class 的字節碼,比如動態地創建 Bean 的子類或實現類。
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>
單元測試:
BookInterface book= (BookInterface) context.getBean("book2");Assert.assertEquals("毛姆",book.getAuthor().getName());Assert.assertTrue(book.getAuthor().hashCode()!=book.getAuthor().hashCode());
通過這種配置方式,就可以為接口提供動態實現啦,而且這樣返回的 Bean 都是新的實例。
所以,如果希望在一個 singleton Bean 中獲取一個 prototype Bean 時,就可以使用 lookup 來實現注入方法。
2 替換方法
在 Spring 中,可以使用某個 Bean 的方法去替換另一個 Bean 的方法。
假設 Book 中有一個 getName() 方法,用于獲取書名:
/** * 書名 */private String name;public String getName() { return name;}
我們現在新建一個 Bean,它實現了 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"/>
測試:
Book book= (Book) context.getBean("book3");assertEquals("活著", book.getName());
總結
以上所述是小編給大家介紹的Spring 框架中注入或替換方法實現,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
新聞熱點
疑難解答
圖片精選