在我的Spring xml配置中,我试图让这样的东西起作用:
<beans>
<import resource="${file.to.import}" />
<!-- Other bean definitions -->
</beans>
Run Code Online (Sandbox Code Playgroud)
我想根据属性文件中的属性决定导入哪个文件.我知道我可以使用System属性,但我无法在启动时向JVM添加属性.
注:PropertyPlaceHolderConfigurer将无法正常工作.在运行任何BeanFactoryPostProcessor之前解析导入.import元素只能解析System.properties.
有人有一个简单的解决方案吗?我不想开始子类化框架类等等......
谢谢
我在stackoverflow.com上看过类似的问题,但没有一个解决方案对我有帮助.我使用的以下配置(maven项目结构):src/main/resources/properties/app.properties文件
#possible values: dev test prod
mode: dev
Run Code Online (Sandbox Code Playgroud)
在Spring配置中:
<context:property-placeholder location="classpath:properties/app.properties"/>
<import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>
Run Code Online (Sandbox Code Playgroud)
基于${mode}我想要导入相应数据源配置文件的值.
当我使用mvn clean install tomcat7:run命令运行嵌入式tomcat7时,我收到错误:
10, 2013 5:52:29 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /SpringWebFlow threw load() exception
java.lang.IllegalArgumentException: Could not resolve placeholder 'mode' in string value "classpath:/spring/db/${mode}-datasource-config.xml"
Run Code Online (Sandbox Code Playgroud)
该target/classes/properties/app.properties文件存在.
我正在使用IntelliJ IDEA,在编辑器中我可以单击"$ {mode}" <import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>并在属性文件中查看其值.编辑器本身也改变${mode}为灰色dev显示它可以识别属性值.在编辑器中我看到:<import resource="classpath:/spring/db/dev-datasource-config.xml"/>
任何想法为什么我得到错误以及如何解决?