Spring @ConditionalOnResource Example

Introduction

We will create examples on Spring @ConditionalOnResource. The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present in the classpath. For our example the Log4j class is only loaded if the log4j configuration file was found on the classpath. This way, we might create similar modules that are only loaded if their respective configuration file has been found.

Prerequisites

Eclipse 2019-06 (4.12), Java 12, Gradle 5.6, Spring Boot 2.1.7

Creating Project

Create a gradle based project in Eclipse with the project name as spring-conditional-on-resource.

Updating Build Script

The default generated build.gradle script does not include required dependencies, so we will update the build script as follows to include the spring boot dependencies.

buildscript {
	ext {
		springBootVersion = '2.1.7.RELEASE'
	}
    repositories {
    	mavenLocal()
    	mavenCentral()
    }
    dependencies {
    	classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 12
targetCompatibility = 12
repositories {
	mavenLocal()
    mavenCentral()
}
dependencies {
	implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
}

Creating Log4j Class

Create a simple Log4j class just to check whether it is loaded or not based on condition.

package com.roytuts.spring.conditional.on.resource;
public class Log4j {
}

Creating SpringConfig Class

Create below SpringConfig class to load the Log4j class conditionally.

So if log4j.properties file is found in the classpath then only Log4j class will be loaded otherwise not.

package com.roytuts.spring.conditional.on.resource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnResource(resources = { "log4j.properties" })
class SpringConfig {
	@Bean
	public Log4j log4j() {
		return new Log4j();
	}
}

Creating Main Class

Create main class to test our application.

package com.roytuts.spring.conditional.on.resource;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class SpringConditionalOnResourceApp implements CommandLineRunner {
	@Autowired
	private ApplicationContext applicationContext;
	public static void main(String[] args) {
		SpringApplication.run(SpringConditionalOnResourceApp.class, args);
	}
	@Override
	public void run(String... args) throws Exception {
		String[] beans = applicationContext.getBeanDefinitionNames();
		Arrays.sort(beans);
		boolean contains = Arrays.stream(beans).anyMatch("log4j"::equalsIgnoreCase);
		if (contains) {
			System.out.println("Log4j loaded");
		} else {
			System.out.println("Log4j not loaded");
		}
	}
}

Testing the Application

Now if you run the above main class, you will see below output:

Log4j not loaded

Now if you put the log4j.properties file under classpath directory src/main/resources then you will see below output:

Log4j loaded

Source Code

download source code

Thanks for reading.

Related posts

Leave a Comment