A component required a bean named xxx that could not be found

简介: A component required a bean named xxx that could not be found

复刻项目过程中,项目启动报错,出现了如图的这个问题


A component required a bean named xxx that could not be found.

这其实是一个很简单的问题,显而易见就是有Bean没有被到注入Spring容器嘛


因为之前写的项目大多都是单体应用,没接触过这种大型的分布式架构项目。这个未被注入的Bean就是API中的一部分,所以我们在写代码时候,虽然可以用引入依赖(在Pom加入jar)的方式,并像其他的bean一样使用@Resource把它当做bean使用。但是在启动类上一定要用@ComponentScan扫描这个包,这样它才会被Spring容器接管


还有一些其他的问题:


pom文件中配置,一定要确保自己项目的application.yml是可用的,如图



env下两个目录,第一个是测试yml文件,第二个是本地yml文件


在pom文件中通过以下代码可达到启用任意yml配置文件的效果


<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profileActive>dev</profileActive>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
<profiles>

总结:Spring并不是只会浅浅会用就完事的,有时间要抽空看一下Spring的加载原理


相关文章
|
6月前
|
JavaScript
Property “selectedItemIndex“ was accessed during render but is not defined on instance. 报错解决
Property “selectedItemIndex“ was accessed during render but is not defined on instance. 报错解决
619 0
|
Java 数据库连接 mybatis
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
203 0
Field userMapper in zero.file.videoProject.service.UserService required a bean of type ‘zero.file.vi
Field userMapper in zero.file.videoProject.service.UserService required a bean of type ‘zero.file.vi
|
5月前
|
Java 数据库
SpringBoot整合es7.x启动报错:Rejecting mapping update to [AAA] as the final mapping would have more than 1
SpringBoot整合es7.x启动报错:Rejecting mapping update to [AAA] as the final mapping would have more than 1
|
Java 数据库连接 Redis
Bean method ‘redisConnectionFactory‘ not loaded because @ConditionalOnClass did not find required c
Bean method ‘redisConnectionFactory‘ not loaded because @ConditionalOnClass did not find required c
99 0
Error: undefined method `launchd_service_path‘ for xxx
使用brew命令进行启动就报错。
240 0
|
Java Apache Spring
解决required a single bean, but 2 were found问题
背景:springboot整合shiro中自定义Realm时出现 错误描述 Parameter 0 of method getDefaultWebSecurityManager in cn.ken.springboot_shiro.config.ShiroConfig required a single bean, but 2 were foun
|
druid
A bean with that name has already been defined in class path resource and overriding is disabled.
A bean with that name has already been defined in class path resource and overriding is disabled.
215 0
|
Nacos
Nacos报错:Error creating bean with name ‘authFilterRegistration‘ defined in class path resource
Nacos报错:Error creating bean with name ‘authFilterRegistration‘ defined in class path resource
481 0