Announcement Announcement Module
Collapse
No announcement yet.
Setting a prop from a value in a Properties object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting a prop from a value in a Properties object

    Hi I have an object in my beanfactory used solely for configuration parameters, it is an object of type Properties. So I set stuff on it like "mail.hostname" and what not. Anyway within the same factory I would like to be able to extract specific values from the properties object to use to set up my other beans. Is this possible? IE i need to do something like:

    Code:
        <property name="mail.hostname">
          <ref bean="propertiesConfigObject"><key>mail.hostname</key></ref>
        </property>
    I know the above is obviously not valid but i am wondering if there is something similar to that that i can do?

    Thanks!

  • #2
    Check out the javadoc for PropertyPlaceHolderConfigurer,
    which allows ant-style placeholder replacement from an external properties file (or from a Properties bean in the context).

    Regards,

    Joris

    Comment


    • #3
      Another possibility would be to apply a MethodInvokingFactoryBean. It should be able to instrument the get-method to return the value you need. See the reference manual for examples on using this feature.

      However, if possible, I would also suggest to use Joris' solution.

      Regards,
      Andreas

      Comment


      • #4
        I very rarely disagree with Andreas Senft but I gotta speak out Reusability over writing it yourself wins every time Particularly when the resuable component comes from the excellent Spring developers

        Comment


        • #5
          Does anyone know if the properties file can be in XML or are you forced to use the default properties type?

          Comment


          • #6
            AFAIK, when using PropertyPlaceholderConfigurer, you have to adhere to the default properties format (as defined for java.util.Properties).

            Regards,
            Andreas

            Comment


            • #7
              What would be the best way to handle this PropertyConfigurer in a webapp? I am currently using the ContextLoaderListener to create my webapp context. Should I just create another listener that will modify the created factory?

              Comment


              • #8
                No. Normally the ApplicationContext will automatically detect the PropertyPlaceHolderConfigurer (which is a post processor) and execute the post processing sequence for the other beans.

                So it's just a declarative issue: Specify the properties file and place your replacement-tags in your application context. Usage details should be available in the Javadoc and the reference manual.

                Regards,
                Andreas

                Comment


                • #9
                  Ok so here is what I've got so far:

                  Code:
                    <bean id="propertyConfigurator" class="PropertyPlaceholderConfigurer">
                      <property name="location">
                        <bean class="org.springframework.web.context.support.ServletContextResource">
                          <constructor-arg index="0">&#40;servlet context???&#41;</constructor-arg>
                          <constructor-arg index="1"><value>/WEB-INF/spring/tecasConfig.xml</value></constructor-arg> 
                        </bean>
                      </property>
                    </bean>
                  I'm in a bit of a predicament though, my path to the file is servlet relative, so I'm use the ServletContextResource to set the location of my properties file, however the Resource also needs my servlet context. I'm using a WebApplicationContext, does it explicitly make the servlet context available as a bean within it anywhere that i could get it for this?

                  Comment


                  • #10
                    Ok got it. Here is what i put into my spring beans xml file:

                    Code:
                      <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                        <property name="location">
                          <value>/WEB-INF/spring/tecasConfig.xml</value>
                        </property>
                      </bean>
                    And you CAN have your properties file in the new xml format if you use the latest spring, 1.2.3

                    Comment

                    Working...
                    X