无状态 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 哈哈~
来源: http://www.jianshu.com/p/c370d71432e0