A property resource configurer that resolves placeholders in bean property values of
context definitions. It pulls values from a properties file into bean definitions.
Example :
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh.core</groupId>
<artifactId>springexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringExample</name>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<spring.version>3.1.2.RELEASE</spring.version>
</properties>
</project>
Create a simple Spring Bean with Properties
package com.mahesh.spring;
public class PropertyRead {
private String url;
private String driverName;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName= driverName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public class PropertyRead {
private String url;
private String driverName;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName= driverName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
ResourcesBundle.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sample
jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/sample
jdbc.username=root
jdbc.password=root
Define the PropertyPlaceholderConfigurer
<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-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>ResourceBundle.properties</value> </property> </bean> <bean id="reader" class="com.mahesh.spring.PropertyRead"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> </beans>
Run Application
package com.mahesh.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mahesh.spring.PropertyRead;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PropertyRead read = (PropertyRead) context.getBean("reader");
read.getUrl();
read.getPassword();
}
}
No comments:
Post a Comment