Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication Examples
Condition On Web And Not Web
The Spring @ConditionalOnWebApplication (condition on web applications) and @ConditionalOnNotWebApplication (condition on non-web applications) annotations let configuration be included depending on whether the application is a web application.
WebApplicationContext, defines a session scope, or has a StandardServletEnvironment.Prerequisites
Java 12/19/22, Spring Boot 2.1.7 – 2.4.5/3.1.5/3.5.4, Maven 3.6.3/3.8.5/3.9.8
Project Setup
Create a maven based project in your favorite IDE or tool with the project name as spring-conditional-on-web-notweb-application.
The following pom.xml file can be used for your project:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.roytuts</groupId>
<artifactId>spring-conditional-on-web-notweb-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>22</maven.compiler.release>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.4</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project> 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.
public class Module {
} Main Class
The main class will be used to run our application.
@SpringBootApplication
public class SpringConditionalOnWebNotWebApp implements CommandLineRunner {
@Autowired
private ApplicationContext applicationContext;
public static void main(String[] args) {
SpringApplication.run(SpringConditionalOnWebNotWebApp.class, args).close();
}
@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:
@Configuration
@ConditionalOnWebApplication
class SpringConfigOnWebNotWebApp {
@Bean
public Module module() {
return new Module();
}
} Testing Condition On Web 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:
@Configuration
@ConditionalOnNotWebApplication
class SpringConfigOnWebNotWebApp {
@Bean
public Module module() {
return new Module();
}
} Testing Condition On Not Web 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.
No comments