Results 1 to 3 of 3

Thread: @Value and PropertyPlaceholderConfigurer

  1. #1
    Join Date
    Jun 2008
    Posts
    12

    Default @Value and PropertyPlaceholderConfigurer

    In Spring 2.5 we used a PropertyPlaceholderConfigurer to load up property files and inject the property values into beans using XML.

    We're trying to migrate to Spring 3.0 M2, and would like to use @Value to accomplish something similar. What would be the correct approach to do something similar to PropertyPlaceholderConfigurer?

    Can an @Value annotation access properties loaded by PropertyPlaceholderConfiguerer? Should we setup a separate PropertiesFactoryBean and inject from that (what's the syntax to do that?)?

    Any advice would be appreciated!
    -Jeremy

  2. #2
    Join Date
    Apr 2007
    Posts
    307

    Default

    You actually won't need PropertyPlaceholderConfigurer if you're using @Value.

    You could do just the following:

    Given a properties file on the classpath at 'com/acme/app/myprops.properties':
    Code:
    hostname=localhost
    foo=a
    bar=b
    In your XML, declare a utilroperties bean
    Code:
    <util:properties id="myProps" location="classpath:com/acme/app/myprops.properties"/>
    Then, within your classes:
    Code:
    @Value("#{myProps.hostname}")
    public void setHostName(String hostname) {
        this.hostname = hostname;
    }
    Spring EL can resolve any bean by its name. Since the content of your properties file is now available as a Spring-managed java.util.Properties bean named 'myProps', you can just refer to it by name.


    I've just added a unit test that captures the scenario above: https://fisheye.springsource.org/cha...k/trunk?cs=874

    Also note that in the forthcoming M3 release (drops this week!), you'll be able to use 'regular' ${...} tokens with @Value, and these will resolve using PropertyPlaceholderConfigurer / <contextroperty-placeholder/>. So you've got two options. The ${...} style is processed once and only once, during BeanFactoryPostProcessing, and the #{...} style is evaluated every time a bean is instantiated. So you can think of one as static and the other as dynamic.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  3. #3
    Join Date
    Jun 2008
    Posts
    12

    Default

    Thanks Chris.

    I apologize for the cross-posting, but had already created a topic in Core Container since I accidentally posted in JavaConfig at first.

    This topic contains a more detailed description of the problems I've encountered in using property files:
    http://forum.springframework.org/sho...lderconfigurer

    Could you please reply to the issues in that topic?

Posting Permissions

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