Announcement Announcement Module
Collapse
No announcement yet.
Environment variable in a bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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?

      Comment


      • #4
        If you want that, and as I mentioned, the first code block you posted should work... No need to mess around with a PPC.

        Comment


        • #5
          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, 06:34 AM.

          Comment


          • #6
            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 />

            Comment

            Working...
            X