在Spring中实现类似SpringBoot的环境检测能力

在Spring中实现类似SpringBoot的环境检测能力

前言

​ 在Boot 你的应用一文中提到了有时候我们需要检测当前时环境是否匹配我们的运行时要求,并根据不同的环境进行个性化的适配。

Spring4已经引入了简单的扩展接口 @ConditionalCondition,允许大家自行去识别环境信息,但也仅此而已,并没有内置一些可以让大家在实际场景中使用的条件判定器。

​ 真正将 @ConditionalCondition发扬光大的是SpringBoot,在SpringBoot是全面采用了 AutoConfiguration@Conditional将自动配置的强大功能展现得淋漓尽致,内置了超过10种不同类型支持超过100种不同场景的环境检测器。比如:检测当前环境中是否存在某个Class,检测当前容器中是否定义了某个SpringBean,检测当前是否有某个配置项,配置项的值是多少等等。所有的环境检测器都在 org.springframework.boot.autoconfigure.condition 下面,大家可以去翻阅源码学习了解。

@Conditional 与 Condition 介绍

​ 前文提到在 Spring 框架中仅仅提供了这两个扩展点,并没有能运用在实际应用场景中的环境检测器,这一节我们将分析这两个接口,并实现一个简单的环境检测功能。

​ 以下是@Conditional的源码:

/**
 * Indicates that a component is only eligible for registration when all
 * {@linkplain #value() specified conditions} match.
 *
 * <p>A <em>condition</em> is any state that can be determined programmatically
 * before the bean definition is due to be registered (see {@link Condition} for details).
 *
 * <p>The {@code @Conditional} annotation may be used in any of the following ways:
 * <ul>
 * <li>as a type-level annotation on any class directly or indirectly annotated with
 * {@code @Component}, including {@link Configuration @Configuration} classes</li>
 * <li>as a meta-annotation, for the purpose of composing custom stereotype
 * annotations</li>
 * <li>as a method-level annotation on any {@link Bean @Bean} method</li>
 * </ul>
 *
 * <p>If a {@code @Configuration} class is marked with {@code @Conditional}, all of the
 * {@code @Bean} methods, {@link Import @Import} and {@link ComponentScan @ComponentScan}
 * annotations associated with that class will be subject to the conditions.
 *
 * <p>NOTE: {@code @Conditional} annotations are not inherited; any conditions from
 * superclasses or from overridden methods are not being considered.
 *
 * @author Phillip Webb
 * @since 4.0
 * @see Condition
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Conditional {

    /**
     * All {@link Condition}s that must {@linkplain Condition#matches match}
     * in order for the component to be registered.
     */
    Class<? extends Condition>[] value();

}

​ 这是一个注解,从注释中我们看到这是 @Since 4.0 的,即在 Spring4 开始提供的,用来指定一系列的配置条件,当所有指定的条件都满足时,被 @Configuration 中标注的 @Bean@Import@ComponentScan才会生效。

​ 它接受一个Condition数组,用来标记所有的筛选条件,当所有的Condition.matches条件均返回true时即可认为该Conditional成立,从而完成环境检测。

Condition接口只有一个方法,源码如下:

/**
 * A single {@code condition} that must be {@linkplain #matches matched} in order
 * for a component to be registered.
 *
 * <p>Conditions are checked immediately before the bean-definition is due to be
 * registered and are free to veto registration based on any criteria that can
 * be determined at that point.
 *
 * <p>Conditions must follow the same restrictions as {@link BeanFactoryPostProcessor}
 * and take care to never interact with bean instances. For more fine-grained control
 * of conditions that interact with {@code @Configuration} beans consider the
 * {@link ConfigurationCondition} interface.
 *
 * @author Phillip Webb
 * @since 4.0
 * @see ConfigurationCondition
 * @see Conditional
 * @see ConditionContext
 */
public interface Condition {

    /**
     * Determine if the condition matches.
     * @param context the condition context
     * @param metadata metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
     * or {@link org.springframework.core.type.MethodMetadata method} being checked.
     * @return {@code true} if the condition matches and the component can be registered
     * or {@code false} to veto registration.
     */
    boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);

}

​ 只需要实现matches方法并根据自己的需要完成环境检测判定即可。

简单用法示例

