Close

Spring Framework - @Conditional Examples

Spring Framework 

This example shows the use of @Conditional annotation along with Condition interface implementations to selectively register the bean depending on the underlying Locale of the machine it's running on.

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

@Configuration
public class AppConfig {
@Bean
@Conditional(LocaleConditionUSA.class)
public MyService myBeanA () {
return new MyServiceA();
}

@Bean
@Conditional(LocaleConditionCanada.class)
public MyService myBeanB () {
return new MyServiceB();
}

@Bean
public ClientBean clientBean () {
return new ClientBean();
}
}
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Locale;

public class LocaleConditionUSA implements Condition {

@Override
public boolean matches (ConditionContext context, AnnotatedTypeMetadata metadata) {
return Locale.getDefault()
.equals(Locale.US);
}
}
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Locale;

public class LocaleConditionCanada implements Condition {

@Override
public boolean matches (ConditionContext context, AnnotatedTypeMetadata metadata) {
return Locale.getDefault()
.equals(Locale.CANADA);
}
}

The main class:

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Locale;

public class BeanConditionExample {

public static void main(String[] args) {
//In real application, System Locale would be used depending on your location.
//Here we are setting it manually to test our example*/
runApp(Locale.US);
System.out.println("----------");
runApp(Locale.CANADA);
}

public static void runApp(Locale locale) {
System.out.printf("setting default locale: %s\n", locale);
Locale.setDefault(locale);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
AppConfig.class);

ClientBean bean = context.getBean(ClientBean.class);
System.out.printf("Injected MyService instance in ClientBean: %s\n", bean.getMyService()
.getClass()
.getSimpleName());
}
}

Output

setting default locale: en_US
Injected MyService instance in ClientBean: MyServiceA
----------
setting default locale: en_CA
Injected MyService instance in ClientBean: MyServiceB
Original Post




This example shows the use of @Conditional annotation on @Component classes used with @ComponentScan.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Locale;


@Configuration
@ComponentScan(basePackageClasses = BeanConditionScanExample.class,
useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {BeanConditionScanExample.MyClientBean.class,
BeanConditionScanExample.ServiceBeanImpl1.class,
BeanConditionScanExample.ServiceBeanImpl2.class})})

public class BeanConditionScanExample {

public static void main(String[] args) {
runApp(Locale.US);
System.out.println("----------");
runApp(Locale.CANADA);
}

public static void runApp(Locale locale) {
System.out.printf("setting default locale: %s\n", locale);
Locale.setDefault(locale);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
BeanConditionScanExample.class);
MyClientBean bean = context.getBean(MyClientBean.class);
System.out.printf("Injected ServiceBean instance in MyClientBean: %s\n", bean.getServiceBean()
.getClass()
.getSimpleName());
}

@Component
public static class MyClientBean {
@Autowired
private ServiceBean serviceBean;

public ServiceBean getServiceBean() {
return serviceBean;
}
}

public interface ServiceBean {
}

@Component
@Conditional(LocaleConditionUSA.class)
public static class ServiceBeanImpl1 implements ServiceBean {
}

@Component
@Conditional(LocaleConditionCanada.class)
public static class ServiceBeanImpl2 implements ServiceBean {
}
}

Output

setting default locale: en_US
Injected ServiceBean instance in MyClientBean: ServiceBeanImpl1
----------
setting default locale: en_CA
Injected ServiceBean instance in MyClientBean: ServiceBeanImpl2
Original Post




See Also