2016-09-16 3 views
1

Я пытаюсь инициализировать компонент PropertyPlaceholderConfigurer с двумя местоположениями. Первое место имеет статическое значение, указывающее на файл свойства. Этот файл содержит путь к директории с вторым местоположением. Структура файла контекста приложения как нижеПружинный заполнитель внутри местоположения PropertyPlaceholderConfigurer

<?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

По прежнему получается исключение 'Caused by: java.io.FileNotFoundException: $ {file2.dir} file2.properties' –

+0

вы можете показать мне полный файл трассировки стека и свойства – Hammad

+0

Я добавил файл трассировки стека и файл свойств на свой вопрос. –

0

Вы получите FileNotFoundException, потому что всякий раз, когда вы говорите $ {file2.dir} внутри вашего файла конфигурации выглядит внутри того же xml

i .e XML-файл ищет свойство, называемое file2.dir, которое является частью утилиты.

добавьте следующие Namespace & контекстов в ваш 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

Удачи!

Смежные вопросы