Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication Examples

Condition On Web And Not Web 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.

Spring @ConditionalOnExpression Example

Condition On Expression 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…

Spring @ConditionalOnResource Example

Condition On Resource In this tutorial I will create examples on Spring @ConditionalOnResource. The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present in the classpath. For example, the Log4j class is only loaded if the log4j configuration file (log4j.properties) was found on the class-path. This way, you might create similar modules that are only loaded if their respective…

Spring @ConditionalOnProperty Example

Condition On Property 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…

Spring @ConditionalOnClass and @ConditionalOnMissingClass

Conditional Class 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 class path and @ConditionalOnMissingClass loads a bean only if a certain class is not on the class path. This mechanism does not apply the same way…

Spring @ConditionalOnMissingBean Example

Conditional On Missing Bean In this tutorial I will create examples on Spring @ConditionalOnMissingBean. Anywhere you define a Spring bean, you 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, you can use any of the @Conditional… annotations. The @ConditionalOnMissingBean annotation lets a…

Spring @ConditionalOnBean Example

Conditional On Bean I will create examples on Spring @ConditionalOnBean. Anywhere you define a Spring bean, you 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, you can use any of the @Conditional… annotations. The @ConditionalOnBean annotation let a bean be included based…