Announcement Announcement Module
No announcement yet.
@Value and PropertyPlaceholderConfigurer Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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!

  • #2
    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/':
    In your XML, declare a utilroperties bean
    <util:properties id="myProps" location="classpath:com/acme/app/"/>
    Then, within your classes:
    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:

    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.


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

      Could you please reply to the issues in that topic?