VOOZH about

URL: https://www.javacodegeeks.com/2015/03/introduction-to-spring-profiles.html

⇱ Introduction to Spring profiles - Java Code Geeks


So many men, so many minds. When we are implementing software for different customers we sometimes need to handle various requirements for the same project. For example Customer A needs SAML authentication and customer B needs LDAP authentication.

With Spring Profiles (available from Spring 3.1) we are able to provide a way to segregate parts of our implemented application configuration. This blog will help us to make certain code or rather certain Spring beans only available for specific requirements. For example the example used in this blog can be used to activate the required authentication provider for the provider manager when using Spring Security.

Profiles can be configured by annotations and/or by xml.

Annotations

@Component or @Configuration annotated beans can contain the annotation @Profile to only load them in a certain environment.

Ldap profile annotated configuration

@Component
@Profile("ldap")
public class LDAPAuthentication {	
 public LDAPAuthentication() {
 System.out.println("LDAP Authentication set by annotations");
 }	
}

Saml profile annotated configuration

@Component
@Profile("saml")
public class SAMLAuthentication { 
 public SAMLAuthentication() {
 System.out.println("SAML Authentication set by annotations");
 } 
}

XML

Probably not used anymore in freshly started projects, but it is also possible to make certain Spring beans only available within your XML configuration.

Spring XML configuration

<!-- 
 We use the profile attribute on the beans element to specify the profile.
 Only the child beans are loaded on initialization if the profile is active 
-->
<beans profile="ldap">
 <bean class="com.jdriven.blog.profiles.xml.LDAPAuthentication" />
</beans>
<beans profile="saml">
 <bean class="com.jdriven.blog.profiles.xml.SAMLAuthentication" /> 
</beans>

Activate correct profile

Of course you are able to combine both configurations, but is should be obvious to choose one configuration to make your code more predictable . Just to show the possibilities we have combined them in one project.In a plain Java application the profiles can be setup by activating the profile in your application context.

Run the sample application

public static void main(String[] args) {
 //Create new context to show the XML Spring profile setup
 GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
 //Setting 'ldap' as active profile
 ctx.getEnvironment().setActiveProfiles("ldap");
 //Load the app-context.xml from the root of the classpath
 ctx.load("classpath:app-context.xml");
 //We need to refresh the application because we added a resource
 ctx.refresh();
 //Closing the application context to release and destroy all resources and cached beans
 ctx.close();

 //Creating a new context to show the annotation Spring profile setup
 AnnotationConfigApplicationContext actx = new AnnotationConfigApplicationContext();
 //Setting 'saml' as active profile
 actx.getEnvironment().setActiveProfiles("saml");
 //Scan base package for annotations
 actx.scan("com.jdriven.blog");
 //We need to refresh the application because we added a scan
 actx.refresh();
 //Closing the application context to release and destroy all resources and cached beans
 actx.close();
}

See the following github for the full source of this project:

Reference: Introduction to Spring profiles from our JCG partner Michel Meeuwissen at the JDriven blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
Spring
πŸ‘ Photo of Arthur Arts
Arthur Arts
March 12th, 2015Last Updated: March 11th, 2015
0 154 2 minutes read

Arthur Arts

Arthur Arts is an experienced Enterprise Java software engineer and hands-on Agile Coach and Scrum Master. He has a strong focus on agile engineering principles and practices and loves to share his knowledge and learn from others. He has his own company Coded Value, is a passionate photographer and writes for agilearts.nl.
Subscribe

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

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz