Home Tutorial Spring Spring3 Web applicationcontext.xml properties file

 
 

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
admin.email=deepak@roseindia.net
mail.server.ip=localhost
 

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>

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

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

 

Related Tags for applicationcontext.xml properties file:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.