@ConditionalOnProperty annotation
This annotation is applied over a spring bean and is used to control whether this bean will be loaded or not based on a property.
This property must be present in the application environment and should not be equal to false
. Example,
@Configuration @ConditionalOnProperty(name = "myconfig.enabled", havingValue = "true") public class MyConfiguration { // Configuration code for the feature }
In this example, the bean MyConfiguration
will be loaded only if myconfig.enabled
property is present in the application environment and its value is “true”.
havingValue
attribute is optional and if not given, then it defaults to “”. This means that only the presence of property is checked regardless of its value.
Attributes
@ConditionalOnProperty
annotation has several attributes which allow to customize its behavior. These are
- name
Specifies the name of the property to check.
Ifprefix
attribute is given, then it is prepended to the name to determine complete name of property. - havingValue
Specifies the expected value of the property. The component or configuration will be enabled only if the property value matches this value. This attribute is optional. - matchIfMissing
Specifies whether the component or configuration should be enabled if the property is missing in the environment.
By default, it is set tofalse
, which means the component or configuration is disabled if the property is missing.
WithmatchIfMissing
set totrue
, the bean will be loaded even if the property given inname
attribute is not found. - prefix
Specifies a common prefix that should be applied to the property names before checking their values.
So,prefix = "app"
andname = "prop"
will make the name of property to be app.prop.
The prefix automatically ends with a dot even if not specified. - value
An alias for thename
attribute. Use eithername
orvalue
to specify the property name.
@ConditionalOnProperty example
Suppose you have a spring boot application with a feature to send email notifications.
You want to conditionally enable the email notification feature based on a property in the application’s configuration.
For this, assume there is NotificationService
class that handles sending email notifications as below
public class NotificationService { // code }
To configure email notification feature, provide @ConditionalOnProperty
annotation over the class as below
@Configuration @ConditionalOnProperty(name = "com.notify.email", havingValue = "true", matchIfMissing = false) public class EmailConfiguration { @Bean public NotificationService notificationService() { return new NotificationService(); } }
If the property com.notify.email
is present in the configuration and its value is "true"
, the EmailConfiguration
class will be loaded, and the notificationService()
method will be invoked to create a bean of NotificationService
.
If the property is missing or its value is anything other than "true"
, the EmailConfiguration
class will not be loaded.
With matchIfMissing
attribute, we can also control the behavior if the property could not be found.
If matchIfMissing
is set to true
, the email notification feature will be enabled even if the property is not present in the configuration.
To conclude,
@ConditionalOnProperty
annotation is a smart way to enable or disable components and configurations conditionally, based on the presence and value of properties in the application’s configuration.With this annotation, you can gain control over the behavior of application, to selectively enable or disable features without code modifications.