In
my previous post, I explained how to load a property file using PropertyPlaceholderConfigurer.
In real world scenario, an application requires to maintain multiple property
files. By using PropertyPlaceholderConfigurer class, you can load multiple
property files.
Following
statement loads the property files mail.properties, db.properties.
<bean id="dbProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:mail.properties</value> <value>classpath:db.properties</value> </list> </property> </bean>
Following
is the complete working application.
mail.properties
app.contactUs=krishna@krishna.com app.reportTo=help@krishna.com
db.properties
db.hostName=abc.xyz.com db.port=8081 db.userName=krish@gmail.com db.password=secret
Database.java
package com.sample.pojo; public class Database { private String hostName; private String port; private String userName; private String password; public String getHostName() { return hostName; } public void setHostName(String hostName) { this.hostName = hostName; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Database [hostName=").append(hostName).append(", port=").append(port).append(", userName=") .append(userName).append(", password=").append(password).append("]"); return builder.toString(); } }
Application.java
package com.sample.pojo; public class Application { private String contactUs; private String mailTo; public String getContactUs() { return contactUs; } public void setContactUs(String contactUs) { this.contactUs = contactUs; } public String getMailTo() { return mailTo; } public void setMailTo(String mailTo) { this.mailTo = mailTo; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Application [contactUs=").append(contactUs).append(", mailTo=").append(mailTo).append("]"); return builder.toString(); } }
myConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dbProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:mail.properties</value> <value>classpath:db.properties</value> </list> </property> </bean> <bean id="database" name="database" class=" com.sample.pojo.Database"> <property name="hostName" value="${db.hostName}" /> <property name="port" value="${db.port}" /> <property name="userName" value="${db.userName}" /> <property name="password" value="${db.password}" /> </bean> <bean id="application" name="application" class=" com.sample.pojo.Application"> <property name="contactUs" value="${app.contactUs}" /> <property name="mailTo" value="${app.reportTo}" /> </bean> </beans>
HelloWorld.java
package com.sample.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sample.pojo.Application; import com.sample.pojo.Database; public class HelloWorld { public static void main(String args[]) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" }); Database database = context.getBean("database", Database.class); Application application = context.getBean("application", Application.class); System.out.println(database); System.out.println(application); ((ClassPathXmlApplicationContext) context).close(); } }
Run
HelloWorld.java, you can able to see following output.
Database
[hostName=abc.xyz.com, port=8081, userName=krish@gmail.com, password=secret]
Application
[contactUs=krishna@krishna.com, mailTo=help@krishna.com]
No comments:
Post a Comment