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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章