Home Tutorial Spring Spring3 Web applicationcontext.xml properties file


Share on Google+Share on Google+
applicationcontext.xml properties file
Posted on: October 15, 2010 at 12:00 AM
Learn how to use properties file in applicationcontext.xml.

applicationcontext.xml properties file

In this tutorial I will explain how you can use the properties defined in .properties file in your spring application

This type of configuration is very useful in web application development. You can put configurations for production, development and test environments is separate properties files. You can easily configure the environment by just changing the properties file.

Suppose you are developing an email sending component for your web application. You may create following property file:

File: application.properties

#Email Configuration

And following mailer bean:

package users.components.utils;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class MailerBean{

	private String adminEmail;
	private String mailServerIP;
	public String getAdminEmail() {
		return adminEmail;
	public void setAdminEmail(String adminEmail) {
		this.adminEmail = adminEmail;
	public String getMailServerIP() {
		return mailServerIP;
	public void setMailServerIP(String mailServerIP) {
		this.mailServerIP = mailServerIP;

	public void sendEmail(String receipent, String message){

		//Code to send email here


Now in the applicationContext.xml file you can add the following code to use the properties defined in the  application.properties file:

 <!--Bean to load properties file -->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:application.properties"/>

<bean id="MailerBean" class="users.components.utils.MailerBean">
<property name="adminEmail" value="${admin.email}" />
<property name="mailServerIP" value="${mail.server.ip}" />

Now you can get the MailerBean in your program to use the Mailer Bean functionality.



Related Tags for applicationcontext.xml properties file:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 15, 2010

Recommend the tutorial

Advertisements Advertisements