在我的Spring XML配置我试图得到这样的工作:
<beans>
<import resource="${file.to.import}" />
<!-- Other bean definitions -->
</beans>
我想决定导入基于在属性文件属性的文件。 我知道我可以使用一个系统属性,但我不能属性的JVM在启动时添加。
注:PropertyPlaceHolderConfigurer将无法正常工作。 任何BeanFactoryPostProcessor的运行之前进口解决。 进口元素只能解决System.properties。
有没有人有一个简单的解决方案呢? 我不想启动子类框架类等等......
谢谢
Answer 1:
这是不幸的是,很多困难比它应该是。 在我的应用程序来完成此通过执行以下操作:
一个小的,“引导”背景下,负责装载提供一个PropertyPlaceholderConfigurer豆和另一个Bean是负责引导的应用程序上下文。
上面提到的第二豆作为输入的“真正的”弹簧上下文文件来加载。 我有我的有组织的Spring上下文文件,以便配置部分是众所周知的,在同一个地方。 例如,我可能有3个配置文件:one.onpremise.xml,one.hosted.xml,one.multitenant.xml。 这个bean程序加载这些上下文文件到当前应用程序上下文。
这工作,因为上下文文件被指定为输入负责加载它们的豆。 它不会工作,如果你只是尝试做进口,如你所说,但有稍微的工作同样的效果。 引导类看起来是这样的:
public class Bootstrapper implements ApplicationContextAware, InitializingBean {
private WebApplicationContext context;
private String[] configLocations;
private String[] testConfigLocations;
private boolean loadTestConfigurations;
public void setConfigLocations(final String[] configLocations) {
this.configLocations = configLocations;
}
public void setTestConfigLocations(final String[] testConfigLocations) {
this.testConfigLocations = testConfigLocations;
}
public void setLoadTestConfigurations(final boolean loadTestConfigurations) {
this.loadTestConfigurations = loadTestConfigurations;
}
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
context = (WebApplicationContext) applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
String[] configsToLoad = configLocations;
if (loadTestConfigurations) {
configsToLoad = new String[configLocations.length + testConfigLocations.length];
arraycopy(configLocations, 0, configsToLoad, 0, configLocations.length);
arraycopy(testConfigLocations, 0, configsToLoad, configLocations.length, testConfigLocations.length);
}
context.setConfigLocations(configsToLoad);
context.refresh();
}
}
基本上,让应用程序上下文,设置其配置的位置,并告诉它刷新自己。 这工作完全在我的应用程序。
希望这可以帮助。
Answer 2:
对于春季2.5和3.0, 我有一个类似的解决方案 ,以路易,但我刚读约3.1即将推出的功能: 物业管理 ,这听起来太伟大。
Answer 3:
有添加在春季JIRA一个老问题性质占位进口(SPR-1358)的支持这是解析为“不会修复”,但此后一直使用EagerPropertyPlaceholderConfigurer提议的解决方案。
我一直在游说有SPR-1358重新开放,但没有反应为止。 也许,如果其他人加入他们的使用情况对这个问题的意见,这将有助于提高人们的认识。
Answer 4:
为什么不:
- 读你的属性上的启动文件
- 这将确定要装入的Spring配置
- 无论Spring配置加载套具体的东西, 然后加载一个共同的Spring配置
所以你有效地反转你当前提出的解决方案。
Answer 5:
添加类似下面的内容:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound"><value>true</value></property>
<property name="locations">
<list>
<value>classpath:propertyfile.properties</value>
</list>
</property>
</bean>
Answer 6:
如果你想要的是applicationContext.xml的外单位指定导入XML文件名,这样你可以不失导入的XML文件路径的配置取代applicationContext.xml中,你可以添加一个中间Spring bean的XML文件,说,confSelector。 XML,使得进口applicationContext.xml中和confSelector.xml仅confSelector.xml包含一个<进口>元素指的是合适的定制豆XML文件。
这可能是使用的另一种方法是XML实体(加入定义<!ENTITY ...>元素融入DTD声明在XML的开始)。 这允许从其他文件导入XML片段,并提供“属性占位符”般的任何XML文件的功能。
这些解决方案都可以让你在Java的的.properties格式的配置文件,但。
Answer 7:
安德烈·舒斯特的答案,这是我撞,帮我解决我在想找到这取决于我在我自己的主机上我们构建主机上运行,詹金斯或属性的不同的表达有一个非常类似的问题,“真正的”部署。 我这样做:
<context:property-placeholder location="file:///etc/myplace/database.properties" />
后来跟着
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/classes/resources/database.properties</value>
...
</list>
</property>
</bean>
这解决了我的问题,因为我的开发主机上,我把一个链接到我自己的database.properties的副本/etc/myplace/database.properties和运行詹金斯在服务器上略有不同的一个。 在实际部署中,没有这样的文件被发现,所以Spring倒在我的类文件子目录中的“真实”的一个资源 。 如果有问题的性质已经由上/etc/myplace/database.properties指定的文件,然后(幸运的),他们不是由本地文件重新定义。
Answer 8:
它不依赖于系统性能的另一个解决方法是使用为每个文件不同PropertyPlaceholderConfigurer加载的所有文件的属性,定义不同的placeholderPrefix为他们每个人。 也就是说placeholderprefix由初始属性文件中配置。
限定所述第一属性的文件:(含有第一或第二)
global.properties
fileToUse=first
定义含有可取决于只是上面所定义的属性切换的属性的文件:
first.properties
aProperty=propertyContentOfFirst
second.properties
aProperty=propertyContentOfSecond
然后定义占位符的所有文件:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:global.properties</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="first{" />
<property name="locations">
<list>
<value>classpath:first.properties</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="second{" />
<property name="locations">
<list>
<value>classpath:second.properties</value>
</list>
</property>
</bean>
在全局中定义的属性标识的资源,从其他文件中使用:
${fileToUse}{aProperty}
Answer 9:
如果我添加了JVM参数下方,并有文件myApplicationContext.dev.xml,春天确实负载
-DmyEnvironment = dev的
<context:property-placeholder />
<import resource="classpath:/resources/spring/myApplicationContext.${myEnvironment}.xml"/>
Answer 10:
我使用Spring 3并加载一个这样的性质:
<context:property-placeholder location="/WEB-INF/my.properties" />
文章来源: Import Spring config file based on property in .properties file