1. lookup-method的应用:

1.1 子元素lookup-method 似乎不是很常用,但是在某些时候他的确是非常有用的属性,通常我们称它为 "获取器注入" .

  引用 "Spring In Action " 中的一句话.

  '获取器注入是一种特殊的方法注入,它是把一个方法声明为返回某种类型的bean,但实际上,返回的bean是配置文件里面配置的,此方法可用在设计一些可插拔的功能上,解除程序依赖'

1.2 我们来看看具体的应用:

  1.2.1 首先我们创建一个父类,

 public class Person {

     public void showMe() {
System.out.println("I am person ,what about you ?");
}
}

  1.2.2 创建其子类,并覆盖其showMe()方法,

 public class Theacher extends Person {

     /*
* (non-Javadoc)
*
* @see test.lookup.method.entity.Person#showMe()
*/
@Override
public void showMe() {
System.out.println("I am a theacher,");
}
}

  1.2.3 创建调用方法

 public abstract class GetBean {

     private void showMe() {
this.getBean().showMe();
} public abstract Person getBean(); }

  1.2.4 创建测试类

 public class Main {

     public static String XML_PATH = "test\\lookup\\method\\entity\\applicationContxt.xml";

     public static void main(String[] args) {
try {
Resource resource = new ClassPathResource(XML_PATH);
XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
GetBean bean = (GetBean) beanFactory.getBean("getBean");
System.out.println(bean);
bean.getBean().showMe();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

  1.2.5 配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="getBean" class="test.lookup.method.entity.GetBean">
<lookup-method name="getBean" bean="teacher"/>
</bean> <bean id="teacher" class="test.lookup.method.entity.Theacher">
</bean>
<bean id="person" class="test.lookup.method.entity.Person">
</bean> </beans>

在配置文件中,我们看到了  lookup-method  子元素, 这个配置完成的功能就是动态地将teacher所代表的bean作为getBean的返回值,那么当我们的业务需要变更的或者需要替换的情况下我们只需要修改配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="getBean" class="test.lookup.method.entity.GetBean">
<lookup-method name="getBean" bean="person"/>
</bean> <bean id="teacher" class="test.lookup.method.entity.Theacher">
</bean>
<bean id="person" class="test.lookup.method.entity.Person">
</bean> </beans>

 至此,我们已经初步了解了lookup-method的作用,这是我们去看看Spring 的源码;

 /**
* Parse lookup-override sub-elements of the given bean element.
*/
public void parseLookupOverrideSubElements(Element beanEle, MethodOverrides overrides) {
NodeList nl = beanEle.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
//仅当在Spring默认Bean的子元素下,
//且为<lookup-method 时有效
if (isCandidateElement(node) && nodeNameEquals(node, LOOKUP_METHOD_ELEMENT)) {
Element ele = (Element) node;
// 获取要修饰的方法
String methodName = ele.getAttribute(NAME_ATTRIBUTE);
// 获取配置返回的Bean
String beanRef = ele.getAttribute(BEAN_ELEMENT);
LookupOverride override = new LookupOverride(methodName, beanRef);
override.setSource(extractSource(ele));
overrides.addOverride(override);
}
}
}

上面的代码很熟悉,似乎与parseMetaElement的代码大同小异,

最大的区别就是在数据存储上面使用LookupOverride 类型的实体来进行数据承载,并记录在AbstractBeanDefinition中的methodOveride 属性中.

04-27 07:24