Java - 执行带有指定注释的类方法 [英] Java - Execute a class method with a specify annotation
问题描述
我有一个 android 应用程序,但它不相关.
I have a android application, but it is not relevant.
我有一个名为前端控制器"的类,它将接收一些消息通过它的构造函数.为简洁起见,该消息可以是一个整数.
I have a class called "Front controller" which will receive some message through it's constructor. The message, for brievity, could be an integer.
我想在其他地方创建一个新的控制器来执行一种基于上面定义的整数的方法
I want somewhere else to create a new controller which will execute a method based on the integer defined above
public class OtherController {
@MessageId("100")
public void doSomething(){
//execute this code
}
@MessageId("101")
public void doSomethingElse(){
//code
}
}
前端控制器可能是这样的:
The front controller could be something like this:
public class FrontController {
private int id;
public FrontController(int id){
this.id=id;
executeProperControllerMethodBasedOnId();
}
public void executeProperControllerMethodBasedOnId(){
//code here
}
public int getId(){
return id;
}
}
所以,如果前端控制器收到整数 100,它将执行用@MessageId(100) 注释的方法.这前端控制器不确切知道此方法所在的类是.
So, if the Front Controller will receive the integer 100, it will execute the method annotated with @MessageId(100). The front controller don't know exactly the class where this method is.
我发现的问题是我需要以某种方式注册每个控制器类.我春天我有@Component 或 @Controller用于自动加载.每个控制器注册后,我需要调用正确注释的方法.
The problem which I found is that I need to register somehow each controller class. I Spring I had @Component or @Controller for autoloading. After each controllers are register, I need to call the properly annotated method.
如何完成这个任务?在 Spring MVC 中,我有这个系统已实现,用于匹配 HTTP 路由.我该如何实施这是在一个普通的 Java 项目中吗?
How to achieve this task? In Spring MVC, I had this system implemented, used to match the HTTP routes. How could I implement this in a plain java project?
有什么建议吗?
推荐答案
感谢 Google Reflections(希望您可以将其集成到您的 android 项目中.)
Thanks to Google Reflections (hope you can integrate this in your android project.)
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections-maven</artifactId>
<version>0.9.8</version>
</dependency>
为了优化,我还添加了使用 MessageType 注释对类进行注释的要求,并且这些类应该在同一个包中(在我的示例中为 org.conffusion):
For optimisation I've added the requirement to also annotate the class with MessageType annotation and the classes should be in the same package (org.conffusion in my example):
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MessageType {
}
OtherController 看起来像:
The OtherController looks like:
@MessageType
public class OtherController {
@MessageId(id=101)
public void method1()
{
System.out.println("executing method1");
}
@MessageId(id=102)
public void method2()
{
System.out.println("executing method2");
}
}
实现将如下所示:
public void executeProperControllerMethodBasedOnId() {
Set<Class<?>> classes = new org.reflections.Reflections("org.conffusion")
.getTypesAnnotatedWith(MessageType.class);
System.out.println("found classes " + classes.size());
for (Class<?> c : classes) {
for (Method m : c.getMethods()) {
try {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
Object o = c.newInstance();
if (mid.id() == id)
m.invoke(o);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
也许您可以优化并构建一个包含已扫描消息 ID 的静态哈希图.
Maybe you can optimise and build a static hashmap containing already scanned message ids.
这篇关于Java - 执行带有指定注释的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!