2016-09-16 70 views
1

我試圖初始化2個位置的PropertyPlaceholderConfigurer bean。第一個位置具有靜態值,指向屬性文件。該文件包含第二個位置的目錄路徑。應用程序上下文文件的結構如下圖所示PropertyPlaceholderConfigurer位置內的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" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>file:C:/SpringTests/src/main/resources/file1.properties</value> 
       <value>file:${file2.dir}file2.properties></value> 
      </list> 
     </property> 
    </bean> 
</beans> 

的問題是,爲什麼佔位符${file2.dir}不受值從第一屬性文件,以及如何解決這個問題所取代。當然,第一個文件裏面有這樣的屬性。 file1.properties的

內容

file2.dir=C:/SpringTests/src/main/resources/ 

堆棧跟蹤

19-Sep-2016 07:12:23.761 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed 
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: ${file2.dir}file2.properties (Nie można odnaleźć określonego pliku) 
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:147) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4716) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5178) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) 
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) 
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482) 
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) 
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) 
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) 
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) 
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) 
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324) 
    at sun.rmi.transport.Transport$1.run(Transport.java:200) 
    at sun.rmi.transport.Transport$1.run(Transport.java:197) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196) 
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.io.FileNotFoundException: ${file2.dir}file2.properties (Nie można odnaleźć określonego pliku) 
    at java.io.FileInputStream.open0(Native Method) 
    at java.io.FileInputStream.open(FileInputStream.java:195) 
    at java.io.FileInputStream.<init>(FileInputStream.java:138) 
    at java.io.FileInputStream.<init>(FileInputStream.java:93) 
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90) 
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188) 
    at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:125) 
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143) 
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) 
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175) 
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156) 
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:137) 
    ... 51 more 

回答

-1
<?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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context 4.2.xsd"> 

    <context:property-placeholder location="file:C:/SpringTests/src/main/resources/file1.properties" ignore-unresolvable="true" order="1" /> 
    <context:property-placeholder location="file:${file2.dir}file2.properties" order="2"/> 

</beans> 
+0

仍然得到一個異常'引起:java.io.FileNotFoundException:$ {file2.dir} file2.properties' –

+0

你能告訴我完整的堆棧跟蹤和屬性文件 – Hammad

+0

我添加了堆棧跟蹤和屬性文件到我的問題。 –

0

,因爲無論何時說$ {} file2.dir您裏面的配置文件,它看起來你會得到FileNotFoundException異常在同一個xml裏面

我i .e XML文件查找名爲file2.dir的屬性,它是util的一部分。

添加下面的命名空間&上下文到您的XML

xmlns:util="http://www.springframework.org/schema/util" 

http://www.springframework.org/schema/util  
http://www.springframework.org/schema/util/spring-util-3.0.xsd 

定義像下面

<context:property-placeholder 
    system-properties-mode="OVERRIDE" properties-ref="environment" /> 
<util:properties id="environment"> 
    <b:prop key="file2.dir">YOUR PATH HERE</b:prop> 
</util:properties> 

我知道你想使用XML配置的屬性佔位符推動態屬性的屬性。我認爲這是不可能的

可能是你可以嘗試有2個屬性佔位符定義爲提供here

祝您好運!