Introduction

The Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations let configuration be included depending on whether the application is a web application. A web application is any application that uses a Spring WebApplicationContext, defines a session scope, or has a StandardServletEnvironment.

Prerequisites

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

Creating Project

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

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 Simple Class

We will create a simple class Module that will be used for both Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations to check whether the Module class is loaded or not using the said annotations.

package com.roytuts.spring.conditional.on.web.notweb.application;
public class Module {
}

Creating Main Class

The main class will be used to run our application.

package com.roytuts.spring.conditional.on.web.notweb.application;
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 SpringConditionalOnWebNotWebApp implements CommandLineRunner {
	@Autowired
	private ApplicationContext applicationContext;
	public static void main(String[] args) {
		SpringApplication.run(SpringConditionalOnWebNotWebApp.class, args);
	}
	@Override
	public void run(String... args) throws Exception {
		String[] beans = applicationContext.getBeanDefinitionNames();
		Arrays.sort(beans);
		boolean contains = Arrays.stream(beans).anyMatch("module"::equalsIgnoreCase);
		if (contains) {
			System.out.println("Module loaded");
		} else {
			System.out.println("Module not loaded");
		}
	}
}

Creating Config Class

We will create a Spring Config class to use the Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication and check the behavior of the application.

Example on @ConditionalOnWebApplication

Creating Config class with below source code:

package com.roytuts.spring.conditional.on.web.notweb.application;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication
class SpringConfigOnWebNotWebApp {
	@Bean
	public Module module() {
		return new Module();
	}
}

Testing the Application

Executing the main class you will see output as Module loaded. Therefore it’s a web application.

Example on @ConditionalOnNotWebApplication

Creating Config class with below source code:

package com.roytuts.spring.conditional.on.web.notweb.application;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnNotWebApplication
class SpringConfigOnWebNotWebApp {
	@Bean
	public Module module() {
		return new Module();
	}
}

Testing the Application

Executing the main class you will see output as Module not loaded. Again it’s a web application and that’s why module was not loaded.

Source Code

You can download source code.

Thanks for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *