Recent Posts

Tuesday, 25 June 2019

Profiling in Spring

     In general, in project life cycle, we have to perform development, testing, production mainly. At each and every phase of project life cycle we may use databases, debugging tools, testing tools etc. with different configuration details.
     In general, in all project life cycle phases we will provide the required configuration details manually, it may increase problems to the applications, in this context, Spring3.x version has provided an automated solution in order to provide the corresponding configuration details w.r.t the life cycle phases, for this, Spring framework has provided "Profiling" feature.
Note: In Project Implementation, we may use database configuration details like data source names, connection pool names, JNDI names in Server etc.
If we want to implement Profiling in Spring applications then we have to use the following steps.
1. Create a separate spring configuration file for each and every phase of the project Lifecyle with the respective configuration details.
E.g.
spring-context-development.xml
spring-context-testing.xml
spring-context-production.xml
Note: spring configuration XML File format must be <FileName>-phase_Name.xml

2. In all Spring Configuration files we must provide "profile" attribute in <beans> tag with the respective life cycle phase name.
E.g.
spring-context-development.xml
------------------------------
<beans profile="development">
     ------
</beans>
spring-context-production.xml
------------------------------
<beans profile="production">
     ------
</beans>
3. Provide project lifecycle phase in System property with the key "spring.profiles.active" in Main Application.
E.g.
System.setProperty("spring.profiles.active", "development");

4. In main Application, we have to use GenericXmlApplicationContext as Container and load all the spring configuration files with ctx.load(--,--,--); method and refresh context.
GenericXmlApplicationCointext context = new GenericXmlApplicationContext();
context.load("spring-context-development.xml", "spring-context-production.xml");
context.refresh();
package com.ashok.spring.core.bean.profiling.beans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class AccountBean {
   private String driverClass;
   private String driverURL;
   private String dbUserName;
   private String dbPassword;

   public String getDriverClass() {
      return driverClass;
   }

   public void setDriverClass(String driverClass) {
      this.driverClass = driverClass;
   }

   public String getDriverURL() {
      return driverURL;
   }

   public void setDriverURL(String driverURL) {
      this.driverURL = driverURL;
   }

   public String getDbUserName() {
      return dbUserName;
   }

   public void setDbUserName(String dbUserName) {
      this.dbUserName = dbUserName;
   }

   public String getDbPassword() {
      return dbPassword;
   }

   public void setDbPassword(String dbPassword) {
      this.dbPassword = dbPassword;
   }

   public void listAccounts() {
      try {
         Class.forName(driverClass);
         Connection con = DriverManager.getConnection(driverURL, dbUserName, dbPassword);
         Statement st = con.createStatement();
         ResultSet rs = st.executeQuery("select * from employee");
         ResultSetMetaData md = rs.getMetaData();
         int columns = md.getColumnCount();
         for (int i = 1; i <= columns; i++) {
            System.out.print(md.getColumnName(i) + "\t");
         }
         System.out.println();
         System.out.println("---------------------------------");
         while (rs.next()) {
            for (int i = 1; i <= columns; i++) {
               System.out.print(rs.getString(i) + "\t");
            }
            System.out.println();
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
<?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.1.xsd"
 profile="development">

 <bean id="account" class="com.ashok.spring.core.bean.profiling.beans.AccountBean">
  <property name="driverClass" value="com.mysql.jdbc.Driver" />
  <property name="driverURL" value="jdbc:mysql://localhost:3306/emp" />
  <property name="dbUserName" value="root" />
  <property name="dbPassword" value="ashok" />
 </bean>
</beans>
<?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.1.xsd"
 profile="production">

 <bean id="account" class="com.ashok.spring.core.bean.profiling.beans.AccountBean">
  <property name="driverClass" value="com.mysql.jdbc.Driver" />
  <property name="driverURL" value="jdbc:mysql://localhost:3306/employee" />
  <property name="dbUserName" value="root" />
  <property name="dbPassword" value="ashok" />
 </bean>
</beans>
package com.ashok.spring.core.bean.profiling.test;

import org.springframework.context.support.GenericXmlApplicationContext;

import com.ashok.spring.core.bean.profiling.beans.AccountBean;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String productionConf = "/com/ashok/spring/core/bean/profiling/config/applicationContext-production.xml";
      String developmentConf = "/com/ashok/spring/core/bean/profiling/config/applicationContext-development.xml";
      System.setProperty("spring.profiles.active", "development");
      GenericXmlApplicationContext context = new GenericXmlApplicationContext();
      context.load(productionConf, developmentConf);
      context.refresh();
      AccountBean accBean = (AccountBean)context.getBean("account");
      accBean.listAccounts();
   }
}
Output
emp_id emp_name emp_address salary
---------------------------------
E0087 Ashok Kumar Bhimavaram 50000.0

Next Tutorial : Spring Expression Language

Previous Tutorial : Property Editors

No comments:

Post a Comment