Wednesday, July 25, 2012

Spring Profile pattern ,part 2

Spring Profile pattern -  phase 1: infra preparation


This phase will establish the initial infra for using Spring Profile and the configuration files.

Step 1.1  - create a properties file which contains all configuration data
Assuming you have a maven style project, create a file in src/main/resources/properties for each environment, e.g:
my_company_dev.properties
my_company_test.properties
my_company_production.properties

example for my_company_dev.properties content:

jdbc.url=jdbc:mysql://localhost:3306/my_project_db
db.username=dev1
db.password=dev1
hibernate.show_sql=true

example for my_company_production.properties content:


jdbc.url=jdbc:mysql://10.26.26.26:3306/my_project_db
db.username=prod1
db.password=fdasjkladsof8aualwnlulw344uwj9l34
hibernate.show_sql=false


Step 1.2  - create an annotation for each profile
In src.main.java.com.mycompany.annotation create annotation for each Profile, e.g :

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("DEV")
public @interface Dev {
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("PRODUCTION")
public @interface Production {
}

Create an enum for each profile:
public interface MyEnums {

public enum Profile{
DEV,
TEST,
PRODUCTION
}


Step 1.3  - make sure the profile is loaded during context loading
  • Define a system variable to indicate on which environment the code is running.
    In Tomcat, go to ${tomcat.di}/conf/catalina.properties and insert a line:
    profile=DEV  (according to your environment)
  • Define a class to set the active profile
    public class ConfigurableApplicationContextInitializer implements
      ApplicationContextInitializer {
    
     @Override
     public void initialize(ConfigurableApplicationContext applicationContext) {
          
      String profile = System.getProperty("profile");
        
      if (profile==null || profile.equalsIgnoreCase(Profile.DEV.name())){
       applicationContext.getEnvironment().setActiveProfiles(Profile.DEV.name());   
      }else if(profile.equalsIgnoreCase(Profile.PRODUCTION.name())){
       applicationContext.getEnvironment().setActiveProfiles(Profile.PRODUCTION.name()); 
      }else if(profile.equalsIgnoreCase(Profile.TEST.name())){
       applicationContext.getEnvironment().setActiveProfiles(Profile.TEST.name()); 
            }
     }
    }
    
  • Make sure the class is loaded during context loading
    in the project web.xml, insert the following:
    
         contextInitializerClasses
         com.matomy.conf.ConfigurableApplicationContextInitializer
     
    


part 1, part 2 , part 3, part 4 , next >>

2 comments :

  1. Hi

    Great job.
    I would like to use this pattern in a integration-test environment.
    Does the @ActiveProfiles("dev") annotation work in order to active the "dev" profile.
    About the step 1.3, is the web configuration mandatory because my production's environment is a standalone application (not a web application).

    Thanks

    Franck

    ReplyDelete
    Replies
    1. Hi Frank,

      If you are working in non-web environment, then you should set the active profile variable on a place other than the Tomcat properties , such as a parameter in the JVM per that machine (such as profile=integration).
      Then you should set your active profile based on that variable on your main or init method (@ActiveProfiles("integration") ).

      Delete