​ 下面我们用一个小的示例来演示这两个接口的使用方法,假设需求:根据不同的操作系统注册不同的 MXBean 服务

1. 实现在不同操作系统环境下的条件判定

​ 这个过程我们就简化地判定当前的os.name就可以,代码如下:

Windows 环境的判定器

/**
 * 判定当前环境是否为 Windows 的条件
 *
 * @author <a href="mailto:huangfengjing@gmail.com">Ivan</a>
 * created on 2018/12/10.
 */
public class WindowsCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Windows");
    }
}

Linux 环境的判定器

/**
 * 判定当前环境是否为 Windows 的条件
 *
 * @author <a href="mailto:huangfengjing@gmail.com">Ivan</a>
 * created on 2018/12/10.
 */
public class WindowsCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Linux");
    }
}

2. 在Bean注册时带上条件注解

​ 有了第1步的的条件判定器,那么在我们进行 @Configuraiton的Bean注册时就可以将这些条件附带上,让Spring容器根据不同的条件加载不同的Bean配置。代码如下所示:

/**
 * 根据不同的操作系统加载不同的 MXBean
 *
 * @author <a href="mailto:huangfengjing@gmail.com">Ivan</a>
 * created on 2018/12/10.
 */
@Configuration
public class ConditionalMXBeanConifg {
    
    @Bean
    @Conditional(WindowsCondition.class)
    public BaseMXBean windowsMXBeanService() {
        return new WindowsMXBean();
    }

    @Bean
    @Conditional(LinuxCondition.class)
    public BaseMXBean linuxMXBeanService() {
        return new LinuxMXBean();
    }
}

根据以上的配置,在不同的操作系统环境下,Spring会分别注册不同的 MXBean 。

高级用法示例

​ 在简单用法示例中我们可以看到,虽然实现了不同环境下的判定识别,但还是太简单了,还是比较静态的,如果我们要像SpringBoot那样动态的判定当前环境中是否存在某个类,Spring容器中是否存在某个Bean定义该怎么做呢?下面我们将演示这几种更高级的用法。

判定当前 classpath 下是否存在某个类

