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的加载原理


相关文章
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
|
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
115 0
|
7月前
|
Linux
报错 Package ‘oniguruma‘, required by ‘virtual:world‘, not found
报错 Package ‘oniguruma‘, required by ‘virtual:world‘, not found
417 0
|
7月前
|
Java
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
171 1
|
Java 数据库连接 mybatis
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
2120 0
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
解决requried a bean of type xxx的问题(可能原因之一)
解决requried a bean of type xxx的问题(可能原因之一)
424 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.
229 0
|
Java Spring 容器
No bean named ‘springSecurityFilterChain‘ is defined
No bean named ‘springSecurityFilterChain‘ is defined
184 0
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?