Sunday, 30 March 2014

Using Template in Defining Beans

To understand templates in spring framework, consider a database connection class. This class requires certain parameters viz.

  • Database name, 
  • Server IP address, 
  • Username and 
  • Password.
As these properties must be present in all beans of database connection class, hence we can specify these properties with default values in a spring template as follows.

<bean id="sqlTemplate" abstract="true"> 
 <property name="database" value="Oracle"/>
 <property name="server" value="127.0.0.1"/> 
 <property name="username" value="scott"/> 
 <property name="password" value="tiger"/> 
</bean>

Note that in template we specify abstract="true" and do not provide a class. Now the beans of ORACLEDBConnection class can use this as template by specifying parent attribute value as the bean-id sqlTemplate.

<bean id="dbConnection" class="org.techmight.ORACLEDBConnection" parent="sqlTemplate"/>

Example

spring-config.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-3.0.xsd">

 <bean id="sqlTemplate" abstract="true"> 
  <property name="database" value="Oracle"/>
  <property name="server" value="127.0.0.1"/> 
  <property name="username" value="scott"/> 
  <property name="password" value="tiger"/> 
 </bean>
 
 <bean id="dbConnection" class="org.techmight.ORACLEDBConnection" parent="sqlTemplate"/>

 <bean id="dbConnection2" class="org.techmight.ORACLEDBConnection" parent="sqlTemplate">  
  <property name="server" value="112.36.12.100"/>
  <property name="username" value="techmight"/> 
  <property name="password" value="passwd@123"/>
 </bean>
</beans>

ORACLEDBConnection.java
package org.techmight;

public class ORACLEDBConnection {

 private String database;
 private String server;
 private String username;
 private String password;

 public String getDatabase() {
  return database;
 }

 public void setDatabase(String database) {
  this.database = database;
 }
 
 public String getServer() {
  return server;
 }

 public void setServer(String server) {
  this.server = server;
 }

 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;
 }
}

MainApp.java
package org.techmight;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
 public static void main(String[] args) {
  AbstractApplicationContext context = new ClassPathXmlApplicationContext(
    "spring-config.xml");
  ORACLEDBConnection db = null;
  
  db = (ORACLEDBConnection) context.getBean("dbConnection");
  System.out.println("Default Database Connection params - ");
  System.out.println("Database name: " + db.getDatabase());
  System.out.println("Server IP : " + db.getServer());
  System.out.println("Username: " + db.getUsername());
  System.out.println("Password: " + db.getPassword());
  
  System.out.println("\nTechMight Database Connection params - ");
  db = (ORACLEDBConnection) context.getBean("dbConnection2");
  System.out.println("Database name: " + db.getDatabase());
  System.out.println("Server IP : " + db.getServer());
  System.out.println("Username: " + db.getUsername());
  System.out.println("Password: " + db.getPassword());
 }
}

Output
Default Database Connection params -
Database name: Oracle
Server IP : 127.0.0.1
Username: scott
Password: tiger

TechMight Database Connection params -
Database name: Oracle
Server IP : 112.36.12.100
Username: techmight
Password: passwd@123

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.