Spring 3 configuration : inject .properties values into annotation driven beans

I was looking for a way to inject values of my .properties files into a annotation driven bean. I had configured a PropertyPlaceholderConfigurer but you cannot inject values of your .properties with this bean. The solution is to configure a PropertiesFactoryBean with it.


  • use the .properties file in a PropertyPlaceholderConfigurer
  • ability to inject values of this .properties file in annotation driven beans (@Controller, @Service, etc)
  • Avoid spring crashing if a .properties file is not found so we can play with .properties overriding behavior.

first, add these lines to your applicationContext.xml :

<beans:bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <beans:property name="singleton" value="true" />
  <beans:property name="ignoreResourceNotFound" value="true" />
  <beans:property name="locations">

<beans:bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <beans:property name="properties" ref="appProperties" />
  <beans:property name="ignoreUnresolvablePlaceholders" value="true"/>
  <beans:property name="ignoreResourceNotFound" value="true"/>

  • You can see that we can put several .properties file (in the classpath or in an external folder). Here, ${config.dir} is a java environment constant passed to the JVM with the argument -Dconfig.dir=”C:/CONFIG_DIR” The external .properties file will override the classpath .properties.
  • The ignoreUnresolvablePlaceholders and ignoreResourceNotFound properties are used to avoid Spring crashing if one of the .properties file is not found

then you can inject values from your .properties in your bean classes :

 public class MyDatabaseRepository {
 private String databaseName;

 private String databaseLogin;


And in your spring XML files :

<beans:bean id="mySQLDataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <beans:property name="url">
  <beans:property name="username">

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>