前面一节讲了meta标签的解析的过程,接下来看spring对lookup-method的解析过程,通过BeanDefinitionParserDelegate#parseLookupOverrideSubElements()方法中完成解析过程,在平时的开发中lookup-method不是很常用,一般用于获取器的注入,简单的来看一个case,代码如下:
首先我们创建一个实例User类,代码如下:
package com.sgcc.bean;
public class User {
public void showMe(){
System.out.println("i is user");
}
}
在创建一个Teacher类来继承该User,代码如下:
package com.sgcc.bean;
public class Teacher extends User {
public void showMe(){
System.out.println("i is Teacher");
}
创建一个类来调用对应的方法:
package com.sgcc.bean;
public abstract class GetBeanCase {
public void showMe(){
this.getBean().showMe();
}
public abstract User getBean();
在来创建一个测试类代码如下:
package com.sgcc.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
GetBeanCase getBeanCase = (GetBeanCase) context.getBean("getBeanCase");
getBeanCase.showMe();
}
来看配置文件,代码如下:
<?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.xsd">
<bean id="getBeanCase" class="com.sgcc.bean.GetBeanCase">
<lookup-method name="getBean" bean="teacher"/>
</bean>
<bean id="teacher" class="com.sgcc.bean.Teacher"/>
</beans>
来看运行结果:
配置文件中,我们看到了lookup-method作为字标签出现,通过配置动态的将id元素也就是这里的teacher作为getBean的返回值,所以我们才会在控制台上看到了我们想要的结果,假如我这里想要变更需求,我想在控制台上看到i is Person的结果,该如何实现了,直接看代码:
public class Person extends User {
public void showMe(){
System.out.println("i is Person");
}
来看配置文件
<?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.xsd">
<bean id="getBeanCase" class="com.sgcc.bean.GetBeanCase">
<lookup-method name="getBean" bean="person"/>
</bean>
<bean id="teacher" class="com.sgcc.bean.Teacher"/>
<bean id="person" class="com.sgcc.bean.Person"/>
</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++) {
//获取元素是LOOKUP_METHOD_ELEMENT
Node node = nl.item(i);
if (isCandidateElement(node) && nodeNameEquals(node, LOOKUP_METHOD_ELEMENT)) {
Element ele = (Element) node;
//获取name和be属性beanRef
String methodName = ele.getAttribute(NAME_ATTRIBUTE);
String beanRef = ele.getAttribute(BEAN_ELEMENT);
//创建LookupOverride实例
LookupOverride override = new LookupOverride(methodName, beanRef);
//封装source属性
override.setSource(extractSource(ele));
//保存LookupOverride实例
overrides.addOverride(override);
}
}
}
上述就是解析子标签lookup-method的过程,简单的总结一下,在该方法中我们看到需要两个参数,其中MethodOverrides的参数简答的说一下:
到该方法上一层,我们发现是这样的传参方法
parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
是通过AbstractBeanDefinition#getMethodOverrides()来获取到的,接着往上走:
AbstractBeanDefinition.java
private MethodOverrides methodOverrides;
/**
* Return information about methods to be overridden by the IoC
* container. This will be empty if there are no method overrides.
* <p>Never returns {@code null}.
*/
public MethodOverrides getMethodOverrides() {
if (this.methodOverrides == null) {
this.methodOverrides = new MethodOverrides();
}
return this.methodOverrides;
}
发现methodOverrides只是作为AbstractBeanDefinition的一个内部属性,只是用来保存对应的数据的,找到了这里相信大家不迷茫了,这个参数的源头来源,接下来我们对methodOverrides类来简单的了解一下:
MethodOverrides
简单的说,就是一组覆盖方法的载体,保证在spring的容器中运行时将此方法交给对应的对象来做处理,该类是在 org.springframework.beans.factory.support包下来看代码实现:
public class MethodOverrides {
//用来保存MethodOverrides实例
private final Set<MethodOverride> overrides = Collections.synchronizedSet(new LinkedHashSet<>(2));
//默认是未做修改
private volatile boolean modified = false;
/**
* Create new MethodOverrides.
*/
public MethodOverrides() {
}
/**
* Deep copy constructor.
*/
public MethodOverrides(MethodOverrides other) {
addOverrides(other);
}
/**
* Copy all given method overrides into this object.
*/
//保存所有的MethodOverrides类型的
public void addOverrides(@Nullable MethodOverrides other) {
if (other != null) {
this.modified = true;
this.overrides.addAll(other.overrides);
}
}
/**
* Add the given method override.
*/
//只用来保存MethodOverride类型的
public void addOverride(MethodOverride override) {
this.modified = true;
this.overrides.add(override);
}
/**
* Return all method overrides contained by this object.
* @return a Set of MethodOverride objects
* @see MethodOverride
*/
public Set<MethodOverride> getOverrides() {
this.modified = true;
return this.overrides;
}
/**
* Return whether the set of method overrides is empty.
*/
public boolean isEmpty() {
return (!this.modified || this.overrides.isEmpty());
}
/**
* Return the override for the given method, if any.
* @param method method to check for overrides for
* @return the method override, or {@code null} if none
*/
//获取MethodOverride方法
@Nullable
public MethodOverride getOverride(Method method) {
if (!this.modified) {
return null;
}
synchronized (this.overrides) {
MethodOverride match = null;
for (MethodOverride candidate : this.overrides) {
if (candidate.matches(method)) {
match = candidate;
}
}
return match;
}
}
上述就是MethodOverrides的内部方法,接下来我们简单的总结下解析lookup-method的过程
- 首先是获取所有的节点元素
NodeList nl = beanEle.getChildNodes();
- 接下来遍历NodeList只拿取lookup-method的元素
public static final String LOOKUP_METHOD_ELEMENT = "lookup-method";
public static final String NAME_ATTRIBUTE = "name";
public static final String BEAN_ELEMENT = "bean";
Node node = nl.item(i);
- 获取name和bean属性
Element ele = (Element) node;
//获取name和be属性bean
String methodName = ele.getAttribute(NAME_ATTRIBUTE);
String beanRef = ele.getAttribute(BEAN_ELEMENT);
- 通过前面获取到的name和bean属性来创建LookupOverride实例
LookupOverride override = new LookupOverride(methodName, beanRef);
LookupOverride.java
/**
* Construct a new LookupOverride.
* @param methodName the name of the method to override
* @param beanName the name of the bean in the current {@code BeanFactory}
* that the overridden method should return (may be {@code null})
*/
public LookupOverride(String methodName, @Nullable String beanName) {
super(methodName);
this.beanName = beanName;
}
MethodOverride.java
/**
* Construct a new override for the given method.
* @param methodName the name of the method to override
*/
protected MethodOverride(String methodName) {
Assert.notNull(methodName, "Method name must not be null");
this.methodName = methodName;
}
上述代码就是创建LookupOverride的过程,代码简单就不说啥了,想去了解LookupOverride的实现可以自己去看,这里简答附上一张它的家族图
可以看到LookupOverride直接继承于MethodOverride类,间接的实现了BeanMetadataElement接口的方法
- 封装source属性
override.setSource(extractSource(ele));
- 保存LookupOverride实例
overrides.addOverride(override);
MethodOverrides.java
//只用来保存MethodOverride类型的
public void addOverride(MethodOverride override) {
this.modified = true;
this.overrides.add(override);
}
以上就是lookup-method的解析过程