Home Tutorial Spring Spring3 Web applicationcontext.xml properties file

 
 

applicationcontext.xml properties file
Posted on: October 15, 2010 at 12:00 AM
Advertisement
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.

 

Advertisement

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
 

 

 

DMCA.com