Java 如何使用Micronaut注册和运行单个接口的所有实现?

Java 如何使用Micronaut注册和运行单个接口的所有实现?,java,interface,micronaut,Java,Interface,Micronaut,假设我有一个简单的界面: public interface Event { void execute(SomeClient client); } 我有5个类实现这个接口,例如: public class FirstEvent implements Event { @Override public void execute(SomeClient client) { //do various things here client.regist

假设我有一个简单的界面:

public interface Event {
    void execute(SomeClient client);
}
我有5个类实现这个接口,例如:

public class FirstEvent implements Event {
    @Override
    public void execute(SomeClient client) {
        //do various things here
        client.registerEvent(someData)
    }
我想要实现的是在应用程序启动期间运行
execute(SomeClient-client)
接口的所有5个实现的
Event
方法

我已经准备好了启动我需要的所有事件的代码,但是我想将这些事件划分为多个类,并有一个执行这些事件的单点。我想创建一个
事件
接口,并使用Micronauts
@Singleton
注释进行多个实现,这样我就可以简单地将它们全部注册,而无需将它们逐个添加到当前正在执行它们的类中

这个接口解决方案是一个好的决定吗?有更好的方法吗?实现我想要的最好方法是什么(用一个方法运行所有类,而不是逐个运行它们)

这个接口解决方案是一个好的决定吗

是的

有更好的方法吗

一般来说,没有

实现我想要的(运行所有类)的最佳方法是什么 使用单个方法而不是逐个运行它们)

没有客观的“最佳”方法,因为您可以使用不同的技术针对不同的目标进行优化。一个选项是,您可以将所有
事件
bean注入将对所有bean调用
execute
方法的内容中

@Singleton
public class SomeBean {
    private List<Event> events;

    public SomeBean(List<Event> events) {
        this.events = events;
    }

    // In this class you have all of the `Event`.
    // it is not clear from the question where the
    // SomeClient instance comes from, but once
    // you have that you could iterate over all
    // of the events and pass the client to the
    // execute method on each Event.
}
@Singleton
公共类SomeBean{
私人列表活动;
公共SomeBean(列出事件){
这个事件=事件;
}
//在这个类中,您拥有所有的“事件”。
//从这个问题上看,我们不清楚
//某个客户端实例来自,但只有一次
//你有一个可以遍历所有
//并将客户端传递给
//对每个事件执行方法。
}