spring lookup method 注入
2016-08-07 11:15
295 查看
lookup method注入是spring动态改变bean里方法的实现。方法执行返回的对象,使用spring内原有的这类对象替换,通过改变方法返回值来动态改变方法。内部实现为使用cglib方法,重新生成子类,重写配置的方法和返回对象,达到动态改变的效果。
实例如下:
配置:
注意:由于采用cglib生成之类的方式,所以需要用来动态注入的类,不能是final修饰的;需要动态注入的方法,也不能是final修饰的。
同时,还得注意command的scope的配置,如果scope配置为singleton,则每次调用方法createCommand,返回的对象都是相同的;如果scope配置为prototype,则每次调用,返回都不同。
实例如下:
package fiona.apple; // no more Spring imports! public abstract class CommandManager { public Object process(Object commandState) { // grab a new instance of the appropriate Command interface Command command = createCommand(); // set the state on the (hopefully brand new) Command instance command.setState(commandState); return command.execute(); } // okay... but where is the implementation of this method? protected abstract Command createCommand(); }
配置:
<!-- a stateful bean deployed as a prototype (non-singleton) --> <bean id="command" class="fiona.apple.AsyncCommand" scope="prototype"> <!-- inject dependencies here as required --> </bean> <!-- commandProcessor uses statefulCommandHelper --> <bean id="commandManager" class="fiona.apple.CommandManager"> <lookup-method name="createCommand" bean="command"/> </bean>
注意:由于采用cglib生成之类的方式,所以需要用来动态注入的类,不能是final修饰的;需要动态注入的方法,也不能是final修饰的。
同时,还得注意command的scope的配置,如果scope配置为singleton,则每次调用方法createCommand,返回的对象都是相同的;如果scope配置为prototype,则每次调用,返回都不同。
相关文章推荐
- Java异常处理和设计
- Struts2与Struts的区别
- Java 异常处理的误区和经验总结
- javase 双色球选7球 红球在【1-36】之间,选出6个,且不能重复 篮球在【1-16】之间,选出1个
- Java之多线程
- Java Web Junit测试
- Java内存泄露的问题调查定位
- Java题库
- 深入理解java异常处理机制
- 遗传算法组卷使用心得
- Java的文件读写操作
- java基本排序之简单选择排序
- JAVA的文件操作
- Java文件操作大全(绝对的经典,值得收藏!)
- 怎么在eclipse中安装properties插件
- 【Eclipse】Eclipse Debug技巧详解
- Spring的事务传播性与隔离级别
- java-IO小记
- 自学java(5)
- Java批量更改文件名