Tag: spring environment

在xml中定义Spring @PropertySource并在Environment中使用它

在Spring JavaConfig中,我可以定义属性源并注入到Environment中 @PropertySource(“classpath:application.properties”) @Inject private Environment environment; 如果在xml中我该怎么做? 我使用context:property-placeholder,并在JavaConfig类@ImportResource上导入xml。 但我无法使用environment.getProperty(“xx”)检索属性文件中定义的属性

动态注入spring bean

在java-spring网络应用程序中,我希望能够动态注入bean。 例如,我有一个具有2种不同实现的接口: 在我的应用程序中,我使用一些属性文件来配置注入: #Determines the interface type the app uses. Possible values: implA, implB myinterface.type=implA 我的注入实际上是有条件地在属性文件中的属性值上加载的。 例如,在这种情况下myinterface.type = implA无论我在哪里注入MyInterface,将注入的实现都是ImplA(我通过扩展条件注释来实现 )。 我希望在运行时 – 一旦属性发生更改,将发生以下情况(无需重新启动服务器): 将注入正确的实现。 例如,当设置myinterface.type=implB ImplB将被注入到使用MyInterface的地方 应该使用新值刷新Spring Environment并重新注入bean。 我想要刷新我的上下文,但这会产生问题。 我想可能会使用setter进行注入,并在重新配置属性后重新使用这些setter。 是否有这种要求的工作实践? 有任何想法吗? UPDATE 正如一些人所建议我可以使用一个工厂/注册表来保存两个实现(ImplA和ImplB),并通过查询相关属性返回正确的实现。 如果我这样做,我还有第二个挑战 – 环境。 例如,如果我的注册表看起来像这样: @Service public class MyRegistry { private String configurationValue; private final MyInterface implA; private final MyInterface implB; @Inject […]