Spring Boot @ConditionalOnClass Annotation Example
In this section we will learn about @ConditionalOnClass Annotation.
For example, when providing library/framework classes to other developers, this annotation can be used to active certain beans only based on the existing of specific classes, e.g. driver-classes etc.
The @ConditionalOnClass annotation may be used on any class annotated with @Configuration, @Component, @Service & @Repository or on methods annotated with @Bean.
1. Using @ConditionalOnClass on @Bean method
To illustrate the use of @ConditionalOnClass, we will create two simple service classes, SMSNotificationService class and EmailNotificationService class.
public class EmailNotificationService {
public EmailNotificationService() {
System.out.println("Inside EmailNotificationService Constructor");
}
public void sendEmailNotification()
{
System.out.println("Sending Email Notification");
}
}
package com.knf.dev.demo.service;
public class SMSNotificationService {
public SMSNotificationService() {
System.out.println("Inside SMSNotificationService Constructor");
}
public void sendSmsNotification()
{
System.out.println("Sending SMS Notification");
}
}
Next, we will create a simple class which will be required to call on @ConditionalOnClass annotation because this class will be executed only when the required class is found.
public class NotificationProvider {
}
Next, we will create a Spring Config class and use annotation @ConditionalOnClass in various ways, such as, by name, value.
@Configuration
public class AppConfig {
@Bean
@ConditionalOnClass(name =
"com.knf.dev.demo.provider.NotificationProvider")
public SMSNotificationService smsNotificationService()
{
return new SMSNotificationService();
}
@Bean
@ConditionalOnClass(NotificationProvider.class)
public EmailNotificationService emailNotificationService()
{
return new EmailNotificationService();
}
}
Here, SMSNotificationService bean and EmailNotificationService bean will register to the application context if NotificationProvider class is found.
2. Using @ConditionalOnClass on @Service class
@Service
@ConditionalOnClass(name=
"com.knf.dev.demo.provider.NotificationProvider")
public class TwitterNotificationService {
public TwitterNotificationService() {
System.out.println("Inside TwitterNotificationService Constructor");
}
public void sendTwitterNotification()
{
System.out.println("Sending Twitter Notification");
}
}
@Service
@ConditionalOnClass(name=
"com.knf.dev.demo.provider.NotificationProvider")
public class TwitterNotificationService {
public TwitterNotificationService() {
System.out.println("Inside TwitterNotificationService Constructor");
}
public void sendTwitterNotification()
{
System.out.println("Sending Twitter Notification");
}
}
Here TwitterNotificationService bean will register to the application context if NotificationProvider class is found.
The following example creates a Spring Boot web application which uses @ConditionalOnClass 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-conditionalonclass-annotation-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-conditionalonclass-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("Inside EmailNotificationService Constructor");
}
public void sendEmailNotification()
{
System.out.println("Sending Email Notification");
}
}
package com.knf.dev.demo.service;
public class EmailNotificationService {
public EmailNotificationService() {
System.out.println("Inside EmailNotificationService Constructor");
}
public void sendEmailNotification()
{
System.out.println("Sending Email Notification");
}
}
SMSNotificationService.java
package com.knf.dev.demo.service;
public class SMSNotificationService {
public SMSNotificationService() {
System.out.println("Inside SMSNotificationService Constructor");
}
public void sendSmsNotification()
{
System.out.println("Sending SMS Notification");
}
}
package com.knf.dev.demo.service;
public class SMSNotificationService {
public SMSNotificationService() {
System.out.println("Inside SMSNotificationService Constructor");
}
public void sendSmsNotification()
{
System.out.println("Sending SMS Notification");
}
}
NotificationProvider.java
package com.knf.dev.demo.provider;
public class NotificationProvider {
}
package com.knf.dev.demo.provider;
public class NotificationProvider {
}
AppConfig.java
package com.knf.dev.demo.config;
import com.knf.dev.demo.provider.NotificationProvider;
import com.knf.dev.demo.service.EmailNotificationService;
import com.knf.dev.demo.service.SMSNotificationService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
@ConditionalOnClass(name =
"com.knf.dev.demo.provider.NotificationProvider")
public SMSNotificationService smsNotificationService()
{
return new SMSNotificationService();
}
@Bean
@ConditionalOnClass(NotificationProvider.class)
public EmailNotificationService emailNotificationService()
{
return new EmailNotificationService();
}
}
package com.knf.dev.demo.config;
import com.knf.dev.demo.provider.NotificationProvider;
import com.knf.dev.demo.service.EmailNotificationService;
import com.knf.dev.demo.service.SMSNotificationService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
@ConditionalOnClass(name =
"com.knf.dev.demo.provider.NotificationProvider")
public SMSNotificationService smsNotificationService()
{
return new SMSNotificationService();
}
@Bean
@ConditionalOnClass(NotificationProvider.class)
public EmailNotificationService emailNotificationService()
{
return new EmailNotificationService();
}
}
TwitterNotificationService.java
package com.knf.dev.demo.service;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Service;
@Service
@ConditionalOnClass(name=
"com.knf.dev.demo.provider.NotificationProvider")
public class TwitterNotificationService {
public TwitterNotificationService() {
System.out.println("Inside TwitterNotificationService Constructor");
}
public void sendTwitterNotification()
{
System.out.println("Sending Twitter Notification");
}
}
package com.knf.dev.demo.service;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Service;
@Service
@ConditionalOnClass(name=
"com.knf.dev.demo.provider.NotificationProvider")
public class TwitterNotificationService {
public TwitterNotificationService() {
System.out.println("Inside TwitterNotificationService Constructor");
}
public void sendTwitterNotification()
{
System.out.println("Sending Twitter Notification");
}
}
Run the application - Application.java
package com.knf.dev.demo;
import com.knf.dev.demo.service.EmailNotificationService;
import com.knf.dev.demo.service.SMSNotificationService;
import com.knf.dev.demo.service.TwitterNotificationService;
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);
SMSNotificationService smsNotificationService =
context.getBean(SMSNotificationService.class);
smsNotificationService.sendSmsNotification();
EmailNotificationService emailNotificationService =
context.getBean(EmailNotificationService.class);
emailNotificationService.sendEmailNotification();
TwitterNotificationService twitterNotificationService =
context.getBean(TwitterNotificationService.class);
twitterNotificationService.sendTwitterNotification();
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
Inside TwitterNotificationService Constructor
Inside SMSNotificationService Constructor
Inside EmailNotificationService Constructor
Sending SMS Notification
Sending Email Notification
Sending Twitter Notification
Download Source Code
More related topics,