这篇文章主要为大家介绍了SpringBoot底层注解@Conditional的使用分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
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");
}
}
到此这篇关于SpringBoot深入探究@Conditional条件装配的使用的文章就介绍到这了,更多相关SpringBoot @Conditional内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:SpringBoot深入探究@Conditional条件装配的使用
基础教程推荐
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- Java实现查找文件和替换文件内容 2023-04-06
- Java并发编程进阶之线程控制篇 2023-03-07
- Java文件管理操作的知识点整理 2023-05-19
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- Java实现线程插队的示例代码 2022-09-03
- springboot自定义starter方法及注解实例 2023-03-31
- java基础知识之FileInputStream流的使用 2023-08-11
- Java数据结构之对象比较详解 2023-03-07
- java实现多人聊天系统 2023-05-19