Spring boot - Conditional Bean Creation
@ConditionalOnBean(name = "otherNeededBean")
The bean is only created, if the bean "otherNeededBean" already exist.
@ConditionalOnMissingBean
The bean is only created, if no other bean with the same name already exist.
@ConditionalOnMissingBean(type = "alternativeBean")
The bean is only created, if bean "alternativeBean" doesn't exist.
Conditional based on Environment property
Add proprty file to the Configuration class.
@PropertySource("classpath:myspecific.properties")
public class MySpecificConfiguration {}
@ConditionalOnProperty(name = "email.notification", havingValue = "true")
@Bean
public MailService ...
myspecific.properties:
email.notification = true | false
The bean is only created, if the bean "otherNeededBean" already exist.
@ConditionalOnMissingBean
The bean is only created, if no other bean with the same name already exist.
@ConditionalOnMissingBean(type = "alternativeBean")
The bean is only created, if bean "alternativeBean" doesn't exist.
Conditional based on Environment property
Add proprty file to the Configuration class.
@PropertySource("classpath:myspecific.properties")
public class MySpecificConfiguration {}
@ConditionalOnProperty(name = "email.notification", havingValue = "true")
@Bean
public MailService ...
myspecific.properties:
email.notification = true | false