Results 1 to 6 of 6

Thread: Environment variable in a bean

  1. #1
    Join Date
    Jan 2013
    Posts
    3

    Default Environment variable in a bean

    Hi!

    I'm new with spring and new in this forum.

    I already searched a lot for the use of environment variable in spring but I still couldn't make it work.
    I have a Java application in which I have a bean like this:

    Code:
    <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-2.0.xsd">
    <bean id="dev" class="org.test.bean.ConfigBean">
    		<property name="propsFile" value="${jboss.server.config.url}/props/a.properties" />
    	</bean>
    </beans>
    Whenever I try to use the bean like this, it doesn't work.
    So I read about the PropertyPlaceholderConfigurer, and I tried something like this:

    Code:
    <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-2.0.xsd">
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
             <property name="location" value="classpath:app.properties" />
             <property name="ignoreResourceNotFound" value="false" />
             <property name="searchSystemEnvironment" value="true" />
             <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
          	</bean>
            <bean id="dev" class="org.test.bean.ConfigBean">
    		<property name="propsFile" value="${test}" />
    	</bean>
    </beans>
    and inside the app.properties I have:
    test=${jboss.server.config.url}/props/a.properties

    But whenever I try to load the propsFile value in my java project it gets only the ${test}.
    What am I missing?!

    Thanks

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

    Default

    And how are you loading your configuration?! Also what is it exactly that you want (not really clear to me)...

    Do you want to value as is then I don't really see what is wrong with the initial configuration, unless you have somewhere a PropertyPlaceholderConfigurer which tries to replace the value.
    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
    Jan 2013
    Posts
    3

    Default

    Sorry if I wasn't clear, what I want is to get the propsFile value (it means the path: ${jboss.server.config.url}/props/a.properties ) in my Java Application.
    But what I get is nothing but ${test}, literally this string and not the path.

    So my mistake is probably in the java side. How can I return the right meaning of ${test} to my Java application?

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

    Default

    If you want that, and as I mentioned, the first code block you posted should work... No need to mess around with a PPC.
    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
    Jan 2013
    Posts
    3

    Default

    but in the first code block it doesn't replace ${jboss.server.config.url} for the jboss path
    but thanks for the help, I'll do the replacement directly on the java application.
    Last edited by anonimo; Jan 28th, 2013 at 06:34 AM.

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

    Default

    Neither will it do in the second...

    If you want to replace environment variables (assuming that you are on Spring 3.1 or higher) you are using the wrong placeholder configurer. Use a PropertySourcesPlaceholderConfigurer. I also strongly suggest the use of the context namespace instead of using the classes directly.

    Code:
    <context:property-placeholder />
    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
  •