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