How to scan classes for annotations?

前端 未结 7 2186
野性不改
野性不改 2020-12-28 17:00

I have a plain jane servlets web application, and some of my classes have the following annotations:

@Controller
@RequestMapping(name = \"/blog/\")
public cl         


        
7条回答
  •  梦毁少年i
    2020-12-28 18:03

    If you can get access of your .class files, then you may get the annotation using the class as below:

      RequestMapping reqMapping =  
                             TestController.class.getAnnotation(RequestMapping.class);
      String name = reqMapping.name(); //now name shoud have value as "/blog/"
    

    Also please make sure, your classes are marked with RetentionPolicy as RUNTIME

      @Retention(RetentionPolicy.RUNTIME)
    

提交回复
热议问题