Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a configuration conditional on another configuration?

Tags:

spring-boot

I want to make my spring-boot configuration class A dependent on another configuration class B, i.e. A configuration is evaluated only if B configuration is evaluated.

In the real context, I have hundreds of Ai configurations and only one B, and I want to implement a way to exclude all the Ai configs by excluding only B during tests.

I tried the following:

@Configuration
@ConditionalOnBean(type = "org.my.B")
public class A1AutoConfiguration {
// ...
}

Where B is a unconditioned configuration class.

But when I run mvn spring-boot:run -Ddebug=true I see that A is never evaluated because B is missing. While the beans created inside B are in the application context, B itself is not.

I though I can make the Ai configuration classes dependent on beans created inside B but I don't like so much this solution.

Is there a cleaner (and working) way to implement such a dependency mechanism?

like image 698
Nicola Ferraro Avatar asked Nov 25 '16 12:11

Nicola Ferraro


People also ask

What is the difference between @configuration and AutoConfiguration?

AutoConfiguration classes are run last (meaning after all regular non-autoconfiguration classes) while the order in which Configuration classes are run is indeterminate (except if we use ordering annotations like @Ordered ) To declare a class as an AutoConfiguration they need to be specified as such in the spring.

What is @configuration when to use it?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.

How do you make conditionally beans?

To conditionally create a bean, we must first create an implementation of Condition. The Condition interface contains the matches method which returns a boolean value. Here, the AuditEnabledCondition class is checking whether audit. enabled is true using the Environment properties.

How do you instruct an auto configuration to back off when a bean exists?

How to Tell an Auto-Configuration to Back Away When a Bean Exists? To instruct an auto-configuration class to back off when a bean already exists, we can use the @ConditionalOnMissingBean annotation.


1 Answers

The key is to make sure that things are ordered correctly. It does not make any sense to request A to only apply if B is present if you can't make sure that B is evaluated first.

The hundreds part frightens me a bit. If As and B are auto-configuration, you can use the following

@AutoconfigureAfter(B.class)
@ConditionalOnBean(B.class)
public class A123AutoConfiguration { ...}

If As and B are not auto-configuration, you need to make sure B is processed first so you can't rely on regular classpath scanning for those.

like image 127
Stephane Nicoll Avatar answered Jun 08 '23 15:06

Stephane Nicoll