Enterprise Java

Introduction to Spring profiles

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.

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
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button