Skip to main content

Spring Note: PropertyPlaceholderConfigurer

Often there are Spring-related configuration options which don't belong inside the final jar/war for a Java application. Either because the options are different for each deployment environment or because the value of the option is sensitive (passwords). The PropertyPlaceholderConfigurer class attempts to remedy the management and injection of the values for these options. It reads one or more property files, optionally using system properties for defaults, and uses the values found to fill property placeholders of the form ${property name} found in the ApplicationContext in which the configurer is defined.

Advantages:
  • It provides a mechanism for externalizing configuration so that each environment can be configured differently without re-building the application.
  • It provides a mechanism for injecting test properties at test-time without incurring much development overhead.
  • Using the classpath:<file> pseudo-uri enables developers to easily include default properties while still allowing users to over-ride the defaults by adding the location of a properties file to the application's classpath.
  • The PropertyPlaceholderConfigurer is extensible and provides a method, convertPropertyValue(String), which can be over-ridden in a sub-class to handle the encryption of properties.
Disadvantages:
  • The configurer class does not re-load at a regular interval. Changing the properties managed by the configurer requires a restart of the application using it. This can be overcome in some limited situations through a custom sub-class, but for the most part, is not really supported by the Spring container.
  • Only Spring-managed beans can take advantage of these properties.

Sample

config.xml

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
    <value>classpath:project.properties</value>
  </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driver}"/>
  <property name="url" value="${jdbc.url}"/>
</bean>

project.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:mydb

Comments

Popular posts from this blog

Using MonoDevelop to Create an ASP.NET Web Service

NOTE: instructions below are for MonoDevelop 2.6 Beta 2 - built on 2011-04-06 03:37:58+0000Getting StartedCreate a new ASP.NET Web Application in MonoDevelop: From the menu, select: File → New → Solution…Expand C#.Select ASP.NET → Web Application.Enter a name for the ASP.NET project that will be created in the solution in Name:.Change the root location for the solution in Location:, if desired.Change the name of the root solution in Solution Name:, if desired.The Results – IWhat you have after executing the new ASP.NET Web Application project wizard is a solution containing one ASP.NET Web Application project. In the default project view in MonoDevelop, you'll find the following items: Default.aspx – This is the default web form rendered and presented in the browser when http://<server>:<port>/ is accessed. Default.aspx.cs – This C# file contains the developer-created common code and event handlers which can be used to affect the processing of the form.Default.aspx.des…

Testing Toolbelt: SpringJUnit4ClassRunner

The org.springframework.test.context.junit4.SpringJUnit4ClassRunner class is another implementation of the JUnit TestRunner class which is used to enable various features of Spring for every run of the test class and every test within it. To use the features provided by the SpringJUnit4ClassRunner class, you need to mark the class using the RunWith annotation using SpringJUnit4ClassRunner as its parameter.In addition to the custom test runner, you will want to mark the class with the ContextConfiguration annotation. The ContextConfiguration annotation is used to mark classes which will automatically read a Spring configuration file and use it to create an ApplicationContext. By default, this file located at <package path>/<test class name>-context.xml. Use the locations argument to over-ride.The ApplicationContext used by the Spring-integrated test will only be loaded once for the whole test class. This behavior can be over-ridden by annotating a test method with the Dirti…