V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
mgzu
V2EX  ›  Java

spring boot ConditionalOnMissingBean 问题

  •  
  •   mgzu · 2023-04-26 17:26:33 +08:00 · 1491 次点击
    这是一个创建于 575 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目引用了业务日志库,使用注解 @EnableLogRecord 来自动配置库相关的 Bean ,其中有个获取当前操作人的 Bean 有 @ConditionalOnMissingBean 注解,当自定义该 Bean 的时候,以下第一种情况符合预期,第二种会启动失败,想请教下各位其中的原因及解决办法

    库源码:com.mzt.logapi.starter.configuration.LogRecordProxyAutoConfiguration

        @Bean
        @ConditionalOnMissingBean(IOperatorGetService.class)
        @Role(BeanDefinition.ROLE_APPLICATION)
        public IOperatorGetService operatorGetService() {
            return new DefaultOperatorGetServiceImpl();
        }
    

    一. 在 spring boot 启动类添加 @EnableLogRecord ,@ConditionalOnMissingBean 有效

    @EnableLogRecord(tenant = "temp")
    @SpringBootApplication
    public class DemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(DemoApplication.class, args);
    	}
    
    }
    
    

    二. 在配置类添加 @EnableLogRecord ,@ConditionalOnMissingBean 无效,启动报错

    @EnableLogRecord(tenant = "temp")
    @Configuration(proxyBeanMethods = false)
    public class LogRecordConfiguration {
        public LogRecordConfiguration() {
        }
    
        /**
         * 自定义获取操作人
         */
        @Bean
        public IOperatorGetService operatorGetService() {
            return () -> {
                Operator operator = new Operator();
                operator.setOperatorId("test");
                return operator;
            };
        }
    
    }
    

    repo: https://github.com/TheLastSunset/spring-boot-ConditionalOnMissingBean-issues.git

    2 条回复    2023-04-26 22:51:19 +08:00
    panpanpan
        1
    panpanpan  
       2023-04-26 18:23:48 +08:00
    spring bean 初始化顺序的问题,如果 LogRecordConfiguration 比 LogRecordProxyAutoConfiguration 先初始化,@ConditionalOnMissingBean 就可以生效,反之 LogRecordProxyAutoConfiguration 先初始化的话,执行到 @ConditionalOnMissingBean(IOperatorGetService.class)的时候就会返回 true ,导致 DefaultOperatorGetServiceImpl 被注入,后面再到 LogRecordConfiguration 的时候就冲突了


    官网文档
    https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.html

    The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.
    mgzu
        2
    mgzu  
    OP
       2023-04-26 22:51:19 +08:00
    @panpanpan 是的,@ConditionalOnMissingBean 会检查容器中是否存在对应的 BeanDefinition ,存在则跳过,
    https://github.com/spring-projects/spring-framework/blob/5.3.x/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java#L194

    而 BeanDefinition 则于以下位置加载
    https://github.com/spring-projects/spring-framework/blob/5.3.x/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java#L343

    在情况一中,configClasses 顺序是:0. LogRecordConfiguration ,1. LogRecordProxyAutoConfiguration
    情况二中,configClasses 顺序是:0. LogRecordProxyAutoConfiguration ,1. LogRecordConfiguration
    因此会导致情况一启动成功,情况二启动失败
    而 configClasses 则跟 spring 扫描类有关,目前没找到比较好的控制扫描类顺序或 BeanDefinition 注册顺序的方法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1180 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:07 · PVG 07:07 · LAX 15:07 · JFK 18:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.