虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > Java编程 > Spring 框架中注入或替换方法实现

Spring 框架中注入或替换方法实现
类别:Java编程   作者:码皇   来源:互联网   点击:

这篇文章主要介绍了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 {
    ...@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 框架中注入或替换方法实现,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

您可能感兴趣的文章:

  • Spring注入Date类型的三种方法总结
  • spring+springmvc整合mabytis时mapper注入失败问题解决方法
  • JSP开发之Spring方法注入之替换方法实现
  • 因Spring AOP导致@Autowired依赖注入失败的解决方法
  • springboot注入servlet的方法
  • spring hibernate实现动态替换表名(分表)的方法
相关热词搜索: spring 方法注入 spring 方法替换