About @Conditional

 

@Conditional(JdbcTemplateCondition.class)

public MyService myService() { }

In this case, the MyService bean will only be created if the JdbcTemplateCondition passes. That is to say that the MyService bean will only be created if JdbcTemplate is available on the classpath. Otherwise, the bean declaration will be ignored.

Spring Boot defines several more interesting conditions and applies them to the configuration classes that make up Spring Boot auto-configuration. Spring Boot applies conditional configuration by defining several special conditional annotations and using them in its configuration classes.

@ConditionalOnBean : the specified bean has been configured

@ConditionalOnMissingBean : the specified bean has not already been configured

@ConditionalOnClass : the specified class is available on the classpath

@ConditionalOnMissingClass :the specified class is not available on the classpath

@ConditionalOnExpression : the given Spring Expression Language (SpEL) expression evaluates to true

@ConditionalOnJava : the version of Java matches a specific value or range of versions

@ConditionalOnJndi : there is a JNDI InitialContext available and optionally given JNDI locations exist

@ConditionalOnProperty : the specified configuration property has a specific value

@ConditionalOnResource : the specified resource is available on the classpath

@ConditionalOnWebApplication : the application is a web application

@ConditionalOnNotWebApplication : the application is not a web application

@ConditionalOnMissingBean(JdbcOperations.class)

public JdbcTemplate jdbcTemplate() {}

@ConditionalOnMissingBean so that the bean will be configured only if there is not already a bean of type JdbcOperations