Spring Profiles – @Profile Annotation and XML Configuration

In application development, sometimes we should make different configuration for each environment. Spring Profiles provides a way to separate application configuration into some parts and they are only available in one or more specific environments.

This tutorial introduces ways to use Spring Profiles by @Profile Annotation or XML Configuration.

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

II. Overview
1. Project Structure

springprofiles-structure
– Our application will have two main profiles: development and production.
– Depending on which profile is active, we will get corresponding @Bean Data.
– In class AppConfig, we also apply @Profile annotation for methods.
– To activate a profile, we use Environment#setActiveProfiles(profile) method.
– Instead of using @Profile annotation, we can declare profiles by XML.

2. Step to do

– Create Maven project
– Add Dependencies & Plugins
– Create Classes for Beans
– Create Configuration Class for each Profile
– Create Configuration Class for Application
– Create MainApplication Class
– Run Application & Enjoy Result

III. Practice
1. Create Maven project

– Open Spring Tool Suite, on Menu, choose File -> New -> Maven Project.
– Check Create a simple project, choose Workspace Location and click Next.
– Fill all fields in Artifact group box, then click Finish.

2. Add Dependencies & Plugins

Open pom.xml, add:

	
		1.8
		4.3.2.RELEASE
	

	
		
			org.springframework
			spring-core
			${org.springframework-version}
		
		
			org.springframework
			spring-context
			${org.springframework-version}
		
		
			org.springframework
			spring-orm
			${org.springframework-version}
		
	

	
		
			
				
					org.apache.maven.plugins
					maven-compiler-plugin
					3.3
					
						1.8
						1.8
					
				
			
		
	
3. Create Classes for Beans

Under package bean, create 3 classes:

Data.java

package com.javasampleapproach.springprofiles.bean;

public class Data {
	private String config;

	public Data() {
	}

	public Data(String config) {
		this.config = config;
	}

	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}
}

Method.java

package com.javasampleapproach.springprofiles.bean;

public class Method {
	private String config;

	public Method(String config) {
		this.config = config;
	}

	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}
}

NewMethod.java

package com.javasampleapproach.springprofiles.bean;

public class NewMethod {
	private String config;

	public NewMethod(String config) {
		this.config = config;
	}

	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}
}
4. Create Configuration Class for each Profile

Under package config.data, create interface DataConfig and two implementation classes:

DataConfig.java

package com.javasampleapproach.springprofiles.config.data;

import com.javasampleapproach.springprofiles.bean.Data;

public interface DataConfig {
	Data getData();
}

DevelopmentDataConfig.java

package com.javasampleapproach.springprofiles.config.data;

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

import com.javasampleapproach.springprofiles.bean.Data;

@Profile("development")
@Configuration
public class DevelopmentDataConfig implements DataConfig {

	@Override
	@Bean
	public Data getData() {
		return new Data("Data Configuration for Development Purpose");
	}
}

ProductionDataConfig.java

package com.javasampleapproach.springprofiles.config.data;

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

import com.javasampleapproach.springprofiles.bean.Data;

@Profile("production")
@Configuration
public class ProductionDataConfig implements DataConfig {

	@Override
	@Bean
	public Data getData() {
		return new Data("Data Configuration for Production Purpose");
	}
}
5. Create Configuration Class for Application

Under package config, create AppConfig.java:

AppConfig.java

package com.javasampleapproach.springprofiles.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.javasampleapproach.springprofiles.bean.Data;
import com.javasampleapproach.springprofiles.bean.Method;
import com.javasampleapproach.springprofiles.bean.NewMethod;

@Configuration
@ComponentScan(basePackages = "com.javasampleapproach.springprofiles")
public class AppConfig {
	@Autowired
	Data data;

	@Bean
	@Profile("development")
	public Method getDevelopmentMethod() {
		return new Method("Method Level for Development Purpose");
	}
	
	@Bean
	@Profile("production")
	public Method getProdutionMethod() {
		return new Method("Method Level for Prodution Purpose");
	}
	
	@Bean
	@Profile("addition")
	public NewMethod getAdditionMethod() {
		return new NewMethod("Method Level for Addition Purpose");
	}
}
6. Create MainApplication Class

MainApp.java

package com.javasampleapproach.springprofiles;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
//import org.springframework.context.support.AbstractApplicationContext;
//import org.springframework.core.env.AbstractEnvironment;
//import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javasampleapproach.springprofiles.bean.Data;
import com.javasampleapproach.springprofiles.bean.Method;
import com.javasampleapproach.springprofiles.bean.NewMethod;
import com.javasampleapproach.springprofiles.config.AppConfig;

public class MainApp {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

		context.getEnvironment().setActiveProfiles("production", "addition");

		context.register(AppConfig.class);
		context.refresh();
		
//		System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "development");
//		AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		
		Data data = context.getBean(Data.class);
		System.out.println(data.getConfig());
		
		Method method = context.getBean(Method.class);
		System.out.println(method.getConfig());
		
		// for @Profile("addition")
		NewMethod newMethod = context.getBean(NewMethod.class);
		System.out.println(newMethod.getConfig());

		context.close();
	}
}

We set a Profile to be active by Environment from Context:

context.getEnvironment().setActiveProfiles("production");

// for activating multi-profile
context.getEnvironment().setActiveProfiles("production", "addition");

Another way to make a profile active is using System#setProperty method:

// ACTIVE_PROFILES_PROPERTY_NAME = "spring.profiles.active"
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "development");
7. Run Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Java Application
– Check results in Console Screen:

Data Configuration for Production Purpose
Method Level for Prodution Purpose
Method Level for Addition Purpose
8. XML Configuration for Profiles

Instead of using Spring Java Annotation, we can do those things by corresponding XML Configuration:

development_config.xml




	
		
	


production_config.xml




	
		
	


Two XML configuration files above will be imported to app_config.xml under :




	

	
		
	

	
		
	

So, to load XML Configuration with Profiles, the code inside function main should be changed to:

GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.getEnvironment().setActiveProfiles("development");
context.load("app_config.xml");
context.refresh();
IV. Source Code

springprofile



By grokonez | October 11, 2016.

Last updated on June 4, 2017.



Related Posts


Got Something To Say:

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

*