由于现在大多数已经不使用xml配置,基本是基于注解实现,因此,此处不考虑xml文件的相关操作(读取,校验等)
1.spring的自动扫描注册功能, 可采用类库 org.reflections,可大大降低反射的操作难度
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
具体使用方式
ConfigurationBuilder builder = new ConfigurationBuilder()
.filterInputsBy(new FilterBuilder().includePackage("com.chen"))
.addUrls(ClasspathHelper.forPackage("com.chen")) //这两个都写包名称
.setScanners(
new MethodAnnotationsScanner(),
new TypeAnnotationsScanner(),
new TypeElementsScanner(),
new MethodParameterNamesScanner(), // 这个必须加上,否则无法获取方法的参数名称
new FieldAnnotationsScanner(),
new SubTypesScanner(false)
);
Reflections reflections = new Reflections(builder);
for (Method m: reflections.getMethodsAnnotatedWith(*****.class)){
System.out.println(Collections.singletonList(reflections.getMethodParamNames(m)));
}
暂时先写这么多吧