Spring MethodOverride

  • Author: HuiFer

  • 源码阅读仓库: SourceHot-spring

  • org.springframework.beans.factory.support.MethodOverride

    • org.springframework.beans.factory.support.LookupOverride
    • org.springframework.beans.factory.support.ReplaceOverride
  • org.springframework.beans.factory.support.MethodOverrides

MethodOverride

  • MethodOverride 方法重载类

MethodOverride定义了下面三个属性

  1. 方法名称
  2. 是否重载
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public abstract class MethodOverride implements BeanMetadataElement {

   /**
    * 方法名称
    */
   private final String methodName;

   /**
    * 是否重载
    */
   private boolean overloaded = true;

   /**
    * 源
    */
   @Nullable
   private Object source;
}
  • 定义了一个抽象方法, 交由子类实现
1
public abstract boolean matches(Method method);

类图

MethodOverride

  • 在 Spring 中有两种可以重写的机制(XML)

    1. lookup-method 标签

      1
      
      <lookup-method name="" bean=""/>
      
    2. replaced-method 标签

      1
      
      <replaced-method name="" replacer=""/>
      

相对应的两个类如类图所示

LookupOverride

  • org.springframework.beans.factory.support.LookupOverride
  • lookup-method 标签对应的实体对象

属性列表

  1. beanName
  2. method
1
2
3
4
5
@Nullable
private final String beanName;

@Nullable
private Method method;

matches

比较方法

  1. method 是否直接相等
  2. method 名称是否相同
  3. 是否需要重载
  4. 是不是 ABSTRACT 方法
  5. 参数列表长度是否等于 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
	@Override
	public boolean matches(Method method) {
		if (this.method != null) {
			// 通过 equals 判断
			return method.equals(this.method);
		}
		else {
			// 1. method 名称是否相同
			// 2. 是否需要重载
			// 3. 是不是 ABSTRACT 方法
			// 4. 参数列表长度是否等于0
			return (method.getName().equals(getMethodName()) && (!isOverloaded() ||
					Modifier.isAbstract(method.getModifiers()) || method.getParameterCount() == 0));
		}
	}

ReplaceOverride

  • org.springframework.beans.factory.support.ReplaceOverride
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
/**
 * 实现 MethodReplacer 接口的bean name
 * @see MethodReplacer
 */
private final String methodReplacerBeanName;

/**
 * 标签 arg-type 数据
 */
private final List<String> typeIdentifiers = new LinkedList<>();
  • 一个例子
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns="http://www.springframework.org/schema/beans"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="apple" class="org.source.hot.spring.overview.ioc.bean.lookup.Apple">
		<replaced-method replacer="methodReplacerApple" name="hello" >
			<arg-type>String</arg-type>
		</replaced-method>

	</bean>

	<bean id="methodReplacerApple" class="org.source.hot.spring.overview.ioc.bean.lookup.MethodReplacerApple">

	</bean>

</beans>

methodReplacerBeanName 对应org.springframework.beans.factory.support.MethodReplacer 的实现类

typeIdentifiers 对应标签 arg-type 的属性值

构造方法

1
2
3
4
5
public ReplaceOverride(String methodName, String methodReplacerBeanName) {
   super(methodName);
   Assert.notNull(methodName, "Method replacer bean name must not be null");
   this.methodReplacerBeanName = methodReplacerBeanName;
}

methodName 通过父类进行设置

matches

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Override
public boolean matches(Method method) {
   // 方法名称是否相同
   if (!method.getName().equals(getMethodName())) {
      return false;
   }
   // 是否重载
   if (!isOverloaded()) {
      // Not overloaded: don't worry about arg type matching...
      return true;
   }
   // If we get here, we need to insist on precise argument matching...
   // 类型标识数量是否和参数列表是否不相同
   if (this.typeIdentifiers.size() != method.getParameterCount()) {
      return false;
   }
   // 获取参数类型列表
   Class<?>[] parameterTypes = method.getParameterTypes();
   for (int i = 0; i < this.typeIdentifiers.size(); i++) {
      String identifier = this.typeIdentifiers.get(i);
      // 判断 方法参数的类型是否在类型标识列表中
      if (!parameterTypes[i].getName().contains(identifier)) {
         return false;
      }
   }
   return true;
}

MethodOverrides

  • org.springframework.beans.factory.support.MethodOverrides

  • 重载方法对象

  • 存储所有重载的方法列表(set 结构)

1
	private final Set<MethodOverride> overrides = new CopyOnWriteArraySet<>();

几个方法

  1. 添加 MethodOverride

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    public void addOverride(MethodOverride override) {
       this.overrides.add(override);
    }
    
    public void addOverrides(@Nullable MethodOverrides other) {
    		if (other != null) {
    			this.overrides.addAll(other.overrides);
    		}
    }
    
  2. 获取 MethodOverride

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    @Nullable
    public MethodOverride getOverride(Method method) {
       MethodOverride match = null;
       for (MethodOverride candidate : this.overrides) {
          if (candidate.matches(method)) {
             match = candidate;
          }
       }
       return match;
    }