spring 方法注入之 方法以替换
2017-04-05 12:41
323 查看
在spring Ioc 容器里,允许用一个Bean 的方法去替换另一个Bean 的方法。
public
class
ForestTwo
implements
MethodReplacer {
public
ForestTwo() {
[align=left] super();[/align]
[align=left] }[/align]
public
Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws
Throwable {
Tree tt =
new
Tree();
[align=left] tt.setName("桂花");[/align]
return
tt;
[align=left] }[/align]
}
public
class
Forest {
private
Tree
tree;
public
Tree
getTree() {
Tree reTree =
new
Tree();
[align=left] reTree.setName("柳树");[/align]
return
reTree;
[align=left] }[/align]
public
void
setTree(Tree tree) {
this.tree
= tree;
[align=left] }[/align]
public
Forest() {
[align=left] super();[/align]
[align=left] }[/align]
public
Forest(Tree tree) {
[align=left] super();[/align]
this.tree
= tree;
[align=left] }[/align]
}
配置一
<bean
id
="forest"
class="com.aowin.modle.Forest"> </bean>
<bean
id
="forest2"
class="com.aowin.modle.ForestTwo"/>
[align=left]System.out.println(forest.getTree().getName());[/align]
输出的是---柳树
配置二
<bean
id
="forest"
class="com.aowin.modle.Forest">
<replaced-method
name="getTree"
replacer="forest2"></replaced-method>
[align=left] </bean>[/align]
[align=left] [/align]
<bean
id
="forest2"
class="com.aowin.modle.ForestTwo"/>
System.out.println(forest.getTree().getName());
输出的是 桂花。
用于替换他人的Bean必须实现MethodReplacer接口
因为forest2 实现了 MethodReplacer 接口,重写了该接口下的 reimplement()方法。
当 forest 调用getTree方法时,就会被forest2替换掉,用重写了的 reimplement()方法去替换。
public
class
ForestTwo
implements
MethodReplacer {
public
ForestTwo() {
[align=left] super();[/align]
[align=left] }[/align]
public
Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws
Throwable {
Tree tt =
new
Tree();
[align=left] tt.setName("桂花");[/align]
return
tt;
[align=left] }[/align]
}
public
class
Forest {
private
Tree
tree;
public
Tree
getTree() {
Tree reTree =
new
Tree();
[align=left] reTree.setName("柳树");[/align]
return
reTree;
[align=left] }[/align]
public
void
setTree(Tree tree) {
this.tree
= tree;
[align=left] }[/align]
public
Forest() {
[align=left] super();[/align]
[align=left] }[/align]
public
Forest(Tree tree) {
[align=left] super();[/align]
this.tree
= tree;
[align=left] }[/align]
}
配置一
<bean
id
="forest"
class="com.aowin.modle.Forest"> </bean>
<bean
id
="forest2"
class="com.aowin.modle.ForestTwo"/>
[align=left]System.out.println(forest.getTree().getName());[/align]
输出的是---柳树
配置二
<bean
id
="forest"
class="com.aowin.modle.Forest">
<replaced-method
name="getTree"
replacer="forest2"></replaced-method>
[align=left] </bean>[/align]
[align=left] [/align]
<bean
id
="forest2"
class="com.aowin.modle.ForestTwo"/>
System.out.println(forest.getTree().getName());
输出的是 桂花。
用于替换他人的Bean必须实现MethodReplacer接口
因为forest2 实现了 MethodReplacer 接口,重写了该接口下的 reimplement()方法。
当 forest 调用getTree方法时,就会被forest2替换掉,用重写了的 reimplement()方法去替换。
相关文章推荐
- 【Spring实战】基本方法替换和获取器方法注入
- Spring 框架中注入或替换方法实现
- Spring(08)——方法注入之替换方法实现
- SPRING.NET 1.3.2 学习20--方法注入之替换方法注入
- JSP开发之Spring方法注入之替换方法实现
- 白话Spring(基础篇)---方法注入与方法替换
- Spring-方法注入lookup、方法替换MethodReplacer接口
- Spring学习----------Bean 的方法注入和方法替换介绍
- 在Spring中使用replaced-method来进行方法替换
- 使用Spring方法注入协调不同步的bean
- 快速上手Spring--9. Lookup方法注入
- spring声明式事务配置方法(四):BeanNameAutoProxyCreator注入Bean
- 在Spring中使用replaced-method来进行方法替换
- 在Spring中使用replaced-method来进行方法替换
- Spring2.5中使用方法注入(method inject)之基本方法替代(basis method replacement)
- 在Spring中使用replaced-method来进行方法替换
- 在Spring中使用replaced-method来进行方法替换
- 在Spring中使用replaced-method来进行方法替换
- 在Spring中使用replaced-method来进行方法替换
- 快速上手Spring--10. 任意方法的替换