java - 在springboot中加载多个带有注释的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 {
}
推荐阅读
- node.js - DNS 解析主机文件中映射到本地主机的域的超时/延迟
- python - 如何将时间偏移应用于熊猫时间戳间隔?
- c# - 如何通过 C# 中的值选择特定的复选框项
- docker - 如何将 docker 镜像从 USS 复制到 IBM zCX 服务器
- javascript - 我可以使用 Javascript 中的自引用函数定义对象的属性吗?
- java - Java中超时的用户输入
- python-3.x - 在 Python 中执行每个子类函数之前调用父类函数
- android - 更换工具栏后汉堡图标未正确显示
- javascript - 如何使用 Hooks 在 React Component 中重新启动音频
- excel - 如果满足条件,将行从一个工作簿复制到另一个工作簿