Friday, 28 October 2011

Autowiring Property Values into Spring Beans

Most people know that you can use @Autowired to tell Spring to inject one object into another when it loads your application context. A lesser known nugget of information is that you can also use the @Value annotation to inject values from a property file into a bean’s attributes.

To demonstrate this requires a few bits and pieces, including a property file:


In this example, I’ve got a some simple datasource connection details for an Oracle database, which will be injected into a fake datasource class AutowiredFakaSource:

public class AutowiredFakaSource {

private String driverClassName;

private String url;

private String userName;

private String password;

private String tmpDir;

public AutowiredFakaSource() {

public String execute() {

System.out.println("Execute FakaSource");
return "A Result";

public String getDriverClassName() {
return driverClassName;

public String getUrl() {
return url;

public String getUserName() {
return userName;

public String getPassword() {
return password;

public String getTmpDir() {
return tmpDir;

In terms of this blog, AutowiredFakaSource doesn’t really need to do anything, it just has to be a bean with some attributes. The crux of the whole thing lies in the @Value annotations:


...which shows that a value from a property file is referenced using its name and the ${} notation.

The JUnit test below demonstrates that all this works okay.

public void testAutowiredPropertyPlaceHolder() {

System.out.println("Autowired Property PlaceHolder Test.");
    ApplicationContext ctx =
new ClassPathXmlApplicationContext("autowired_property_place_holder.xml");

    AutowiredFakaSource fakeDataSource = ctx.getBean

("oracle.jdbc.OracleDriver", fakeDataSource.getDriverClassName());
("jdbc:oracle:thin:@on-the-beach:1521:mysid", fakeDataSource.getUrl());
("john", fakeDataSource.getUserName());
("lennon", fakeDataSource.getPassword());
    String expected = System.getProperty
(expected, fakeDataSource.getTmpDir());

On last thing to note is the XML configuration file. In the example below, you can see that the XML contains two entries. The first is the PropertyPlaceholderConfigurer class that loads the properties from the file and second is the

<context:component-scan base-package="miscellaneous.property_placeholder" />

XML element that enables autowiring.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

 <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
 <!-- (in this case, JDBC-related settings for the dataSource definition below) -->
 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <!-- List other property files here -->
    <!-- value></value -->

 <!-- Enable autowiring -->
  <context:component-scan base-package="miscillaneous.property_placeholder" /> 


Finally, eagle eyed readers will have spotted that the @Value annotation also allows you to load system properties. In this example I’ve loaded the Java temp directory path using:


…and then tested it using the final assert in the unit test:


Anonymous said...

Saved my day !!
Thank you very much :)


Mustafa Nural said...

Enabling autowiring can easily be overlooked. Thanks for specifically mentioning this.