zoukankan      html  css  js  c++  java
  • @ConditionalOnProperty来控制Configuration是否生效

    1. 简介

    Spring Boot通过@ConditionalOnProperty来控制Configuration是否生效

    2. 说明

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Documented
    @Conditional(OnPropertyCondition.class)
    public @interface ConditionalOnProperty {
    
        String[] value() default {}; //数组,获取对应property名称的值,与name不可同时使用  
      
        String prefix() default "";//property名称的前缀,可有可无  
      
        String[] name() default {};//数组,property完整名称或部分名称(可与prefix组合使用,组成完整的property名称),与value不可同时使用  
      
        String havingValue() default "";//可与name组合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置  
      
        boolean matchIfMissing() default false;//缺少该property时是否可以加载。如果为true,没有该property也会正常加载;反之报错  
      
        boolean relaxedNames() default true;//是否可以松散匹配  
    } 

    3. 使用方法

    通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值。
    如果该值为空,则返回false;
    如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。
    如果返回值为false,则该configuration不生效;为true则生效。

    4.使用示例

    我们来模拟通过application.yml配置文件来配置我们的服务是使用Nosql型的数据库还是sql数据库

    application.yml的配置如下:

    database:
      type: "${DATABASE_TYPE:cassandra}" # cassandra OR sql

    这里我们分别定义Nosql的注解与sql的注解

    @ConditionalOnProperty(prefix = "database", value = "type", havingValue = "cassandra")
    public @interface NoSqlDao {
    }
    
    @ConditionalOnProperty(prefix = "database", value = "type", havingValue = "sql")
    public @interface SqlDao {
    }

    然后简单的写一下dao的实现:

    public interface DeviceDao {
        List<Device> findDevicesByCustomerId(UUID customerId);
    }
    
    @Component
    @SqlDao
    public class JpaDeviceDao implements DeviceDao {
        @Override
        public List<Device> findDevicesByCustomerId(UUID customerId) {
            return null;
        }
    }
    
    @Component
    @NoSqlDao
    public class CassandraDeviceDao implements DeviceDao {
    
        @Override
        public List<Device> findDevicesByCustomerId(UUID customerId) {
            return null;
        }
    }

    那么当我们想使用cassandra数据库的时候,只需要更改yml配置文件为cassandra。

    而在DeviceServiceImpl中,代码则不需要进行更改,因为此时@ConditionalOnProperty注解让我们的JpaDeviceDao的注入失效了,所以会自动匹配CassandraDao

    @Autowired
    private DeviceDao deviceDao;

    参考链接:https://www.jianshu.com/p/7bfe623cabcf

  • 相关阅读:
    Codeforces 992C(数学)
    Codeforces 990C (思维)
    Codeforces 989C (构造)
    POJ 1511 Invitation Cards(链式前向星,dij,反向建边)
    Codeforces 1335E2 Three Blocks Palindrome (hard version)(暴力)
    POJ 3273 Monthly Expense(二分)
    POJ 2566 Bound Found(尺取前缀和)
    POJ 1321 棋盘问题(dfs)
    HDU 1506 Largest Rectangle in a Histogram(单调栈)
    POJ 2823 Sliding Window(单调队列)
  • 原文地址:https://www.cnblogs.com/chongaizhen/p/11038334.html
Copyright © 2011-2022 走看看