溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Spring 框架實(shí)現(xiàn)注入和替換

發(fā)布時(shí)間:2021-05-27 17:02:41 來(lái)源:億速云 閱讀:238 作者:Leah 欄目:編程語(yǔ)言

如何使用Spring 框架實(shí)現(xiàn)注入和替換?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

首先配置 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());

通過(guò)這種配置方式,就可以為接口提供動(dòng)態(tài)實(shí)現(xiàn)啦,而且這樣返回的 Bean 都是新的實(shí)例。

 所以,如果希望在一個(gè) singleton Bean 中獲取一個(gè) prototype Bean 時(shí),就可以使用 lookup 來(lái)實(shí)現(xiàn)注入方法。

2 替換方法

在 Spring 中,可以使用某個(gè) Bean 的方法去替換另一個(gè) Bean 的方法。

假設(shè) Book 中有一個(gè) getName() 方法,用于獲取書(shū)名:

/**
 * 書(shū)名
 */
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="燦爛千陽(yáng)">
  <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)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI