Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication Examples

Introduction The Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations let configuration be included depending on whether the application is a web application. A web application is any application that uses a Spring WebApplicationContext, defines a session scope, or has a StandardServletEnvironment.

Continue

Spring @ConditionalOnExpression Example

Introduction I will create examples how to work with Spring Conditional on Expression using @ConditionalOnExpression. The @ConditionalOnExpression annotation lets configuration be included based on the result of a SpEL (Spring Expression Language) expression. For this example the Module class is only loaded if a particular SpEL is enabled. This way, you might create similar modules that are only loaded if their respective SpEL has been found or enabled.

Continue

Spring @ConditionalOnResource Example

Introduction We will create examples on Spring @ConditionalOnResource. The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present in the classpath. For our example the Log4j class is only loaded if the log4j configuration file was found on the classpath. This way, we might create similar modules that are only loaded if their respective configuration file has been found.

Continue

Spring @ConditionalOnProperty Example

Introduction Here I will create examples on Spring conditional on property using the annotation @ConditionalOnProperty. The @ConditionalOnProperty annotation allows you to load beans conditionally depending on a certain environment property or configuration of a property. Use the prefix and name attributes to specify the property that should be checked. By default, any property that exists and is not equal to false is matched. You can also create more advanced checks by using the havingValue and matchIfMissing attributes.

Continue

Spring @ConditionalOnClass and @ConditionalOnMissingClass

Introduction Spring @ConditionalOnClass and @ConditionalOnMissingClass annotations let @Configuration classes be included based on the presence or absence of specific classes. So @ConditionalOnClass loads a bean only if a certain class is on the classpath and @ConditionalOnMissingClass loads a bean only if a certain class is not on the classpath. This mechanism does not apply the same way to @Bean methods where typically the return type is the target of the condition: before the condition on the method applies, the JVM will have loaded the class and potentially processed method references…

Continue

Spring @ConditionalOnMissingBean Example

We will create examples on Spring @ConditionalOnMissingBean. Anywhere we define a Spring bean, we can optionally add a condition. Only if the specified condition is satisfied then only bean will be added to the application context. To declare a condition, we can use any of the @Conditional… annotations. The @ConditionalOnMissingBean annotation lets a bean be included based on the absence of specific beans. By default Spring will search entire hierarchy (SearchStrategy.ALL).

Continue

Spring @ConditionalOnBean Example

Introduction We will create examples on Spring @ConditionalOnBean. Anywhere we define a Spring bean, we can optionally add a condition. Only if the specified condition is satisfied then only bean will be added to the application context. To declare a condition, we can use any of the @Conditional… annotations. The @ConditionalOnBean annotation let a bean be included based on the presence of specific beans. By default Spring will search entire hierarchy (SearchStrategy.ALL).

Continue