Saturday, October 29, 2022

Spring - Profiling

  • In general, in the project lifecycle, we have to perform development, testing, production mainly. At each and every phase of project lifecycle we may use databases, debugging tools, testing tools with different configuration details.
  • In general, in all project lifecycle 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 inorder to provide the corresponding configuration details wrt the lifecycle phases, for this, Spring framework has provided "Profiling" feature.

  • IN Project Implementation, we may use database configuration details like data source names, connection pool names, and JNDI names in Server

If we want to implement Profiling in Spring applications then we have to use the following steps.

  • Create a seperate spring configuration file for each and every phase of the project lifecyle with the respective configuration details.

  • Example:
    • spring-context-development.xml 
    • spring-context-testing.xml 
    • spring-context-production.xml

  • Spring configuration XML File format must be <FileName>-phase_Name.xml 2.In all Spring Configuration files we must provide "profile" attribute in <beans> tagwith the respective lifecycle phyase name.

Example: 

spring-context-development.xml

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

spring-context-production.xml

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

  • Provide project lifecycle phase in System property with the key "spring.profiles.active" in Main Application.
    • System.setProperty("spring.profiles.active", "development");

  • In main Application, we have to use GenericXmlApplicationContext as Container and load all the spring configuration files with ctx.load(--,--,--); method and rdfresh context.

Example:

GenericXmlApplicationCointext context = new GenericXmlApplicationContext(); 
context.load("spring-context-development.xml", "spring-context-production.xml"); 
context.refresh();

AccountBean.java

package com.cloudtechtwitter.beans;
import java.sql.Connection;
import java.sql.DriverManager; import java.sql.ResultSet;
import java.sql.ResultSetMetaData; import java.sql.Statement;

public class AccountBean { 

   private String driverClass; 
   private String driverURL;  
   private String dbUserName; 
   private String dbPassword;

   setXXX()  
   getXXX()

    public void listAccounts() {
     try {
        Class.forName(driverClass);
        Connection con = DriverManager.getConnection(driverURL,
        dbUserName, dbPassword);
        
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from account"); 
        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(); 
      }
   }
}

applicationContext-development.xml

<beans ..... profile="development">
        <bean id="accBean" class="com.cloudtechtwitter.beans.AccountBean">
                <property name="driverClass" value="oracle.jdbc.OracleDriver"/>
                <property name="driverURL" value="jdbc:oracle:thin:@localhost:1521:xe"/>
                <property name="dbUserName" value="system"/>
                <property name="dbPassword" value="cloud"/> 
        </bean>
</beans>

applicationContext-production.xml

<beans .... profile="production">
        <bean id="accBean" class="com.cloudtechtwitter.beans.AccountBean">
                <property name="driverClass" value="com.mysql.jdbc.Driver"/>
                <property name="driverURL" value="jdbc:mysql://localhost:3306/clouddb"/>
                <property name="dbUserName" value="root"/>
                <property name="dbPassword" value="root"/> 
        </bean>
</beans>

Test.java

package com.cloudtechtwitter.test;
import org.springframework.context.support.GenericXmlApplicationContext; 
import com.cloudtechtwitter.beans.AccountBean;

public class Test {

        public static void main(String[] args)throws Exception { 

        System.setProperty("spring.profiles.active", "production"); 
        GenericXmlApplicationContext context = new
                GenericXmlApplicationContext();

        context.load("applicationContext-development.xml", 
                "applicationContext- production.xml");
        context.refresh();
        
        AccountBean accBean = (AccountBean)context.getBean("accBean");
        accBean.listAccounts();

        }

}

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS