J3 Limited
Introduction to Spring

Runtime Control Using a Properties File

Organisations often have one team to develop software and another team to deploy software.

The deployment process needs to be kept as simple as possible. The runtime environments used by the development teams and deployment teams are rarely the same.

One solution is to store application configuration information in property files that the application loads when launched. In this way one file can be used by the development team, and another version is used by the deployment team.

The project developed in the previous section is used to add a property file.

Adding Property File Support

A new file called SpringConsole.properties is created in the res folder.


The contents of the properties file, have a property called "printer" with the value "prodPrint". This is how we configure the application using properties files.

The application context xml file is edited to add property file support.

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


<context:property-placeholder location="classpath:SpringConsole.properties"/>

<bean id="devPrint" class="springtutorial.DevelopmentPrinter"/>
<bean id="prodPrint" class="springtutorial.ProductionPrinter"/>
<alias alias="printer" name="${printer}"/>

<bean id="mainApp" class="springtutorial.Main">
<property name="springWiredProperty"
value="This value is defined in the Spring Context"/>
<property name="print" ref="printer"/>

In Spring's context name space is the element property-placeholder. his element is used to load the properties file SpringConsole.properties from the classpath.

The printer alias is updated to use the property read from the file (${printer}).

The application is run, and the console confirms that the properties file was read successfully, the printer class instantiated and used is the ProductionPrinter class, instead of the DevelopmentPrinter class seen previously.

Production: It works This value is defined in the Spring Context

The source code for the project in this section can be found here.


This section of the Introduction to Spring has shown how to use a property file to control the behaviour of an application. The "printer" entry in the property file is given the name of a bean to use for printing text (prodPrint), changing the value from prodPrint to devPrint will change the application's behaviour.

Another good use for property files is to store database connection details an application uses (database url, username, password).

The next section takes a look at an important topic in Spring: bean scopes.

  Copyright © 2006 J3 Ltd Permission is granted to reproduce material on this page, on the condition that a reference to "WWW.J3Ltd.com" is given as the source of the material.