Spring MethodOverride

简介: 《基础系列》
  • org.springframerk.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. 是否重载
public abstract class MethodOverride implements BeanMetadataElement {
   /**
    * 方法名称
    */
   private final String methodName;
   /**
    * 是否重载
    */
   private boolean overloaded = true;
   /**
    * 源
    */
   @Nullable
   private Object source;
}Copy to clipboardErrorCopied
  • 定义了一个抽象方法, 交由子类实现
public abstract boolean matches(Method method);Copy to clipboardErrorCopied


  • 在 Spring 中有两种可以重写的机制(XML)
  1. lookup-method 标签
<lookup-method name="" bean=""/>Copy to clipboardErrorCopied
  1. replaced-method 标签
<replaced-method name="" replacer=""/>Copy to clipboardErrorCopied

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

LookupOverride

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

属性列表

  1. beanName
  2. method
@Nullable
private final String beanName;
@Nullable
private Method method;Copy to clipboardErrorCopied

matches

比较方法

  1. method 是否直接相等
  2. method 名称是否相同
  3. 是否需要重载
  4. 是不是 ABSTRACT 方法
  5. 参数列表长度是否等于 0
@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));
        }
    }
Copy to clipboardErrorCopied

ReplaceOverride

  • org.springframework.beans.factory.support.ReplaceOverride
/**
 * 实现 MethodReplacer 接口的bean name
 * @see MethodReplacer
 */
private final String methodReplacerBeanName;
/**
 * 标签 arg-type 数据
 */
private final List<String> typeIdentifiers = new LinkedList<>();Copy to clipboardErrorCopied
  • 一个例子
<?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>Copy to clipboardErrorCopied

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

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

构造方法

public ReplaceOverride(String methodName, String methodReplacerBeanName) {
   super(methodName);
   Assert.notNull(methodName, "Method replacer bean name must not be null");
   this.methodReplacerBeanName = methodReplacerBeanName;
}Copy to clipboardErrorCopied

methodName 通过父类进行设置

matches

@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;
}Copy to clipboardErrorCopied

MethodOverrides

  • org.springframework.beans.factory.support.MethodOverrides
  • 重载方法对象
  • 存储所有重载的方法列表(set 结构)
private final Set<MethodOverride> overrides = new CopyOnWriteArraySet<>();Copy to clipboardErrorCopied

几个方法

  1. 添加 MethodOverride
public void addOverride(MethodOverride override) {
   this.overrides.add(override);
}
public void addOverrides(@Nullable MethodOverrides other) {
        if (other != null) {
            this.overrides.addAll(other.overrides);
        }
}Copy to clipboardErrorCopied
  1. 获取 MethodOverride
@Nullable
public MethodOverride getOverride(Method method) {
   MethodOverride match = null;
   for (MethodOverride candidate : this.overrides) {
      if (candidate.matches(method)) {
         match = candidate;
      }
   }
   return match;
}
相关文章
|
3月前
|
XML Java 开发者
【Spring】Spring是什么?
【Spring】Spring是什么?
【Spring】Spring是什么?
|
7月前
|
缓存 Java 数据库连接
|
4月前
|
前端开发 Java 开发者
【Spring】 ——初识Spring
【Spring】 ——初识Spring
39 0
|
4月前
|
存储 Java 数据库
【Spring】——Spring简单 读和取(一)
【Spring】——Spring简单 读和取
45 0
【Spring】——Spring简单 读和取(一)
|
4月前
|
存储 设计模式 Java
【Spring】——Spring简单 读和取(二)
【Spring】——Spring简单 读和取
36 0
【Spring】——Spring简单 读和取(二)
|
4月前
|
Java 程序员 Maven
Spring(上)
Spring(上)
27 0
|
9月前
|
Java Spring
Spring 是什么?
Spring 是什么?
42 0
|
9月前
|
消息中间件 缓存 Java
Spring 代码优化技巧(大全2)(一)
Spring 代码优化技巧(大全2)
211 1
|
9月前
|
存储 前端开发 Java
Spring 代码优化技巧(大全1)(二)
Spring 代码优化技巧(大全1)
111 0
|
XML 存储 Java
Spring的使用
Spring的使用
208 0
Spring的使用