在Guice中模仿Spring的个人资料 [英] Mimicking Spring profiles in Guice
问题描述
在Spring中,如果我想要一组用于生产的对象,而另一组用于本地开发/测试的对象.我可以使用@Profile
批注指定不同的类,并在启动应用程序时通过提供系统属性在它们之间进行切换.
In Spring, if I want to have one set of objects for production, and another for local development/testing. I could use the @Profile
annotation to designate the different classes, and switch between them by providing a system property when starting the app.
Guice中是否有类似的东西,或者我需要在引导Injector
时手动检查自己的某些属性并加载不同的模块集?
Is there anything like this in Guice, or do I need to manually check some property myself and load a different set of modules when bootstrapping my Injector
?
推荐答案
您需要自己确定环境,并选择要在该环境中应用的模块,但是您确实可以访问
You'll need to identify the environment yourself, and choose which modules to apply in which environment, but you do have access to Modules.override
to specifically override certain bindings without having to create a lot of module variants. Use it judiciously—it's very easy to get your modules tangled if you override many bindings or in unpredictable places.
这篇关于在Guice中模仿Spring的个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!