Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: pass location value to PropertyPlaceholderConfigurer thought context-param

  1. #1
    Join Date
    Jul 2010
    Posts
    21

    Lightbulb pass location value to PropertyPlaceholderConfigurer thought context-param

    Problem is to load property file from external location not in classpath witch is given as context parameter in web.xml.


    The path of the config file is given as context-apram in web.xml:

    Code:
           <context-param>
                <param-name>config-filename</param-name>
    	    <param-value>C:/my.properties</param-value>
           </context-param>
    If to put this In the Spring context.xml, Spring can't resolve ${config-filename} placeholder until it's configured the property-placeholder (http://stackoverflow.com/questions/1...other-property)

    Code:
           <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">	
    		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>	
    	</bean>
    	<context:property-placeholder location="file:${config-fileName}" />
    So as it was suggested I have now this:

    Code:
           <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">	
    		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    		<property name="location">
    	    	<bean class="java.lang.System" factory-method="getenv">
    	        	<constructor-arg value="config-fileName"/>
    	     	</bean>    	
       		</property>	
    	</bean>
    	<context:property-placeholder location="file:${config-fileName}" />
    But properties files doesn't load:

    Code:
    2012-09-25 14:52:18,068 1 ERROR [main] context.ContextLoader - Context initialization failed
    java.lang.NullPointerException
    	at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
    	at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
    	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:640)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:615)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    For starters your configuration is flawed as you have 2 PropertyPlaceHoldersConfigured either use the namespace or use the plain bean but not both and one will not process the other as they are initialized in the same part of the startup sequence.

    Also getEnv has nothing to do with context-parameters those are completly different so your getenv approach is basically not going to work.

    Just curious why does it have to be a context-parameter? If you can set it as a context parameter you can also set it is a value in your spring configuration?

    However assuming you use Spring 3.x or higher you could try some SpEL to achieve your goal.

    Code:
    <context:property-placeholder location="file:#{contextAttributes['config-filename']}" />
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2010
    Posts
    21

    Default

    I changed the configuration to
    Code:
           <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location" value="file:///#{contextAttributes['config-filename']}"/>
    		<property name="ignoreUnresolvablePlaceholders" value="true"/>
    	</bean>
    Now, the problem is the properties in file are not accessible:
    Code:
    2012-09-25 16:52:30,157 770 ERROR [main] util.JDBCExceptionReporter - Cannot load JDBC driver class '${database.driver.classname}'
    I use Spring 3.0.

    Quote Originally Posted by Marten Deinum View Post
    Just curious why does it have to be a context-parameter? If you can set it as a context parameter you can also set it is a value in your spring configuration?
    Lately the file path will be given through Tomcat context configuration.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I'm still not sure why you are using the plain bean instead of the namespace.. Remove ignoreUnresolvablePlaceholders and enable debug/trace logging for the org.springframework.beans.factory.config package and see what is happening (and if the file gets replaced). Also why the /// in fthe location only file: should be enough.

    Lately the file path will be given through Tomcat context configuration.
    Which isn't the same as a context-param in web.xml (I assume here you mean putting it in a context.xml file for tomcat) as in general those are JNDI resources...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jul 2010
    Posts
    21

    Default

    The same result with namespace.

    for
    Code:
    <context:property-placeholder location="file:///#{contextAttributes['dgs3g-filename']}" />
    got the exception:

    Code:
    2012-09-25 17:18:59,105 1 ERROR [main] context.ContextLoader - Context initialization failed
    org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSourceJdbc' defined in class path resource [spring/spring-backend-context-hibernate.xml]: Could not resolve placeholder 'database.driver.classname'
    	at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:272)
    	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:640)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:615)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

    If to remove ///

    Code:
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException:  (The system cannot find the path specified)
    	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:640)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:615)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: java.io.FileNotFoundException:  (The system cannot find the path specified)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(FileInputStream.java:120)
    	at java.io.FileInputStream.<init>(FileInputStream.java:79)
    	at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
    	at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
    	at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:124)
    	at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
    	at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
    	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
    	... 21 more
    2012-09-25 17:22:21,107 1 ERROR [main] context.ContextLoader - Context initialization failed
    Quote Originally Posted by Marten Deinum View Post
    Which isn't the same as a context-param in web.xml (I assume here you mean putting it in a context.xml file for tomcat) as in general those are JNDI resources...
    Why is not the same?

    http://tomcat.apache.org/tomcat-6.0-...g/context.html

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Why is not the same?
    as mentioned in my previous post a context-param isn't the same as a jndi resource!

    Edit: A forgot about the fact that you can also specify paramaeters in the context.xml which are available to the application. Must be having used to much JNDI .

    If the placeholder cannot be resolved it isn't in the file.

    To pin point your issue I suggested you enable debug/trace logging for the package that way you get information on what is happening (which file is being loaded etc.).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Jul 2010
    Posts
    21

    Default

    Quote Originally Posted by Marten Deinum View Post
    as mentioned in my previous post a context-param isn't the same as a jndi resource!
    Putting in parameter is context also is not a jndi resource. They are both the same.

    Code:
    <Context>
      ...
      <Parameter ../>
    Quote Originally Posted by Marten Deinum View Post
    If the placeholder cannot be resolved it isn't in the file.
    It is in the file for sure.

    Quote Originally Posted by Marten Deinum View Post
    To pin point your issue I suggested you enable debug/trace logging for the package that way you get information on what is happening
    Debugging is already enabled.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Then post your logging and not the stacktraces (also as mentioned you might need trace logging enabled).

    If it is in the file the placeholder will be found and replaced if it isn't you will get this message... So as mentioned I'm not 100% sure it is in the file you are trying to load...

    Putting in parameter is context also is not a jndi resource. They are both the same.
    See my edited post (before you posted this reply ).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Jul 2010
    Posts
    21

    Default

    Quote Originally Posted by Marten Deinum View Post
    Then post your logging and not the stacktraces (also as mentioned you might need trace logging enabled).
    Doesn't give any other result.

    Quote Originally Posted by Marten Deinum View Post
    If it is in the file the placeholder will be found and replaced if it isn't you will get this message... So as mentioned I'm not 100% sure it is in the file you are trying to load...
    But I'm sure it is in file (database.driver.classname=com.mysql.jdbc.Driver).

    Quote Originally Posted by Marten Deinum View Post
    See my edited post (before you posted this reply ).
    Sorry I haven't seen it)).

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Sigh... Read..

    Then post your logging
    Enabling or disabling logging doesn't do anything for the result... I want to see the logging not the stacktraces..

    As mentioned before it it either not in the file, it isn't matching in case or maybe in file encodings. Either way without seeing logging there is not much to do..
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •