Spring @Profile

We can enable the environment-aware feature “Profile” in Spring XML configuration, also we can enable this feature using annotation @Profile in Spring @Configuration class “the Spring annotation configuration” which is equivalent to XML configuration <beans…>…..</beans>.

springsource

Objective:

  • How to use @Profile to load Spring configuration for specific environment?

Environment & Tools:

  • Eclipse
  • Maven (optional)

Libraries:

  • Spring framework

Source Code @ GitHub

Assume we want to have two set beans of different values, state or types, each to be instantiated on a different environment, one of development and one for production.

( 1 ) Project Structure

( 2 ) @Configuration Classes

@Configuration annotated class is the java-equivalent to XML configuration.

  • src/main/java/com/hmkcode/spring/SpringConfigDev.java

Spring configurations for development environment.

package com.hmkcode.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.hmkcode.vo.Person;

@Configuration
@Profile("development")
public class SpringConfigDev {

  @Bean
  public Person person(){

	  Person person =  new Person();
	  person.setId(1);
	  person.setName("dev-person");

	  return person;
  }
}

The equivalent to XML configuration would be as following:

<?xml version="1.0" encoding="UTF-8"?>
<beans..... profile="development">

            <bean id="person" class="com.hmkcode.vo.Person">
                <property name="id" value="1" />
                <property name="name" value="dev-person" />
            </bean>
</beans>
  • src/main/java/com/hmkcode/spring/SpringConfigPro.java

Spring configurations for production environment.

package com.hmkcode.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.hmkcode.vo.Person;

@Configuration
@Profile("production")
public class SpringConfigPro {

  @Bean
  public Person person(){

	  Person person =  new Person();
	  person.setId(2);
	  person.setName("pro-person");

	  return person;
  }
}

( 3 ) Which Profile to Set Active?

Now, we need to tell the ApplicationContext which profile to pick “to set active”.

package com.hmkcode;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.hmkcode.vo.Person;

public class App 
{
    public static void main( String[] args )
    {

    	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.getEnvironment().setActiveProfiles("development"); //or production
        ctx.scan("com.hmkcode.spring"); // register all @Configuration classes
        //ctx.register(SpringConfigDev.class);
        ctx.refresh();
        Person person = (Person)ctx.getBean("person");

        System.out.println("Person: "+person);
    }
}