Spring Boot @ConditionalOnResource Annotation Example
In this section we will learn about @ConditionalOnResource Annotation.
The @ConditionalOnResource annotation may be used on any class annotated with @Configuration, @Component, @Service & @Repository or on methods annotated with @Bean.
1. Using @ConditionalOnProperty on @Bean method and @Configuration class
For example,
The EmailNotificationService class is only loaded if the notification configuration file (notification.properties) was found on the classpath.
The TwitterNotificationService class is only loaded if the notification and message configuration files (notification.properties and message.properties) was found on the classpath.
@Configuration
@ConditionalOnResource(resources = {"notification.properties"})
public class AppConfig {
@Bean("emailNotification")
public EmailNotificationService emailNotificationService()
{
return new EmailNotificationService();
}
@ConditionalOnResource(resources = {"message.properties"})
@Bean("twitterNotification")
public TwitterNotificationService twitterNotificationService()
{
return new TwitterNotificationService();
}
}
2. Using @ConditionalOnResource on @Service class
For example, the SMSNotificationService class is only loaded if the notification configuration file (notification.properties) was found on the classpath.
@Service("smsNotification")
@ConditionalOnResource(resources = {"notification.properties"})
public class SMSNotificationService {
public SMSNotificationService(){
System.out.println("UserService Constructor");
}
}
@Service("smsNotification")
@ConditionalOnResource(resources = {"notification.properties"})
public class SMSNotificationService {
public SMSNotificationService(){
System.out.println("UserService Constructor");
}
}
The following example creates a Spring Boot web application which uses @ConditionalOnResource 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-conditionalonresource-annotation-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-conditionalonresource-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.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnResource(resources = {"notification.properties"})
public class AppConfig {
@Bean("emailNotification")
public EmailNotificationService emailNotificationService()
{
return new EmailNotificationService();
}
@ConditionalOnResource(resources = {"message.properties"})
@Bean("twitterNotification")
public TwitterNotificationService twitterNotificationService()
{
return new TwitterNotificationService();
}
}
SMSNotificationService.java
package com.knf.dev.demo.service;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.stereotype.Service;
@Service("smsNotification")
@ConditionalOnResource(resources = {"notification.properties"})
public class SMSNotificationService {
public SMSNotificationService(){
System.out.println("UserService 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;
import java.util.Arrays;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(Application.class, args);
String[] beans = context.getBeanDefinitionNames();
boolean containsEmailNotification = Arrays.stream(beans)
.anyMatch("emailNotification"::equalsIgnoreCase);
boolean containsSMSNotification = Arrays.stream(beans)
.anyMatch("smsNotification"::equalsIgnoreCase);
boolean containsTwitterNotification = Arrays.stream(beans)
.anyMatch("twitterNotification"::equalsIgnoreCase);
if (containsTwitterNotification) {
System.out.println("Twitter Notification loaded");
} else {
System.out.println("Twitter Notification not loaded");
}
if (containsEmailNotification) {
System.out.println("Email Notification loaded");
} else {
System.out.println("Email Notification not loaded");
}
if (containsSMSNotification) {
System.out.println("SMS Notification loaded");
} else {
System.out.println("SMS Notification not loaded");
}
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;
import java.util.Arrays;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(Application.class, args);
String[] beans = context.getBeanDefinitionNames();
boolean containsEmailNotification = Arrays.stream(beans)
.anyMatch("emailNotification"::equalsIgnoreCase);
boolean containsSMSNotification = Arrays.stream(beans)
.anyMatch("smsNotification"::equalsIgnoreCase);
boolean containsTwitterNotification = Arrays.stream(beans)
.anyMatch("twitterNotification"::equalsIgnoreCase);
if (containsTwitterNotification) {
System.out.println("Twitter Notification loaded");
} else {
System.out.println("Twitter Notification not loaded");
}
if (containsEmailNotification) {
System.out.println("Email Notification loaded");
} else {
System.out.println("Email Notification not loaded");
}
if (containsSMSNotification) {
System.out.println("SMS Notification loaded");
} else {
System.out.println("SMS Notification not loaded");
}
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
UserService Constructor
EmailNotificationService Constructor
Twitter Notification not loaded
Email Notification loaded
SMS Notification loaded