Java-执行带有指定批注的类方法 [英] Java - Execute a class method with a specify annotation

查看:102
本文介绍了Java-执行带有指定批注的类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android应用程序,但这无关紧要.

I have a android application, but it is not relevant.

我有一个名为"Front controller"的类,它将收到一些消息通过它的构造函数.为了简洁起见,该消息可以是整数.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