Java 获取bean方法上的注释
我试图在我的bean上获取方法的注释,但是注释列表返回空。我是这样做的Java 获取bean方法上的注释,java,spring-boot,Java,Spring Boot,我试图在我的bean上获取方法的注释,但是注释列表返回空。我是这样做的 @PostConstruct public void findSensitiveAnnotations(){ Map<String,Object> beans = applicationContext.getBeansWithAnnotation(RestController.class); for (Map.Entry<String, Object> entry : beans.en
@PostConstruct
public void findSensitiveAnnotations(){
Map<String,Object> beans = applicationContext.getBeansWithAnnotation(RestController.class);
for (Map.Entry<String, Object> entry : beans.entrySet()){
for (int i=0 ; i<entry.getValue().getClass().getDeclaredMethods().length ; i++){
System.out.println(entry.getValue().getClass().getDeclaredMethods()[i]);
for (Annotation a : entry.getValue().getClass().getDeclaredMethods()[i].getAnnotations()){
System.out.println(a.toString());
}
}
}
}
但是第二个System.out.println()
从未被调用,注释列表长度==0
下面是对应于sendResetEmail(@RequestParam String-email,HttpServletRequest-request)
我认为您需要调用
getDeclaredAnnotations()
而不是getAnnotations()
getBeansWithAnnotation似乎没有返回真正的类名
尝试这样做:
Map<String,Object> beans = applicationContext.getBeansWithAnnotation(Service.class);
for (Map.Entry<String, Object> entry : beans.entrySet()){
Class<?> clazz = entry.getValue().getClass();
//System.out.println(clazz.getName());
String[] split = clazz.getName().split("\\$");
String realClassName = split[0];
//System.out.println(realClassName);
Class<?> myClass = getClass().getClassLoader().loadClass(realClassName);
Method[] methods = myClass.getDeclaredMethods();
for(Method method: methods) {
for (Annotation a : method.getAnnotations()){
System.out.println(a.toString());
}
}
}
Map bean=applicationContext.getBeansWithAnnotation(Service.class);
for(Map.Entry:beans.entrySet()){
Class clazz=entry.getValue().getClass();
//System.out.println(clazz.getName());
字符串[]split=clazz.getName().split(\\$);
字符串realClassName=split[0];
//System.out.println(realClassName);
类myClass=getClass().getClassLoader().loadClass(realClassName);
方法[]方法=myClass.getDeclaredMethods();
用于(方法:方法){
对于(注释a:method.getAnnotations()){
System.out.println(a.toString());
}
}
}
注释需要有@Retention(RUNTIME)才能通过反射显示。@MarcosVasconcelos,抱歉。我未能添加注释代码Yep,是由于类名称后面的$$EnhancerBySpringCGLIB$$623cfd77
导致了问题
@RequestMapping(value = "/reset/byEmail",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@Sensitive
public ResponseEntity<String> sendResetEmail(@RequestParam String email, HttpServletRequest request) {...}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Sensitive { }
Map<String,Object> beans = applicationContext.getBeansWithAnnotation(Service.class);
for (Map.Entry<String, Object> entry : beans.entrySet()){
Class<?> clazz = entry.getValue().getClass();
//System.out.println(clazz.getName());
String[] split = clazz.getName().split("\\$");
String realClassName = split[0];
//System.out.println(realClassName);
Class<?> myClass = getClass().getClassLoader().loadClass(realClassName);
Method[] methods = myClass.getDeclaredMethods();
for(Method method: methods) {
for (Annotation a : method.getAnnotations()){
System.out.println(a.toString());
}
}
}