Spring Boot @ConditionalOnBean Annotation Example

In this section we will learn about @ConditionalOnBean Annotation.


The @ConditionalOnBean annotation let a bean be loaded based on the presence of specific bean inside Spring container.

The @ConditionalOnBean annotation may be used on any class annotated with @Component@Service & @Repository or on methods annotated with @Bean.

1. Using @ConditionalOnBean on @Bean method

For example, @ConditionalOnBean(name = "emailNotificationProvider"), when the Bean of name "emailNotificationProvider" exists in the container, the bean emailNotification will be loaded.

@Bean("emailNotification")
@ConditionalOnBean(name = "emailNotificationProvider")
public EmailNotificationService emailNotificationService()
{
return new EmailNotificationService();
}


2. Using @ConditionalOnBean on @Service class

For example, @ConditionalOnBean(name = "smsNotificationProvider"), when the Bean of name "smsNotificationProvider" exists in the container, the bean smsNotification will be loaded.

@Service("smsNotification")
@ConditionalOnBean(name = "smsNotificationProvider")
public class SMSNotificationService {

public SMSNotificationService() {
System.out.println("SMSNotificationService Constructor");
}
}


The following example creates a Spring Boot web application which uses @ConditionalOnBean annotation.

Project Directory



pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-boot-conditionalonbean-annotation-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-conditionalonbean-annotation-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


EmailNotificationService.java

package com.knf.dev.demo.service;

public class EmailNotificationService {

public EmailNotificationService() {
System.out.println("EmailNotificationService Constructor");
}
}


TwitterNotificationService.java

package com.knf.dev.demo.service;

public class TwitterNotificationService {

public TwitterNotificationService() {
System.out.println("TwitterNotificationService Constructor");
}
}


AppConfig.java

package com.knf.dev.demo.config;

import com.knf.dev.demo.service.EmailNotificationService;
import com.knf.dev.demo.service.TwitterNotificationService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean("emailNotification")
@ConditionalOnBean(name = "emailNotificationProvider")
public EmailNotificationService emailNotificationService()
{
return new EmailNotificationService();
}

@Bean("twitterNotification")
public TwitterNotificationService twitterNotificationService()
{
return new TwitterNotificationService();
}
}


SMSNotificationService.java

package com.knf.dev.demo.service;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;

@Service("smsNotification")
@ConditionalOnBean(name = "smsNotificationProvider")
public class SMSNotificationService {

public SMSNotificationService() {
System.out.println("SMSNotificationService Constructor");
}
}


Run the application - Application.java

package com.knf.dev.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(Application.class, args);

context.close();
}
}

Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning. 

Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:

mvn spring-boot:run

Console Output:
TwitterNotificationService Constructor


Download Source Code

More related topics,

Spring Core Annotations

Spring Web Annotations

Spring Boot Annotations

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete