首页 > 其他分享 >记@ConditionalOnMissingBean注解导致bean注入失败的问题

记@ConditionalOnMissingBean注解导致bean注入失败的问题

时间:2023-07-06 19:11:46浏览次数:37  
标签:TaskExecutionAutoConfiguration 配置 nacos bean ConditionalOnMissingBean 注解 ThreadP

1.背景

springboot项目,引入nacos做配置中心,pom.yaml导入依赖

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.12</version>
        </dependency>

 

2.运行项目时报错如下:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field threadPoolTaskExecutor in com.paas.dmp.server.job.state.job.JobStateSynService required a bean of type 'org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)
	- @org.springframework.beans.factory.annotation.Qualifier(value=applicationTaskExecutor)

The following candidates were found but could not be injected:
	- Bean method 'applicationTaskExecutor' in 'TaskExecutionAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: java.util.concurrent.Executor; SearchStrategy: all) found beans of type 'java.util.concurrent.Executor' nacosConfigListenerExecutor


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor' in your configuration.


Process finished with exit code 1

 

3.原因分析

在springboot项目中注入TaskExecutionAutoConfiguration的配置如下:

    @Autowired
    @Qualifier(value = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

 

根据上述报错描述可知:TaskExecutionAutoConfiguration自动配置类没有成功注入名称为applicationTaskExecutor的bean,因为@ConditionalOnMissingBean注解找到了Executor.class的bean。

查看TaskExecutionAutoConfiguration这个自动配置类的源码,关键代码如下:

    @Lazy
    @Bean(
        name = {"applicationTaskExecutor", "taskExecutor"}
    )
    @ConditionalOnMissingBean({Executor.class})
    public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
        return builder.build();
    }

可以看到ThreadPoolTaskExecutor加上了@ConditionalOnMissingBean的注解,这个注解在这里的意思是:如果没有Executor的bean,就注入ThreadPoolTaskExecutor的bean。

当我们引入nacos的配置中心相关依赖时,会自动注入一个Executor的bean,名称为nacosConfigListenerExecutor,所以当我们在代码中再注入ThreadPoolTaskExecutor 的bean时就会有前面说到的报错。

 

4.问题解决

手动注入bean

 

    @Autowired
    @Qualifier(value = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

//通过@Bean注解手动注入ThreadPoolTaskExecutor的bean @Bean(name = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) public ThreadPoolTaskExecutor threadPoolTaskExecutor(){ return new ThreadPoolTaskExecutor(); }

 

 

5.引入nacos做配置中心其他注意事项

5.1.注意引入依赖的版本,nacos-config-spring-boot-starter版本太高可能会拉取不到nacos的配置;

5.2.nacos地址通过域名代理出来时,通过nacos的域名+api接口是可以拿到nacos的配置的,但是如果在springboot的配置文件里配置的nacos.server-addr的地址为域名时,是拿不到nacos上的配置的;

5.3.nacos配置中心配置的logging.config配置不生效,这可能与bootstrap.yml(bootstrap.properties)、application.yml(application.properties)的加载顺序有关,可以把logging.config的相关配置放到本地的配置文件中,不放到配置中心,这样也可以使相关配置生效。

 

参考:

https://blog.csdn.net/weixin_43916074/article/details/123951297

 

标签:TaskExecutionAutoConfiguration,配置,nacos,bean,ConditionalOnMissingBean,注解,ThreadP
From: https://www.cnblogs.com/wdgde/p/17533099.html

相关文章

  • No bean named 'transactionManager' available: No matching PlatformTransactionMan
    报错内容:找不到transactionManager原因:xml配置平台事务管理器的时候给了id。配置@Transaction注解时没有配置transactionManager 解决方案:将xml中配置的id="tranManager"改为id="transactionManager"。原因是因为@Transaction中transactionManager的默认名称是”transactionM......
  • IDEA中SpringBoot项目 注解报错
    WSG报错:应通过@SpringBootApplication指定特性 IDEA中@EnableAutoConfiguration注解报错Attributesshouldbespecifedvia@SpringBootApplication多次用Maven清理重新导包后还是不可以但是项目可以正常启动起来这种是IDEA级别的检查错误 需要在IDEA中Settings中设......
  • 通过aop 注解的方式防止表单重复提交
    pom.xml<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><a......
  • Spring中bean标签的所有属性以及作用
    在Spring中,bean标签用于定义和配置bean对象。以下是常用的bean标签属性及其作用:id:指定bean的唯一标识符。在整个Spring容器中,每个bean都必须有一个唯一的id。class:指定bean的类名。通过该属性,Spring将实例化并管理指定类的对象作为bean。name:用于指定bean的名称。除了id属性......
  • Spring中getBean方法的使用方法
    在Spring中,可以使用getBean()方法从容器中获取一个已注册的bean实例。以下是getBean()方法的使用方法:使用ApplicationContext获取bean://创建Spring应用上下文ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml");//通过bean的名......
  • Springboot No bean named 'XXXXX' available 问题解决
    一、问题描述近日在工作中遇见了一个bug,后端程序频频报错Nobeannamed'XXXXX'available。对比同类程序文件,没有发现有任何特殊之处。在网上搜索方法基本上就是扫描包配置、注解问题、路径问题等,皆不能解决我的问题。排查问题是发现出现问题的类命名不符合驼峰规范,按照这个......
  • Spring配置文件中,bean标签下是各个子标签的作用解释
    bean标签的子标签propertyconstructor-argdescriptionlookup-methodmetaqualifierreplaced-method在Spring配置文件中,bean标签下是各个标签的作用解释:<property>:用于设置bean的属性值。它可以用于注入基本类型、引用类型或其他属性。通过指定属性名称和对应的值,可以......
  • 条件注解之@ConditionalOnProperty注解:通过配置文件的配置来控制配置类是否加入spring
    一、条件注解分类常见的@ConditionalOnxxx开头的注解我们称之为条件注解,常见的条件注解有class条件注解:@ConditionalOnClassbean条件注解:@ConditionalOnBean属性条件注解:@ConditionalOnProperty…@ConditionalOnProperty:如果有指定的配置,条件生效;@ConditionalOnBean:如果......
  • springboot封装redission的分布式锁逻辑为注解
    场景概述使用分布式锁的时候,每次都需要使用trycatch处理方法中的逻辑。考虑是否可以这块逻辑抽离出来。实现在自定义的注解中添加属性来设置锁的等待时间、租赁时间和时间单位importjava.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTI......
  • Spring容器获取Bean的9种方式
    1前言随着SpringBoot的普及,Spring的使用也越来越广,在某些场景下,我们无法通过注解或配置的形式直接获取到某个Bean。比如,在某一些工具类、设计模式实现中需要使用到Spring容器管理的Bean,此时就需要直接获取到对应的Bean。本文为大家整理汇总了常见的获取Bean的方式,并提供一些优......