• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

@Profile Annotation in Spring 4

July 3, 2015 //  by Krishna Srinivasan//  Leave a Comment

This tutorial explain how to enable profiles in your spring application for different environments. Also this tutorial explains the new improvements in the Spring 4.0 release and how it is different from the older releases.

If you are working for a real time projects, it is often basic requirement to have the different environments like development, testing and production. In this scenario, the underlying resources (ie, database, file system, etc) for each environment would be different. The challenges arises when you want to move your source code to different environments. You would have to adjust the configurations to point to new environment details like Database, etc. The solution for the above problem is to create different profiles for each environment and enable them when you move to another environment.

List of Spring Tutorials : Spring Tutorial

@Profile annotation is introduced as part of the Spring Framework 3.1 release. @Profile annotation helps developers to create multiple configurations details for different environments. Prior to Spring 4, only bean level annotation is supported by @Profile. With Spring 4, this can be used in the method level. That gives more control and eliminate the need for creating multiple configuration files. This tutorial provide simple example for @Profile annotation.

Spring official document defines @Profile as:

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments.

I have written a very simple example to demonstrate the power of @Profile feature in Spring 4. This example declares data source for dev and prod. By using the @Profile annotation, a suitable data source will be invoked by the test class.

@Profile Annotation in Spring 4

@Profile Annotation

The syntax for this annotation is @Profile(“dev”). If a bean or method marked with annotation will be associated with that particular profile name. This example uses the method type annotation (Spring 4 improvements). Where as you also do it in the class level.

@ActiveProfiles Annotation

How do we tell the container that which profile is active now to pick up the right configurations. We have to use @ActiveProfiles to tell the currently active profile. This can be done through XML, JavaConfig, Annotations and Programatically.

Annotation Configuration

@ActiveProfiles(profiles = "dev")
public class DevConfigTest {
//code here
}

Web.xml Configuration

<context-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>DOUBLEUPMINT</param-value>
</context-param>

Programatic Configuration

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//Enable a "live" profile
context.getEnvironment().setActiveProfiles("live");

@Profile Annotation Example

Here is the example application using @Profile annotation.

Student.java

package javabeat.net.spring.core;

public class Student {
	private String id;
	private String name;
	private String grade;

	public Student(String id, String name, String grade){
		this.id = id;
		this.name = name;
		this.grade = grade;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

}

DevEnvConfig.java

package javabeat.net.spring.core;

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

@Configuration
@Profile("dev")
public class DevEnvConfig {
	@Bean
	public Student user(){
		return new Student("001", "Dev Student 1","Grade 1");
	}
}

ProdEnvConfig.java

package javabeat.net.spring.core;

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

@Configuration
@Profile("prod")
public class ProdEnvConfig {
	@Bean
	public Student user(){
		return new Student("002", "Prod Student 1","Grade 2");
	}
}

ProductionDataSourceConfig.java

package javabeat.net.spring.core;

import javax.sql.DataSource;

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

@Configuration
@Profile("prod")
public class ProductionDataSourceConfig {
	  @Bean
	  public DataSource dataSource(){
		//Do connection related task
    	        return null;
	  }
}

SpringProfileTest.java

package javabeat.net.spring.core;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringProfileTest {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.getEnvironment().setActiveProfiles("prod");
		ctx.scan("javabeat.net.spring.core");
		ctx.refresh();
		Student student = ctx.getBean(Student.class);
		System.out.println("\nStudent Id:" + student.getId() + ", \nStudent Name:" + student.getName()
				+ ", \nStudent Grade : " + student.getGrade());
	}
}

Summary

This tutorial demonstrated how to use @Profile annotation in your project. It is a simple example and I hope this helps you to understand the concepts. If you have any questions, please write it in the comments section.

Category: Spring FrameworkTag: Spring Annotations

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Node.js : Reading and Writing File in Node.js
Next Post: Spring 4.2 Released »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact