欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 前端技术 > javascript >内容正文

javascript

《springboot系列十六》条件装配时configurationcondition和condition有什么区别?什么时候用configurationcondition? -凯发ag旗舰厅登录网址下载

发布时间:2024/1/18 javascript 24 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 《springboot系列十六》条件装配时configurationcondition和condition有什么区别?什么时候用configurationcondition? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

针对条件装配我们讨论了如下内容:

  • 《springboot系列十一》:精讲如何使用@conditional系列注解做条件装配
  • 《springboot系列十二》:如何自定义条件装配(由@conditionalonclass推导)
  • 《springboot启动流程六》:springboot自动装配时做条件装配的原理(万字图文源码分析)(含@conditionalonclass原理)
  • 《springboot系列十三》:图文精讲@conditional条件装配实现原理
  • 《springboot系列十四》:@conditionalonbean、@conditionalonmissingbean注解居然失效了!
  • 在《springboot系列十三》:图文精讲@conditional条件装配实现原理一文中,我们知道了条件装配时是分两阶段(配置类解析、bean注册)进行的。

    configurationcondition接口是spring4.0提供的注解,位于org.springframework.context.annotation包内,继承自condition接口;

    public interface configurationcondition extends condition {/*** 返回当前condition可以被评估的配置阶段* return the {@link configurationphase} in which the condition should be evaluated.*/configurationphase getconfigurationphase();/*** condition应该被评估的各个配置阶段* the various configuration phases where the condition could be evaluated.*/enum configurationphase {/*** 配置类解析阶段* the {@link condition} should be evaluated as a {@code @configuration}* class is being parsed.*

    if the condition does not match at this point, the {@code @configuration}* class will not be added.*/parse_configuration,/*** bean注册阶段* the {@link condition} should be evaluated when adding a regular* (non {@code @configuration}) bean. the condition will not prevent* {@code @configuration} classes from being added.*

    at the time that the condition is evaluated, all {@code @configuration}* classes will have been parsed.*/register_bean}}

    configurationcondition中的getconfigurationphase()方法,用于返回configurationphase配置阶段(configurationphase 的枚举);

  • parse_configuration:表示在配置类解析阶段进行condition的评估;
  • register_bean:表示在注册bean阶段进行condition的评估;
  • 1、configurationcondition和condition的区别?

    1> condition评估的时机:

  • configurationcondition中提供配置阶段的概念,其包含两个阶段:parse_configuration 和 register_bean,使用configurationcondition接口实现类做condition条件装配时,会判断传入的配置阶段和configurationcondition#getconfigurationphase()返回的配置阶段是否一致,如果不一致则不进行condition评估;以此实现更细粒度的条件装配控制。
  • condition中没有配置阶段的概念,在任何时候都可以使用其进行condition评估;
  • 2> 使用对比:

  • onbeancondition实现configurationcondition接口,getconfigurationphase()返回configurationphase.register_bean,表示只在注册bean阶段进行condition评估,其他阶段conditionevaluator#shouldskip()方法均直接返回false。
  • onclasscondition实现condition接口,配置类加载的任何阶段都可以进行条件评估。
  • 1)onbeancondition

    1> onbeancondition类图:

    2> onbeancondition实现的方法:

    2)onclasscondition

    1> onbeancondition类图:

    2、什么时候用configurationcondition?

    configurationphase的作用是控制条件评估(过滤)的时机:是在解析配置类的时候 还是在创建bean的时候。

    一般而言configurationcondition多用于 只在注册bean阶段才进行条件评估的condition中使用,比如onbeancondition。以onbeancondition为例,其中的很多条件评估的依据只有在注册bean阶段才会相对更加完整。

    总结

    以上是凯发ag旗舰厅登录网址下载为你收集整理的《springboot系列十六》条件装配时configurationcondition和condition有什么区别?什么时候用configurationcondition?的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

    • 上一篇:
    • 下一篇:
    网站地图