Profiling

Profiling

In this tutorial, we are going to discuss profiling in the spring framework. In general, in the project life cycle, we mainly have to perform development, testing, and production. We may use databases, debugging tools, testing tools, etc., with different configuration details at every phase of the project life cycle.

Spring Profiling

In general, we will provide the required configuration details manually in all project life cycle phases, which may increase problems for the applications. In this context, the Spring3.x version has provided an automated solution to provide the corresponding configuration details w.r.t the life cycle phases. For this, the Spring framework has provided a “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 every project Lifecycle phase 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 the “profile” attribute in tag with the respective life cycle phase name.

E.g.

spring-context-development.xml

<beans profile="development">
     ---------
</beans>

spring-context-testing.xml

<beans profile="testing">
     ---------
</beans>

spring-context-production.xml

<beans profile="production">
     ---------
</beans>

3. Provide project life cycle phase in System property with the key “spring.profiles.active” in Main Application.

E.g.

System.setProperty("spring.profiles.active", "development");

In the 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();

E.g

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
Profiling

Scroll to top