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 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.
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
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!