最近在看国内比较优秀的分布式调度框架xxl-job的源码,作者使用了比较多的spring4版本后提供的便捷方法,正好自己工作中有用到xxl-job,就顺带研究下。
xxl-job相关的代码就不细讲了,重点讲下MethodIntrospector这个工具类的使用场景。
先看下源码中的注释:
/**
* Defines the algorithm for searching for metadata-associated methods exhaustively
* including interfaces and parent classes while also dealing with parameterized methods
* as well as common scenarios encountered with interface and class-based proxies.
*
* <p>Typically, but not necessarily, used for finding annotated handler methods.
*
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @since 4.2.3
*/
大概的意思是,定义了查找指定方法的一些算法,主要可以被用来查找有指定注解的方法
核心方法 selectMethods
/**
* Select methods on the given target type based on the lookup of associated metadata.
* <p>Callers define methods of interest through the {@link MetadataLookup} parameter,
* allowing to collect the associated metadata into the result map.
* @param targetType the target type to search methods on
* @param metadataLookup a {@link MetadataLookup} callback to inspect methods of interest,
* returning non-null metadata to be associated with a given method if there is a match,
* or {@code null} for no match
* @return the selected methods associated with their metadata (in the order of retrieval),
* or an empty map in case of no match
*/
public static <T> Map<Method, T> selectMethods(Class<?> targetType, final MetadataLookup<T> metadataLookup) {
fi