DSBean.java:
package com.lnn.props;
public class DSBean {
private String url = null;
private String driver = null;
private String username = null;
private String password = null;
public String getUrl()
{
return url;
}
public void setUrl(String
url) {
this.url = url;
}
public String
getDriver() {
return driver;
}
public void
setDriver(String driver) {
this.driver = driver;
}
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;
}
}
database.properties:
database.url=jdbc:hsqldb:Training
database.driver=org.hsqldb.jdbcDriver
database.username=appUser
database.password=password
props.xml:
<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-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="database.properties" />
</bean>
<bean id="dsBean"
class="com.lnn.props.DSBean">
<property name="url"
value="${database.url}"/>
<property name="driver"
value="${database.driver}"/>
<property name="username"
value="${database.username}"/>
<property name="password"
value="${database.password}"/>
</bean>
</beans>
Test.java:
package com.lnn.props;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[]
args) {
ApplicationContext
context = new ClassPathXmlApplicationContext("props.xml");
DSBean
bean = context.getBean("dsBean", DSBean.class);
System.out.println(bean.getDriver());
System.out.println(bean.getUrl());
System.out.println(bean.getUsername());
System.out.println(bean.getPassword());
}
}
No comments:
Post a Comment