Spring Boot @ConditionalOnMissingBean Annotation Example
In this section we will learn about @ConditionalOnMissingBean Annotation.
@Bean
@ConditionalOnMissingBean
public NotificationService emailNotificationService()
{
return new EmailNotificationService();
}
@Bean
@ConditionalOnMissingBean
public NotificationService emailNotificationService()
{
return new EmailNotificationService();
}
This example loads the EmailNotificationService into the application context if there is no other NotificationService exist in application context.
The following example creates a Spring Boot web application which uses @ConditionalOnMissingBean 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-conditionalonmissingbean-annotation-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-conditionalonmissingbean-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>
NotificationService.java
package com.knf.dev.demo.service;
public interface NotificationService {
}
EmailNotificationService.java
package com.knf.dev.demo.service;
public class EmailNotificationService implements NotificationService {
public EmailNotificationService() {
System.out.println("EmailNotificationService Constructor");
}
}
SMSNotificationService.java
package com.knf.dev.demo.service;
public class SMSNotificationService implements NotificationService{
public SMSNotificationService() {
System.out.println("SMSNotificationService Constructor");
}
}
AppConfig.java
package com.knf.dev.demo.config;
import com.knf.dev.demo.service.EmailNotificationService;
import com.knf.dev.demo.service.NotificationService;
import com.knf.dev.demo.service.SMSNotificationService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public NotificationService smsNotificationService()
{
return new SMSNotificationService();
}
@Bean
@ConditionalOnMissingBean
public NotificationService emailNotificationService()
{
return new EmailNotificationService();
}
}
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:
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.
mvn spring-boot:run
Console Output:
SMSNotificationService Constructor
Download Source Code
More related topics,