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


相关文章
|
2月前
|
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. 报错解决
291 0
|
12月前
|
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
136 0
|
2月前
|
Java 数据库 Spring
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
36 0
|
9月前
|
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
57 0
|
11月前
|
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.
167 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
407 0
|
XML Java 数据库连接
【Error】:BeanCreationException: Error creating bean(Could not resolve resource location)
【Error】:BeanCreationException: Error creating bean(Could not resolve resource location)
310 0
【Error】:BeanCreationException: Error creating bean(Could not resolve resource location)
|
Java 数据库连接 网络安全
Error creating bean with name ‘attrAttrgroupRelationController‘
Error creating bean with name ‘attrAttrgroupRelationController‘
Error creating bean with name ‘attrAttrgroupRelationController‘
|
JavaScript 算法 前端开发
Property xxx was accessed during render but is not defined on instance
目前el-form的model主要用表单验证的,也就是配合el-form的rules和el-form-item的prop来使用的。不信的话,你可以增加一个rules和prop(为了调用验证方法,也el-form也加一个ref属性,相当于id或者class选择器的意思),但是不写model,然后验证的话,会提示缺少model,导致无法验证成功。
Property xxx was accessed during render but is not defined on instance