【发布时间】:2018-09-25 11:30:07
【问题描述】:
我有注释:
@Retention(RUNTIME)
public @interface MyHandler {
MyType type();
}
我有 3 个班级:
@MyHandler(type = MyType.TYPE1)
@Component
public class MyFirstHandler implements MyHandler {
public MyResponse test() {
return new MyResponse("first");
}
}
@MyHandler(type = MyType.TYPE2)
@Component
public class MySecondHandler implements MyHandler {
public MyResponse test() {
return new MyResponse("second");
}
}
@MyHandler(type = MyType.TYPE3)
@Component
public class MyLastHandler implements MyHandler {
public MyResponse test() {
return new MyResponse("last");
}
}
我需要找到所有带有@MyHandler 注释的bean 并从这个bean 创建resolver。之后我需要这个 locic:
MyHandler handler = resolver.getHandler(MyType.TYPE3)
用spring boot怎么办?
【问题讨论】:
-
为什么?只需创建一个委托给
ApplicationContext的解析器,然后使用getBeansWithAnnotation方法来检索正确的解析器。
标签: java spring annotations