我有一个批处理文件,它将使用config.properties设置系统属性并执行一个jar文件。 jar中的Spring上下文文件需要加载此属性文件,以在上下文文件中设置相关bean的jdbc连接设置。但我得到文件未找到错误,如下所示。任何帮助表示赞赏。提前谢谢。
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)
批处理文件内容:
set JAVA_OPTS=%JAVA_OPTS% -Dconfig.properties.name=../config.properties
java -classpath .;../lib/xyz.jar ax.bx.cx.ABCClass
jar中的Spring上下文文件:
<?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 location="classpath:${config.properties.name}"/>
<!-- other beans declared below -->
</beans>
答案 0 :(得分:4)
<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}"/>
现在,由于您正在使用上下文模式,请按如下所示修改beans标记 -
<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>