pom.xml:
<dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.9.11/version> </dependency>
定义注解类:
/** * * 业务逻辑注解,所有包含该注解的类都认为是一个业务逻辑需求 * 不需要参数 * */ @Target({ ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Business { }
获取某个包下注解为@Business类:
import java.util import com.test.dps.annotation.Business import org.reflections.Reflections import scala.collection.JavaConversions._ object TestManager { private final val BASIC_PACKAGE = "com.test" def main(args: Array[String]): Unit = { val reflections: Reflections = new Reflections(BASIC_PACKAGE) val annotations: util.Set[Class[_]] = reflections.getTypesAnnotatedWith(classOf[Business]) annotations.foreach(clazz=>{ val name: String = clazz.getSimpleName println(name) }) } }