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 属性中.

3.4 spring- lookup-method 子元素的使用与解析的更多相关文章

  1. spring lookup method 注入

           lookup method注入是spring动态改变bean里方法的实现.方法执行返回的对象,使用spring内原有的这类对象替换,通过改变方法返回值来动态改变方法.内部实现为使用cgl ...

  2. 3.5 spring-replaced-method 子元素的使用与解析

    1.replaced-method 子元素 方法替换: 可以在运行时用新的方法替换现有的方法,与之前的 look-up不同的是replace-method 不但可以动态地替换返回的实体bean,而且可 ...

  3. 3.8 spring-qualifier 子元素的使用与解析

      对于 qualifier 子元素,我们接触的更多的是注解形式,在使用Spring 自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean 时, S ...

  4. 3.7 spring-property 子元素的使用与解析

    1.0 Property子元素的使用 property 子元素是再常用不过的了, 在看Spring源码之前,我们先看看它的使用方法, 1. 实例类如下: public class Animal { p ...

  5. 3.6 spring-construction-arg 子元素的使用与解析

    对于构造函数子元素是非常常用的. 相信大家也一定不陌生, 举个小例子: public class Animal { public String type; public int age; /** * ...

  6. 3.3 spring-meta子元素的使用与解析

    1. meta元素的使用 在解析元数据的分析之前,我们先回顾一下 meta属性的使用: <bean id="car" class="test.CarFactoryB ...

  7. spring源码学习之默认标签的解析(一)

    继续spring源码的学习之路,现在越来越觉得这个真的很枯燥的,而且我觉得要是自己来看源码,真的看不下去,不是没有耐心,而是真的没有头绪,我觉得结合着书来看,还是很有必要的,最起码大致的流程是能够捋清 ...

  8. 1.spring.net Look-up Method 查找方法的注入(方法是抽象的需要spring.net注入)

    .为什么需要查找方法的注入 当Object依赖另一个生命周期不同的Object,尤其是当singleton依赖一个non-singleton时,常会遇到不少问题,Lookup Method Injec ...

  9. spring bean属性及子元素使用总结

    spring bean属性及子元素使用总结 2016-08-03 00:00 97人阅读 评论(0) 收藏 举报  分类: Spring&SpringMVC(17)  版权声明:本文为博主原创 ...

随机推荐

  1. oracle数据库创建表空间和表临时空间

    1:创建临时表空间 create temporary tablespace user_temp tempfile 'Q:\oracle\product\10.2.0\oradata\Test\xyrj ...

  2. 老男孩-金角大王-python学习博客地址

    http://www.cnblogs.com/alex3714/category/770733.html

  3. Red Hat Enterprise Linux 5安装序列号

    为了保证安装的组件和订阅相匹配,红帽企业 Linux 5 需要输入一个安装号.它被用来配置安装程序来提供正确的软件包.安装号码包含在你的订阅里. 如果您没有输入安装号码,只有核心服务器或 Deskto ...

  4. autolayout高度动态改变的一些体会

    autolayout这个东西就不在此说明了,网上已经有很多大神做了很详细的讲解,自己也看了不少好文章,在这里只是想记录一下自己初步的一些认识与体会,这个东西毕竟还是很强大,如果要用到更高级的用法还得在 ...

  5. 接口(interface)

    接口(interface) 接口(interface)定义了一个可由类和结构实现的协定.接口可以包含方法.属性.事件和索引器.接口不提供它所定义的成员的实现-它仅指定实现该接口的类或结构必须提供的成员 ...

  6. jQuery学习一:jQuery中的ready和load事件

    //ready事件 $(document).ready(function(){ 代码........ }); //ready事件简写: $(function(){ 代码........ }); //l ...

  7. UvaLive7362 Fare(欧拉函数)

    题意:求1~n的素因子之和. 分析:欧拉函数 #include<cstdio> #include<cstring> #include<cctype> #includ ...

  8. DEDECMS中,channel标签

    获取栏目列表标签 dede:channel 标签: {dede:channel type='top' row='8' currentstyle="<li><a href=' ...

  9. 正文字体大小:大 中 小 解决configure: error: Cannot find libmysqlclient under /usr

    今天在64位centos5.6系统上编译PHP5.2.17报错 checking for MySQL support... yes, shared checking for specified loc ...

  10. Linux rar

    http://www.vpsyou.com/2010/06/15/to-extract-rar-centos.html wget http://www.rarsoft.com/rar/rarlinux ...