Thursday, 8 December 2016

Using Conditional annotation in Spring framework

In some cases you do want to conditionally enable or disable a complete @Configuration class, or even individual @Bean methods. One common example of this is to use the @Profile annotation to activate beans only when a specific profile has been enabled in the Spring Environment.

Spring 4 @Conditional annotation

In Spring 4 @Conditional annotation has been added which can be used for providing your own logic for conditional checking and then decide whether specific bean should be registered or not.

The @Conditional annotation indicates specific org.springframework.context.annotation.Condition implementations that specifies the condition which should be consulted before a @Bean is registered.

Condition interface

A single condition that must be matched in order for a component to be registered. The class given as value in @Conditional annotation has to implement the Condition interface. Condition interface required that you provide an implementation for the matches() method.

  • boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) - Determine if the condition matches.

    Parameters:

    • context - the condition context
    • metadata - metadata of the class or method being checked
    • Returns: true if the condition matches and the component can be registered or false to veto registration.

Conditional annotation example

Suppose you want to create a bean only if a specific condition is present in the property file otherwise you don’t to create the bean. That can be done using @Conditional annotation.

TestBean class

public class TestBean {
 private String name;

 public String getName() {
  return name;
 }

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

TestBeanCondition class

This is the class which implements the Condition interface and provides the condition for creating the TestBean. As you can see in the matches method it checks if environment contains the property “test”.

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class TestBeanCondition implements Condition {

 @Override
 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  Environment env = context.getEnvironment();
  return env.containsProperty("test");
 }

}

test.properties class

test=true
country=India

TestBeanConfig class

This is the class where TestBean is created, you can see the @Conditional annotation used here with the class that provides the condition.
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value="classpath:config/test.properties", ignoreResourceNotFound=true)
public class TestBeanConfig {
 @Bean
 @Conditional(TestBeanCondition.class)
 public TestBean testBean() {
  System.out.println("test bean creation");
  return new TestBean();
 }
}

You can test this code using the following piece of code -

 
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AppProfile {
 public static void main( String[] args ){
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBeanConfig.class);
  
  TestBean tb = (TestBean)context.getBean("testBean");  
  tb.setName("Ram");
  System.out.println("" + tb.getName());
  context.close();
    }
}

Output

test bean creation
Ram

That's all for this topic Using Conditional annotation in Spring framework. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Using Spring profiles to switch environment
  2. Insert\Update using JDBCTemplate in Spring framework
  3. Wiring collections in Spring
  4. Data access in Spring framework
  5. What is Dependency Injection in Spring

You may also like -

>>>Go to Spring tutorial page

No comments:

Post a Comment