在上一篇Spring系列—基础篇之依赖注入中讲解了Spring中常用的两种注入方式,即构造方法注入和Setter方法注入,在本篇文章中介绍一种在项目中很可能会用到的另一种注入方式 Method Injection(即方法注入)。
在很多应用场景中,在Spring容器中的bean都是单例的,当一个单例的bean A 组合另一个非单例的bean B时,如果通过构造方式或者Setter方法注入时,单例的bean A 就会缓存了第一次注入的非单例的bean B,但是每次使用Bean A时都不会再创建,所以bean A 种组合的Bean B 也就不会改变,从而每次使用的都是同一个Bean A。
一种处理方式就是Bean A 实现 ApplicationContextAware 接口,在Bean中使用bean B 时,通过调用 applicationContext.getBean()的方式从Spring容器中获取一个新的Bean B 实例,如下图所示:
package org.spring.stud.po;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.util.Map;
public class CommandManager implements ApplicationContextAware {
private ApplicationContext applicationContext;
public Object process(Map<String, Object> params){
Command command = createCommand();
command.setStateMap(params);
return command.execute();
}
/**
* 每次调动都从Spring容器中获取一个新的Command实例
* @return
*/
protected Command createCommand(){
return this.applicationContext.getBean("command", Command.class);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
这种方式简单,但是次方式对具有侵入,即代码中侵入了Spring的代码。除了此种方式Spring中还提供了一种注入方式即上面提到的Method Injection —— 方法注入方式
Spring提供了两种Method Injection方式,一种为 Lookup Method Injection,另一种为Arbitrary Method Replacement方式。
一、Lookup Method Injection
Lookup Method Injection 的实现方式是Spring通过CGLIB动态生成一个子类,来继承覆盖父类中的方法,由于是继承所用父类中的方法可以是抽象方法,也可以为非抽象方法,但是一定不能为final修饰的方法。如下所示:分别通过 XML配置和注解两种方式实现。
1、XML方式实现
package org.spring.stud.po;
import java.util.Map;
public abstract class LookupMethodInjection {
public Object process(Map<String, Object> params){
Command command = this.createCommand();
command.setStateMap(params);
return command.execute();
}
public abstract Command createCommand();
}
XML的配置如下:
<!-- 配置根据createCommand方法返回值类或者起子类型 -->
<bean id="myCommand" class="org.spring.stud.po.MyCommand" scope="prototype" />
<bean id="lookupMethodInjection" class="org.spring.stud.po.LookupMethodInjection">
<!-- 通过lookup-method标签指定要覆盖的方法返回的类型 -->
<lookup-method name="createCommand" bean="myCommand" />
</bean>
2、注解方式 即把@Lookup注解添加到需要改变返回值的方法上,如下图所示:
package org.spring.stud.po;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public abstract class LookupMethodInjection {
public Object process(Map<String, Object> params){
Command command = this.createCommand();
command.setStateMap(params);
System.out.println("通过lookup method injection 方式注入的实例: " + command);
return command.execute();
}
@Lookup("myCommand") //通知注解方式
public abstract Command createCommand();
}
其中createCommand()方法必须满足一下定义格式
<public|protected> [abstract] <return-type> theMethodName(no-arguments);
二、Arbitrary Method Replacement 方式
此种方法与lookup method injection 方法相比是不太使用的一种方法,他是通过替换已有方法的形式实现的,此种方法只支持XML配置方式。其实现方式如下所示:
package org.spring.stud.po;
public class ArbitraryMethodReplacement {
private Command command;
public Object process(String state){
command = this.getCommand();
System.out.println("通过 Arbitrary method replacement 方式实现: " + command);
return command.execute();
}
public Command getCommand() {
return command;
}
public void setCommand(Command command) {
this.command = command;
}
}
package org.spring.stud.po;
import org.springframework.beans.factory.support.MethodReplacer;
import java.lang.reflect.Method;
public class ReplacementMethod implements MethodReplacer {
@Override
public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
return new Command();
}
}
<bean id="replacementMethod" class="org.spring.stud.po.ReplacementMethod"></bean>
<bean id="methodReplacement" class="org.spring.stud.po.ArbitraryMethodReplacement" >
<replaced-method name="getCommand" replacer="replacementMethod" />
</bean>
说明:此种方法一般不常用,因为此种方法需要实现spring的 org.springframework.beans.factory.support.MethodReplacer 接口。如果子一个类中需要替换的方法有多个重载方法时可以通过 <replace-method>标签的子元素<arg-type>通过指定具体要替换那个方法。