Simplified Property Management with Spring

Discussions

News: Simplified Property Management with Spring

  1. Simplified Property Management with Spring (2 messages)

    Spring has always tried to be as transparent as possible when it comes to working with properties. Before Spring 3.1 however, the mechanism of both adding new sources of properties into Spring as well as actually using the properties wasn’t as flexible and as robust as it could be.

    Starting with Spring 3.1, the new Environment and PropertySource abstractions simplify working with properties. The default Spring Environment now contains two property sources: the System properties and the JVM properties, with the System properties having precedence.

    For more information on the unified property management in Spring 3.1, see this official article.

    Registering Properties via the XML namespace

    In an XML configuration, new property files can be made accessible to Spring via the following namespace element:

    Registering Properties via Java Annotations

    Spring 3.1 also introduces the new @PropertySource annotation, as a convenient mechanism of adding property sources to the environment. This annotation is to be used in conjunction with Java based configuration and the @Configuration annotation:

    @PropertySource("classpath:/com/foo/foo.properties")

    As opposed to using XML namespace element, the Java @PropertySource annotation does not automatically register a PropertySourcesPlaceholderConfigurer with Spring. Instead, the bean must be explicitly defined in the configuration to get the property resolution mechanism working. The reasoning behind this unexpected behavior is by design and documented on this issue.

    Behind the Scenes – the Spring Configuration

    Before Spring 3.1

    Since the convenience of defining property sources with annotations was introduced in the recently released Spring 3.1, XML based configuration was necessary in the previous versions.

    Defining a XML element automatically registers a new PropertyPlaceholderConfigurer bean in the Spring Context. This is also the case in Spring 3.1 if, for backwards compatibility purposes, the XSD schemas are not updated to the 3.1 versions.

    In Spring 3.1

    From Spring 3.1 onward, the XML will no longer register the old PropertyPlaceholderConfigurer but the newly introduced PropertySourcesPlaceholderConfigurer. This replacement class was created be more flexible and to better interact with the newly introduced Environment and PropertySource mechanism; it should be considered the standard for 3.1 applications.

    Read the rest of the article or go to the REST with Spring series

    Threaded Messages (2)

  2. Dynamic updates.[ Go to top ]

    Does it provide easy way of dynamically updating bean properties when external property source (such as property value in property file or database table) changes without reloading the application context?

  3. Dynamic updates.[ Go to top ]

    Dynamic updating of properties is not something Spring supports out of the box. There are however custom solutions for this (http://stackoverflow.com/questions/6594213/dynamic-spring-properties), and you can always roll your own (not recommended).

    Thanks for the feedback.

    Eugen.