说说在 Spring 中如何注入或替换方法

无状态 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 {
  ...
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.factory = beanFactory;
}

public Author getPrototypeAuthor() {
    return (Author) factory.getBean("author");
    }
}

单元测试:

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());
}
测试结果

从结果中可以发现,只有从 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 框架的这招乾坤大挪移是不是很厉害呀O(∩_∩)O哈哈~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,933评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,958评论 6 342
  • 什么是Spring Spring是一个开源的Java EE开发框架。Spring框架的核心功能可以应用在任何Jav...
    jemmm阅读 16,555评论 1 133
  • 一:沉稳 (1)不要随便显露你的情绪。 (2)不要逢人就诉说你的困难和遭遇。 (3)在征询别人的意见之前,自己先思...
    Robin_lian阅读 395评论 0 0
  • 我知道一定有什么 是超然的 比如堆在天空上面的天空 及其飘飞其上的 文字一样无序的茅草 我还知道一定有什么 是卓然...
    江西黄小军阅读 357评论 0 1