Home > Java > Spring PropertyPlaceholderConfigurer

Spring PropertyPlaceholderConfigurer

February 20, 2014 Leave a comment Go to comments

Berikut ini example penggunaan PropertyPlaceholderConfigurer class, dalam case ini untuk koneksi database MySql.

db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springdb
jdbc.username=root
jdbc.password=

Koneksi class

public class Koneksi {
private DriverManagerDataSource driverManager;

public void setDriverManager(DriverManagerDataSource driverManager) {
this.driverManager = driverManager;
}

public DriverManagerDataSource getDriverManager() {
return driverManager;
}

public void displayInfo(){
String url  = getDriverManager().getUrl();
String user = getDriverManager().getUsername();
String pass = getDriverManager().getPassword();
System.out.println("URL: "+url);
System.out.println("User: " +user);
System.out.println("Password: "+pass);
}

}

Bean Configuration

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>db.properties</value>
</property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<bean id="connection" class="com.util.Koneksi">
<property name="driverManager">
<ref bean="dataSource"></ref>
</property>
</bean>

Result

Capture

Capture

Referensi : dari berbagai sumber.
Semoga bermanfaat.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: