How to find annotated methods in a given package?

前端 未结 3 1042
再見小時候
再見小時候 2020-11-28 23:25

I have a simple marker annotation for methods (similar to the first example in Item 35 in Effective Java (2nd ed)):

/**
 * Marker annotation for met         


        
3条回答
  •  暖寄归人
    2020-11-28 23:52

    If you're happy to use Spring, then that does something along these lines using it's context:component-scan functionality, where Spring scans for annotated classes in a given package. Under the covers, it's pretty gruesome, and involves grubbing about on the filesystem and in JAR files looking for classes in the package.

    Even if you can't use Spring directly, having a look through its source code might give you some ideas.

    Certainly, the Java reflection APi is no use here, it specifically does not provide a means to obtain all classes in a package.

提交回复
热议问题