Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication Examples

Last Updated on May 19th, 2021 at 03:56 pm

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

Java at least 8, Spring Boot 2.1.7 – 2.4.5

Project Setup

Create a maven based project in your favorite IDE or tool with the project name asĀ spring-conditional-on-web-notweb-application.

Simple Class

I 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 {
}

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");
		}
	}
}

Spring Config Class

I 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

Download

Leave a Reply

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