我有三个数据库配置
- 用于单元测试环境的本地数据库
- 开发环境测试数据库
- 产品环境在线数据库
如何根据环境自动注入(inject)不同的数据库属性?
By using xml with spring, I can use maven profiles and Resources filter feature to have this work done.
请问guice有什么解决办法吗?
最佳答案
在最简单的情况下,您可以使用 binder.bindConstant()
在您的Module
内像这样:
bindConstant().annotatedWith(Names.named("user.name")).to(System.getProperty("user.name"));
你可以像这样使用它:
@Inject @Named("user.name")
private String userName;
如何在程序启动时设置系统属性取决于您。
另一种类似的方法是实用方法 cNames.bindProperties(Binder, Map<String, String>)
您可以一次性设置任意属性。
关于java - Guice多数据库自动切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16143904/