3.4 spring- lookup-method 子元素的使用与解析
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 子元素的使用与解析的更多相关文章
- spring lookup method 注入
lookup method注入是spring动态改变bean里方法的实现.方法执行返回的对象,使用spring内原有的这类对象替换,通过改变方法返回值来动态改变方法.内部实现为使用cgl ...
- 3.5 spring-replaced-method 子元素的使用与解析
1.replaced-method 子元素 方法替换: 可以在运行时用新的方法替换现有的方法,与之前的 look-up不同的是replace-method 不但可以动态地替换返回的实体bean,而且可 ...
- 3.8 spring-qualifier 子元素的使用与解析
对于 qualifier 子元素,我们接触的更多的是注解形式,在使用Spring 自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean 时, S ...
- 3.7 spring-property 子元素的使用与解析
1.0 Property子元素的使用 property 子元素是再常用不过的了, 在看Spring源码之前,我们先看看它的使用方法, 1. 实例类如下: public class Animal { p ...
- 3.6 spring-construction-arg 子元素的使用与解析
对于构造函数子元素是非常常用的. 相信大家也一定不陌生, 举个小例子: public class Animal { public String type; public int age; /** * ...
- 3.3 spring-meta子元素的使用与解析
1. meta元素的使用 在解析元数据的分析之前,我们先回顾一下 meta属性的使用: <bean id="car" class="test.CarFactoryB ...
- spring源码学习之默认标签的解析(一)
继续spring源码的学习之路,现在越来越觉得这个真的很枯燥的,而且我觉得要是自己来看源码,真的看不下去,不是没有耐心,而是真的没有头绪,我觉得结合着书来看,还是很有必要的,最起码大致的流程是能够捋清 ...
- 1.spring.net Look-up Method 查找方法的注入(方法是抽象的需要spring.net注入)
.为什么需要查找方法的注入 当Object依赖另一个生命周期不同的Object,尤其是当singleton依赖一个non-singleton时,常会遇到不少问题,Lookup Method Injec ...
- spring bean属性及子元素使用总结
spring bean属性及子元素使用总结 2016-08-03 00:00 97人阅读 评论(0) 收藏 举报 分类: Spring&SpringMVC(17) 版权声明:本文为博主原创 ...
随机推荐
- CSS3秘笈第三版涵盖HTML5学习笔记1~5章
第一部分----CSS基础知识 第1章,CSS需要的HTML HTML越简单,对搜索引擎越友好 div是块级元素,span是行内元素 <section>标签包含一组相关的内容,就像一本书中 ...
- css3 背景记
css3 背景 css背景主要包括五个属性: background-color background-color:transparent || <color> 用来设置元素的背景颜色,默认 ...
- Bootstrap之Footer页尾布局(2015年05月28日)
直接上页尾部分的代码: <!--采用container-fluid,使得整个页尾的宽度为100%,并设置它的背景色--><footer class="container-f ...
- Bootstrap插件之Carousel轮播效果(2015年-05月-21日)
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"& ...
- 【AngularJs】---Error: [$injector:modulerr] Failed to instantiate module starter.services
[遇到问题解决问题,原谅我这个菜鸟] 加了services angular.module('starter', ['ionic', 'starter.controllers', 'starter.se ...
- ibatis.net 多线程的调试
ibatis是一个挺不错的半自动orm框架,从java移植到c# 在ibatis中是支持多线程操作的,但是这几天的使用过程中发现就框架本身任然存在一些问题,可能会让你对多线程的使用并不是那么的顺利 在 ...
- Span flag详解
在android中,如果要实现text的各种样式,图文混排等,简单的样式可以靠几个不同的textview来拼成,而复杂的样式要求,用不同的textview来拼接则不太现 实.这时候就spannable ...
- BootStrap glyphicons字体图标
本章将讲解Bootstrap glyphicons字体图标,并通过一些实例了解它的使用,字体图标是在 Web 项目中使用的图标字体.字体图标在下载的Bootstrap的fonts文件夹中 本章将讲 ...
- Xcode 真机无法调试
关于只能在模拟器上测试不能在真机测试的问题 2. 在 buildSetting 里面搜索bitcode,更改为 No 即可.
- BT之下拉菜单
<div class="dropdown"> <button class="btn btn-default dropdown-toggle" ...