Home  >  Spring Core

Example of @Profile Spring

By Arvind Rai, March 11, 2013
@Profile in spring is used when we are writing our configuration class for XML free bean. If we need to create two configuration files for test environment and production environment both. In this case @Profile will be helpful to select the correct config file. @Profile belongs to the package org.springframework.context.annotation.Profile. @Profile is used as
@Profile("prod") 
To select the profile, we need to do like
ctx.getEnvironment().setActiveProfiles("prod"); 
In real life, we may need to create two data source one for production and second for development. And while development we can activate development profile and in production, by a minor change we can change the data source. Configuration for "prod" profile.
@Configuration
@Profile("prod")
public class ProductionDataSourceConfig {
	  @Bean
	  public DataSource dataSource(){
		//Do connection related task
    	        return null;
	  }
} 
Configuration for "dev" profile.
@Configuration
@Profile("test")
public class TestDataSourceConfig {
 @Bean
  public DataSource dataSource(){
	//Do connection related task
	  return null;
  }
} 

Complete Example of @Profile

For @Profile demo, we are creating a user. The user can be development environment user and production environment user. We have two configuration classes with two profiles, one for production and one for development.

Gradle File for Spring Boot

Find the gradle file to resolve spring dependency
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'SpringDemo'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile  'org.springframework.boot:spring-boot-starter:1.2.0.RELEASE'
}

Configuration Class with Profile

Find the Production configuration class with prod profile.
ProdEnvConfig.java
package com.concretepage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.concretepage.User;
@Configuration
@Profile("prod")
public class ProdEnvConfig {
	@Bean
	public User user(){
		return new User(111, "Prod User");
	}
}
Find the development configuration class with dev profile.
DevEnvConfig.java
package com.concretepage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.concretepage.User;
@Configuration
@Profile("dev")
public class DevEnvConfig {
	@Bean
	public User user(){
		return new User(111, "Dev User");
	}
}
Find the user class.
User.java
package com.concretepage;
public class User {
	public Integer id;
	public String name;
	public User(Integer id, String name){
		this.id = id;
		this.name = name;
	}
}

Run Demo Project

To run demo project, we are creating a main class. First we will set the profile as dev.
SpringProfileTest.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringProfileTest {
	public static void main(String[] args) {
		    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	  	    ctx.getEnvironment().setActiveProfiles("dev");
		    ctx.scan("com.concretepage.config");
		    ctx.refresh();
		    User user = ctx.getBean(User.class);
		    System.out.println("id:"+user.id +", Name:"+ user.name );
    }
}
Find the output
 id:111, Name:Dev User
Now change the profile as prod changing the line as below.
ctx.getEnvironment().setActiveProfiles("prod");
Now find the output.
 id:111, Name:Prod User
We are done now. Happy spring learning.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us