How to read property file in spring using xml based configuration file
This page shows how to load property file from the classpath using xml based configuration. Declare your property file in your
xml based configuration file using "context:property-placeholder" tag, and refer property key any where in the xml based configuration file using
${db.host.url} syntax.
package com.java2novice.beans;
public class MyDbConfig {
private String dbHost;
private String dbPort;
private String dbService;
private String dbUrl;
private String dbPassword;
public String getDbHost() {
return dbHost;
}
public void setDbHost(String dbHost) {
this.dbHost = dbHost;
}
public String getDbPort() {
return dbPort;
}
public void setDbPort(String dbPort) {
this.dbPort = dbPort;
}
public String getDbService() {
return dbService;
}
public void setDbService(String dbService) {
this.dbService = dbService;
}
public String getDbUrl() {
return dbUrl;
}
public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}
public String getDbPassword() {
return dbPassword;
}
public void setDbPassword(String dbPassword) {
this.dbPassword = dbPassword;
}
@Override
public String toString(){
return dbHost+"|"+dbPort+"|"+dbService+"|"+dbUrl+"|"+dbPassword;
}
}
|
db.properties file:
db.host.url=db.java2novice.com
db.port.number=1521
db.service.name=test_db
db.user=java2novice
db.password=testme!*
|
Xml based configuration file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:db.properties" />
<bean id="dbConfig" class="com.java2novice.beans.MyDbConfig">
<property name="dbHost" value="${db.host.url}"/>
<property name="dbPort" value="${db.port.number}"/>
<property name="dbService" value="${db.service.name}"/>
<property name="dbUrl" value="${db.user}"/>
<property name="dbPassword" value="${db.password}"/>
</bean>
</beans>
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.MyDbConfig;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
MyDbConfig dbConfig = (MyDbConfig) context.getBean("dbConfig");
System.out.println(dbConfig.toString());
context.close();
}
}
|
You can specify multiple property file seperated by comma:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:db.properties,app.properties" />
<bean id="dbConfig" class="com.java2novice.beans.MyDbConfig">
<property name="dbHost" value="${db.host.url}"/>
<property name="dbPort" value="${db.port.number}"/>
<property name="dbService" value="${db.service.name}"/>
<property name="dbUrl" value="${db.user}"/>
<property name="dbPassword" value="${db.password}"/>
</bean>
</beans>
|
|