SpringBoot深入探究@Conditional条件装配的使用
作者:鸣鼓ming 时间:2021-08-18 00:06:53
1.相关介绍
@Conditional注解可以用在任何类型或者方法上面,通过@Conditional注解可以配置一些条件判断,当所有条件都满足的时候,被@Conditional标注的目标才会被spring容器处理。
比如可以通过@Conditional来控制bean是否需要注册,控制被@Configuration标注的配置类是需要需要被解析等。
Conditional相关的其它注解:
注解 | 描述 |
---|---|
@ConditionalOnBean | 在某个 bean 存在的时候 |
@ConditionalOnMissingBean | 在某个 bean 不存在的时候 |
@ConditionalOnClass | 当前 classPath 下可以找到某个 class 的时候 |
@ConditionalOnMissingClass | 当前 classPath 下无法找到某个 class 的时候 |
@ConditionalOnResource | 当前 classPath 下否存在某个资源文件的时候 |
@ConditionalOnProperty | 当前 JVM 包含某个属性值的时候 |
@ConditionalOnWebApplication | 当前 Spring context 是 web 应用程序的时候 |
@ConditionalOnJava | 在指定JDK版本的时候 |
@ConditionalOnExpression | 符合表达式的时候 |
@ConditionalOnJndi | JNDI存在该项的时候 |
2.举例测试
@ConditionalOnBean, 在某个 bean 存在的时候被标注的组件才被注册到IOC容器中
下面是其详细代码
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({OnBeanCondition.class})
public @interface ConditionalOnBean {
Class<?>[] value() default {};
String[] type() default {};
Class<? extends Annotation>[] annotation() default {};
String[] name() default {};
SearchStrategy search() default SearchStrategy.ALL;
Class<?>[] parameterizedContainer() default {};
}
我们就拿它来测试一下
1.指定组件不存在时
MyConfig
package com.limi.config;
import com.limi.entity.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.*;
@Configuration
public class MyConfig {
// @Bean
// public User user01(){
// return new User(1, "andy", "123456");
// }
@Bean
@ConditionalOnBean(name = "user01") //当user01存在时才注册user02
public User user02(){
return new User(2, "bob", "666666");
}
}
MainApplication
package com.limi;
import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
if(run.containsBean("user01"))
System.out.println((User)run.getBean("user01"));
System.out.println("user01 不存在!\n");
if(run.containsBean("user02"))
System.out.println((User)run.getBean("user02"));
else
System.out.println("user02 不存在!\n");
}
}
运行结果
2.指定组件存在时
MyConfig
package com.limi.config;
import com.limi.entity.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.*;
@Configuration
public class MyConfig {
@Bean
public User user01(){
return new User(1, "andy", "123456");
@Bean
@ConditionalOnBean(name = "user01") //当user01存在时才注册user02
public User user02(){
return new User(2, "bob", "666666");
}
}
MainApplication
package com.limi;
import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
if(run.containsBean("user01"))
System.out.println((User)run.getBean("user01"));
else
System.out.println("user01 不存在!\n");
if(run.containsBean("user02"))
System.out.println((User)run.getBean("user02"));
else
System.out.println("user02 不存在!\n");
}
}
运行结果
3.完整代码
pom.xml
<?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.limi</groupId>
<artifactId>springboot-test1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--打包插件, 使用它可以把项目打包为jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
User
package com.limi.entity;
public class User {
private Integer id;
private String userName;
private String passWord;
public User(){}
public User(Integer id, String userName, String passWord) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
MyConfig
package com.limi.config;
import com.limi.entity.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.*;
@Configuration
public class MyConfig {
@Bean
public User user01(){
return new User(1, "andy", "123456");
}
@Bean
@ConditionalOnBean(name = "user01") //当user01存在时才注册user02
public User user02(){
return new User(2, "bob", "666666");
}
}
MainApplication
package com.limi;
import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
if(run.containsBean("user01"))
System.out.println((User)run.getBean("user01"));
else
System.out.println("user01 不存在!\n");
if(run.containsBean("user02"))
System.out.println((User)run.getBean("user02"));
else
System.out.println("user02 不存在!\n");
}
}
来源:https://blog.csdn.net/qq_41865229/article/details/125185114
标签:SpringBoot,@Conditional,条件装配
0
投稿
猜你喜欢
Springboot @Validated和@Valid的区别及使用详解
2023-05-30 18:40:25
springboot中使用FastJson解决long类型在js中失去精度的问题
2022-04-05 05:15:27
C#正则表达式Regex类的用法
2022-06-07 13:44:58
聊聊Java 中的线程中断
2021-05-31 02:04:30
Java中ArrayList和LinkedList的遍历与性能分析
2023-10-31 11:04:17
HashMap在JDK7与JDK8中的实现过程解析
2022-03-04 18:26:44
使用Spring Security OAuth2实现单点登录
2023-08-13 01:44:34
java Class.getSimpleName() 详解及用法
2021-06-26 16:35:41
Spring Security 图片验证码功能的实例代码
2023-11-17 14:23:56
Java Lambda List转Map代码实例
2022-05-24 20:15:42
Android实现手势滑动多点触摸放大缩小图片效果
2022-02-13 09:26:28
在C#中新手易犯的典型缺陷
2023-08-26 01:24:20
Java重写equals及hashcode方法流程解析
2023-10-14 06:53:13
分享两种实现Winform程序的多语言支持的多种解决方案
2023-10-17 21:24:17
Java通俗易懂系列设计模式之装饰模式
2023-08-07 15:41:28
Android事件冲突解决悬浮窗拖拽处理方案
2023-12-17 01:51:17
Android实现简单画中画功能
2022-07-18 04:51:25
C#中new的几种用法详解
2021-09-16 23:05:56
C#基于Sockets类实现TCP通讯
2023-03-22 05:08:30
SpringBoot通过源码探究静态资源的映射规则实现
2022-03-26 19:05:53