This article brings you what is the @Conditional annotation in SpringBoot? how to use? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Some features of the @Conditional annotation have been used in recent projects, so I wrote this article to record
What is @Conditional?
What is the @Conditional annotation? It can load different beans according to the conditions set in the code. For example, when an interface has two implementation classes, we usually hand over the interface to Spring for management. Only one of the implementation classes will be chosen to implement. We can't use if-else at this time, so the @Conditional annotation appears.
How do I use it?
In SpringBoot, @Conditional annotations are not alone, they are a family. Let’s take a look at several of their members and how they are used
@ ConditionalOnBean
Sample code
@Bean @ConditionalOnBean(RedisConnectionFactory.class) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); template.setConnectionFactory(connectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; }
The @ConditionalOnBean annotation is added to this method, and the attribute in the annotation is RedisConnectionFactory. What it means is that if you configure the relevant configuration information of redis, then I will instantiate the RedisTemplate for you to operate. If you do not configure the relevant configuration of redis, then I will not instantiate it (after all, even if there is no configuration, instantiation will report an error. )
Not only that, you can also play like this:
According to the name of the bean @ConditionalOnMissingBean(name = “connectionFactory”) or @ConditionalOnMissingBean(annotation = EnableSyjRateLimit.class), you can perform various operations based on annotations and so on.
@ConditionalOnMissingBean
This is just one more Missing than 1. What does it mean? As the name suggests, it is instantiated when a certain bean does not exist.@ConditionalOnClass (A Bean will be instantiated only when a certain class exists)
@ConditionalOnMissingClass (When a certain class does not exist, Only then will a Bean be instantiated)
@ConditionalOnProperty(prefix = “syj”, name = “algorithm”, havingValue = “token”)
This is a little more complicated. What it means is that when there is an attribute prefixed with syj in the configuration file, the attribute name is algorithm, and then its value is token, a class will be instantiated.
And this one also has a better attribute
@ConditionalOnProperty(prefix = “syj”, name = “algorithm”, havingValue = “counter”, matchIfMissing = true)
What does matchIfMissing mean? That is to say, if all are not satisfied, it will be implemented by default, regardless of whether the attribute syj.algorithm is equal to counter@ConditionalOnJava (if it is a Java application)
@ConditionalOnWebApplication (if it is a Web application)
In fact, when it comes to the members of the @Conditional family, we have only talked about a small part of them
There are probably so many family members:
If you want to know more about children's shoes, you can refer to this package
org.springframework.boot.autoconfigure.condition
Although you talked a lot above, I still Not satisfied
Why are you not satisfied? In fact, there are many reasons. For example, you have not used SpringBoot, and you want to customize it. So what should we do at this time? Let’s customize one.
First customize a rule class
public class MyCondition implements Condition { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //在这里写你的逻辑,比如说你想a>0时实例化类A,a<0时不实现 return a>0; } }
Then you can use it
@Bean @Conditional(MyCondition.class) public A a(){ return new A() }
The above is the detailed content of What is the @Conditional annotation in SpringBoot? how to use?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment