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內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- SpringBoot圖文並茂講解Lombok庫的安裝與使用
- Spring自動配置之condition條件判斷上篇
- 詳解如何實現SpringBoot的底層註解
- SpringBoot整合BCrypt實現密碼加密
- springboot使用Redis作緩存使用入門教程