Spring特殊注入lookup-method、replaced-method

1、lookup-method注入

lookup method注入是spring动态改变bean里方法的实现。方法执行返回的对象,使用spring内原有的这类对象替换,通过改变方法返回值来动态改变方法。内部实现为使用cglib方法,重新生成子类,重写配置的方法和返回对象,达到动态改变的效果。 

 
     实例如下:
复制代码
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(); }
复制代码

 

配置:
复制代码
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
</bean>
 
<bean id="commandManager" class="fiona.apple.CommandManager">
    <lookup-method name="createCommand" bean="command"/>
</bean>
复制代码

 

注意:由于采用cglib生成之类的方式,所以需要用来动态注入的类,不能是final修饰的;需要动态注入的方法,也不能是final修饰的。
 
同时,还得注意command的scope的配置,如果scope配置为singleton,则每次调用方法createCommand,返回的对象都是相同的;如果scope配置为prototype,则每次调用,返回都不同。
 
 
2、replaced-method注入
     replaced method注入是spring动态改变bean里方法的实现。需要改变的方法,使用spring内原有其他类(需要继承接口org.springframework.beans.factory.support.MethodReplacer)的逻辑,替换这个方法。通过改变方法执行逻辑来动态改变方法。内部实现为使用cglib方法,重新生成子类,重写配置的方法和返回对象,达到动态改变的效果。
 
     实例如下:
复制代码
public class MyValueCalculator {
 
    public String computeValue(String input) {
        // some real code...  } // some other methods...  } /** * meant to be used to override the existing computeValue(String) * implementation in MyValueCalculator */ public class ReplacementComputeValue implements MethodReplacer { public Object reimplement(Object o, Method m, Object[] args) throws Throwable { // get the input value, work with it, and return a computed result String input = (String) args[0]; ... return ...; } }
复制代码
 
配置:
复制代码
<bean id="myValueCalculator" class="x.y.z.MyValueCalculator">
    <!-- arbitrary method replacement -->
    <replaced-method name="computeValue" replacer="replacementComputeValue">
        <arg-type>String</arg-type>
    </replaced-method>
</bean>
 
<bean id="replacementComputeValue" class="a.b.c.ReplacementComputeValue"/>
复制代码

 

 
注意:由于采用cglib生成之类的方式,所以需要用来动态注入的类,不能是final修饰的;需要动态注入的方法,也不能是final修饰的。
 

转载于:https://www.cnblogs.com/atwanli/articles/6154920.html

当然,除了使用注解方式,我们还可以使用XML配置来实现Spring Lookup Method。下面是一个使用XML配置的示例: 首先,定义一个抽象类: ``` public abstract class AbstractBean { public void doSomething() { getDependency().execute(); } public abstract Dependency getDependency(); } ``` 接下来,定义一个具体的子类: ``` public class ConcreteBean extends AbstractBean { private Dependency dependency; @Override public Dependency getDependency() { if (dependency == null) { dependency = createDependency(); } return dependency; } protected Dependency createDependency() { // 返回一个新的Dependency对象 } } ``` 注意到这里的`createDependency()`方法没有被`@Lookup`注解标记,这是因为我们将会在XML中配置这个方法。 接下来,我们需要在XML中配置这个类: ``` <bean id="concreteBean" class="com.example.ConcreteBean"> <lookup-method name="createDependency" bean="dependency"/> </bean> <bean id="dependency" class="com.example.Dependency"/> ``` 注意到这里的`lookup-method`元素,它告诉Spring在运行时为`createDependency()`方法生成一个代理,并将其返回值注入到`ConcreteBean`对象中。 现在,我们可以在其他类中注入`concreteBean`对象,并调用它的`doSomething()`方法,它将会使用`ConcreteBean`中的`createDependency()`方法来获取一个新的`Dependency`对象,并执行它的`execute()`方法。 这就是使用XML配置的Spring Lookup Method的一个简单示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值