Results 1 to 9 of 9

Thread: context:property-placeholder does not work

  1. #1
    Join Date
    Sep 2008
    Posts
    5

    Exclamation context:property-placeholder does not work

    Hi, I tried hard a lot of hours maybe someone can help me.
    I simply want to outsource configuration.

    I have the following in my applicationContext.xml
    Code:
    <context:property-placeholder location="/resources/jacavi.properties"/>
    
    <bean id="testBean" class="java.lang.String">
    	 <constructor-arg><value>${test}</value></constructor-arg>
    </bean>
    as location i also tried "file:/resources/jacavi.properties", "javavi.properties", "file:jacavi.properties", "classpath*:jacavi.properties", "classpath*:/resources/jacavi.properties", and so on.

    my jacavi.properties looks like:
    test=hello world

    when I get my testBean and print it out it gives me ${test} and not the expected hello world.

    what am I doing wrong?


    cheers Flo

  2. #2
    Join Date
    Sep 2004
    Posts
    602

    Default

    Quote Originally Posted by flosk8 View Post
    Hi, I tried hard a lot of hours maybe someone can help me.
    I simply want to outsource configuration.
    Code:
    <context:property-placeholder
    		location="classpath:application.properties" />
    And then add the directory that the properties file is in to your apps classpath
    Last edited by Paul Newport; Sep 19th, 2008 at 04:25 AM. Reason: added code tag

  3. #3
    Join Date
    Sep 2008
    Posts
    5

    Default

    Quote Originally Posted by Paul Newport View Post
    Code:
    <context:property-placeholder
    		location="classpath:application.properties" />
    And then add the directory that the properties file is in to your apps classpath
    Hi Paul no effect. still the same. I still got only the string ${test} on my console. I added the resources folder where my applicationContext.xml and my jacavi.properties lies to the classpath but it still doesnot work any ideas?

    cheers

  4. #4
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by flosk8 View Post
    Hi Paul no effect. still the same. I still got only the string ${test} on my console. I added the resources folder where my applicationContext.xml and my jacavi.properties lies to the classpath but it still doesnot work any ideas?

    cheers
    It's not possible to get the behavior you described with the mentioned config because:
    1. If PPC is not possible to load a properties, exception is thrown during context instantiation. So, you application.properties resource is found and properties are loaded;
    2. If PPC is not able to resolve a bean property, exception is thrown. Hence, it's not allowed for bean to have an unresolved property at ${...} format if PPC is defined;

  5. #5
    Join Date
    Sep 2008
    Posts
    5

    Default

    Quote Originally Posted by denis.zhdanov View Post
    It's not possible to get the behavior you described with the mentioned config because:
    1. If PPC is not possible to load a properties, exception is thrown during context instantiation. So, you application.properties resource is found and properties are loaded;
    2. If PPC is not able to resolve a bean property, exception is thrown. Hence, it's not allowed for bean to have an unresolved property at ${...} format if PPC is defined;
    Hi Denis, what is PPC and how can i define it. I dont have any exceptions and i could load my bean but it gives my ${test} and not the value test is mapping to. If i write ${somthingbad} I also get no error (something bad isnot part of application.properties).

    cheers

  6. #6
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    PPC is a PropertyPlaceHolderConfigurer

    You can do the following - define a PropertyPlaceHolderConfigurer bean directly at the config (i.e. don't use <contextroperty-placeholder> element). You can find an example of how to do that at the reference - 3.7.2.1. Example: the PropertyPlaceholderConfigurer. If you still have the same behavior (the bean holds ${..} property), check PPC content via debugger. It should contain resolved Properties object with resolved properties.

  7. #7
    Join Date
    Sep 2008
    Posts
    5

    Default

    Quote Originally Posted by denis.zhdanov View Post
    PPC is a PropertyPlaceHolderConfigurer

    You can do the following - define a PropertyPlaceHolderConfigurer bean directly at the config (i.e. don't use <contextroperty-placeholder> element). You can find an example of how to do that at the reference - Example: the PropertyPlaceholderConfigurer[/URL]. If you still have the same behavior (the bean holds ${..} property), check PPC content via debugger. It should contain resolved Properties object with resolved properties.
    Hi denis,

    I tried the following.

    Code:
    <bean id="propLoader" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        	<property name="locations">
            	<value>classpath:test.properties</value>
        	</property>
    	</bean>
    test.properties does not exist but i dont get an exception or annything else.

    with the following in debugging
    Code:
    <bean id="propLoader" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        	<property name="locations">
            	<value>classpath:jacavi.properties</value>
        	</property>
    	</bean>
    localProperties of propLoader is null and in all the other members I can not find the stuff of my properties file.

    Still anny ideas?

    cheers

    Flo

  8. #8
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    How're you building the container? Do you use ApplicationContext or BeanFactory?

  9. #9
    Join Date
    Sep 2008
    Posts
    5

    Default

    Quote Originally Posted by denis.zhdanov View Post
    How're you building the container? Do you use ApplicationContext or BeanFactory?
    Thank you very much ma man we user the BeanFactory now we use ApplicationContext and it works fine.

    cheers

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
  •