​ 这类条件判定器主要用在一些模板类SDK中,根据当前用户是否依赖了某些类来确定是否要定义相应的模板、工具、服务等。如同应用分发 base-boot-starter 的使用说明中对于 OA 权限平台的判定一样,当用户没有添加OA权限平台这个MAVEN依赖时,应用仍然能智能判定而不是抛出 NoClassDefFoundError

  1. 首先定义一个自定义的注解,供用户使用判定

    /**
     * 是否存在某个类的条件判定注解
     *
     * @author <a href="mailto:huangfengjing@gmail.com">Ivan</a>
     * Time: 2018/12/7 : 19:34
     */
    @Target({ElementType.TYPE, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Conditional(OnClassCondition.class)
    public @interface ConditionalOnClass {
    
        /**
         * 必须存在的类
         *
         * @return 必须存在的类
         */
        Class<?>[] value() default {};
    
        /**
         * 必须存在的类名
         *
         * @return 必须存在的类名
         */
        String[] name() default {};
    
    }
    

当用户在注解某个@Bean进,可以添加这个注解来进行判定。这个注解本身还依赖另外一个注解@Conditional(OnClassCondition.class),表示扫Spring容器在扫描到某个类定义被标注了@ConditionalOnClass时,会执行里面的OnClassCondition来完成条件判定。

  1. 定义真正的条件判定器 OnClassCondition

    /**
     * 判定某个类是否存在的条件
     *
     * @author <a href="mailto:huangfengjing@gmail.com">Ivan</a>
     * Time: 2018/12/7 : 19:29
     */
    public class OnClassCondition extends BaseCondition implements Condition {
    
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    
            MultiValueMap<String, Object> attributes = metadata
                    .getAllAnnotationAttributes(ConditionalOnClass.class.getName(), true);
            if (null == attributes) {
                return false;
            }
            List<String> candidates = new ArrayList<>();
            addAll(candidates, attributes.get("value"));
            addAll(candidates, attributes.get("name"));
    
            for (String candidate : candidates) {
                if (!ClassUtils.isPresent(candidate, null)) {
                    return false;
                }
            }
    
            return true;
        }
    }
    

    其实也很简单,就是从注解中获取当前用户要判定是否存在的Class(支持类定义,和全类名),然后在当前 classpath 下去查找这个类是否存在即完成判定过程。

  2. 使用自定义的注解

    使用起来就比较简单了,加上注解即可,如下所示:

    @ConditionalOnClass(SSOFilter.class)
    @Configuration
    public class SsoAutoConfiguration {...}
    

判定当前Spring容器中是否定义了某个Bean

​ 这类判定主要用在如下的场景:某些组件需要依赖某个SpringBean,如果当前Spring容器中不存在这个Bean,那么就要添加一个,如果存在就不能再添加,防止产生NoSuchBeanDefinitionException或者NoUniqueBeanDefinitionException异常。

​ 其实现过程其实和@ConditionalOnClass大同小异,最主要的区别在于Condition.matches方法一个是判定类是否存在,一个是判定Bean是否存在。代码如下:

/**
 * 判定某个 bean 是否存在的条件
 *
 * @author <a href="mailto:huangfengjing@gmail.com">Ivan</a>
 * Time: 2018/12/7 : 19:29
 */
@Slf4j
public class OnBeanCondition extends BaseCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        MultiValueMap<String, Object> conditionOnBeanAttrs = metadata
                .getAllAnnotationAttributes(ConditionalOnBean.class.getName(), true);
        if (null != conditionOnBeanAttrs) {
            return matchBean(context, conditionOnBeanAttrs, metadata);
        }

        MultiValueMap<String, Object> conditionOnMissingBeanAttrs = metadata
                .getAllAnnotationAttributes(ConditionalOnMissingBean.class.getName(), true);
        if (conditionOnMissingBeanAttrs != null) {
            return matchMissingBean(context, conditionOnMissingBeanAttrs, metadata);
        }
        return false;
    }

    private boolean matchBean(ConditionContext context, MultiValueMap<String, Object> attributes, AnnotatedTypeMetadata metadata) {
        if (attributes == null) {
            return false;
        }

        BeanFactory beanFactory = context.getBeanFactory();

        List<String> classNameCandidates = new ArrayList<>();
        addAll(classNameCandidates, attributes.get("value"));
        try {
            for (String clsName : classNameCandidates) {
                beanFactory.getBean(Class.forName(clsName));
            }
        } catch (Exception e) {
            log.debug("没有找到需要的 Bean: {}", e.getMessage());
            return false;
        }

        List<String> beanNameCandidates = new ArrayList<>();
        addAll(beanNameCandidates, attributes.get("name"));
        for (String beanName : beanNameCandidates) {
            if (!beanFactory.containsBean(beanName)) {
                log.debug("没有找到需要的 bean: {}", beanName);
                return false;
            }
        }

        return true;
    }

    private boolean matchMissingBean(ConditionContext context, MultiValueMap<String, Object> attributes, AnnotatedTypeMetadata metadata) {
        // ... 和 matchBean 相反,判定是否不存在某个 Bean,省略
    }
}

总结

​ 本文主要讲解了如何通过@ConditionalCondition实现环境检测的能力,并从源码及示例两方面演示了从简单到高级的用法支持。其它的诸如判定当时配置项中的值以及资源判定的实现原理都差不多,感兴趣的可以翻阅应用分发 base-boot-starter 的源码。

当然,这里高级应用里面的判定规则并不如SpringBoot中的功能强大,但应付常规的应用已经足够,当不满足需求时,通过本文的讲解读者应该也已经了解到了如何自行扩展,或者联系我协助。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容

  • 本来是准备看一看Spring源码的。然后在知乎上看到来一个帖子,说有一群**自己连Spring官方文档都没有完全读...
    此鱼不得水阅读 6,934评论 4 21
  • 第三章 高级装配 标签(空格分隔): 未分类 [TOC] 环境与profile 配置profile bean 在开...
    施瓦阅读 390评论 0 0
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,670评论 0 3
  • 为什么到简书来 朋友圈,微博,空间…… 在越来越多的网络社交里变得越来越浮躁 缺少一个安安静静的地方认真生活 第一...
    某木a阅读 278评论 0 1
  • 夜晚从窗往外望,是一扇一扇的人生,大家那么不一样,甚至是家里光的颜色和亮度都是不一样的。所以人如果像遇到跟自己完全...
    二狗站住阅读 230评论 0 0