reflections 中包含很多的Scanner ,也就是扫描器,调用对应的方法时需要有配置对应的扫描器,不然程序会抛出异常.
扫描器结构:
使用时,我们主要使用Reflections 这个类来调用.
Reflections 默认配置了一部分扫描器,
但是在实际使用时需要添加很多的扫描器,可以根据程序抛出的异常进行添加对应的扫描
使用方法:
Reflections reflections = new Reflections("cn.*", new MethodAnnotationsScanner(), new TypeAnnotationsScanner(), new SubTypesScanner(), new MethodParameterNamesScanner());
指明扫描的包路径,并配置扫描器
1.获取所有带有action注解的类
Set> classes = reflections.getTypesAnnotatedWith(Action.class);for (Class>action : classes) {
RequestMapper request= action.getAnnotation(RequestMapper.class);
System.out.println(action+ "=RequestMapper==" +request.value());
}
2.获取所有带有requestMapper注解的方法
Set methods = reflections.getMethodsAnnotatedWith(RequestMapper.class);for(Method method : methods) {
System.out.println(method.getName()+ "=methods==" +reflections.getMethodParamNames(method));
}
获取某个方法的参数名称:(jdk里面没有对应的方法)
reflections.getMethodParamNames
原文:http://www.cnblogs.com/gj1990/p/7976389.html