spring context - how to load properties file specified as system property in a batch file
I have a batch file which will set a system property with config.properties and execute a jar file. Spring context file inside the jar needs to load this property file to set the jdbc connection settings related bean in the context file. But I get file not found error as shown below. Any help is appreciated. Thanks in advance.
org.springframework.beans.factory.BeanInitializationException: Could not load pr
operties; nested exception is java.io.FileNotFoundException: \${config.propertie
s.name} (The system cannot find the file specified)
org.springframework.beans.factory.BeanInitializationException: Could not load pr
operties; nested exception is java.io.FileNotFoundException: \${config.propertie
s.name} (The system cannot find the file specified)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.p
ostProcessBeanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invoke
BeanFactoryPostProcessors(AbstractApplicationContext.java:624)
at org.springframework.context.support.AbstractApplicationContext.invoke
BeanFactoryPostProcessors(AbstractApplicationContext.java:599)
at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:398)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:83)
at ax.bx.cx.ABCClass.main()
Caused by: java.io.FileNotFoundException: \${config.properties.name} (The system
cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection
.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLCon
nection.java:161)
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.ja
va:124)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadPrope
rties(PropertiesLoaderSupport.java:181)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProp
erties(PropertiesLoaderSupport.java:161)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.p
ostProcessBeanFactory(PropertyResourceConfigurer.java:69)
batch file content:
set JAVA_OPTS=%JAVA_OPTS% -Dconfig.properties.name=../config.properties
java -classpath .;../lib/xyz.jar ax.bx.cx.ABCClass
Spring context file within the jar:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<context:property-placeholder开发者_如何学C location="classpath:${config.properties.name}"/>
<!-- other beans declared below -->
</beans>
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_FALLBACK</value>
</property>
</bean>
<context:property-placeholder location="file:${config.properties.name}"/>
Now since you are using context schema, modify your beans tag as follows -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
//.....
</beans>
精彩评论