在 spring boot 中加载带有注释的多个 spring boot 应用程序

load multiple springboot app with annotation in springboot

我有一个 springboot 应用程序可以根据用户输入动态加载不同的类。

有人可以提出更好的优化方法吗?

以下是我的做法:-

//I have a map of actions available with their respective classes(which is also a springboot app)
private static Map<String,Class> actions = ImmutableMap.<String,Class>builder()
                                                       .put("A",A.class),
                                                       .put("B",B.class);

//main method of springboot root app
public static void main(String args[]){
 new MainApplication().run(args);
}

//get the action class and run that application
public void run(String args){
 Class action = getAction();
 SpringApplication app = new SpringApplicationBuilder(job).build();
 app.run(args);
}

//action.name is passed as an argument while starting the application( for.ex action.name="A")
private Class getAction(){
 String action = System.getProperty("action.name");
 Class classType = actionMap.get(action);
 return classType;
}

现在我只想加载这些带有注释的动作应用程序。 有人可以建议任何方法来做同样的事情吗?

使用条件Bean概念如下

@Service
@ConditionalOnProperty(
        value="action.name", 
        havingValue = "a", 
        matchIfMissing = false)
public class TestBeanConditional {

}