Not registered via @EnableConfigurationProperties or marked as Spring component
Spring not or as Component via registered marked
2023-09-11 14:22:20 时间
SpringBoot中,将类中的属性和配置文件中的配置进行绑定时出现以下的问题:
当使用@ConfigurationProperties时IDEA顶部出现这样的提示:
按照提示点击跳转到官方文档,接着在pom.xml中添加如下的配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
添加完后的效果是,当你写配置文件(yml,properties配置文件)时会有相关的提示
上面弄完后@ConfigurationProperties下面还有报错,按照提示可以看到
Not registered via @EnableConfigurationProperties or marked as Spring component,网上找到的博客有说要添加 @EnableConfigurationProperties(Person.class)【此时的Person是自定义的bean】,
添加后错误确实是没了,但是在SpringBoot的单元测试时会看到如下的错误:Could not autowire. No beans of 'Person' type found
回到自定义的bean Person中,添加注解@Component,声明将这个组件添加至容器中,这样才可以被使用?
“只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能,”
关于@Component和@EnableConfigurationProperties找到一个讨论,觉得应该有用,只是现在还不是很理解
https://www.imooc.com/qadetail/299025
确保添加了依赖 <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-configuration-processor </artifactId> <optional> true </optional> </dependency> 如果发现@ConfigurationPropertie不生效,有可能是项目的目录结构问题,可以通过@EnableConfigurationProperties(ConnectionSettings.class)来明确指定需要用哪个实体类来装载配置信息。 Spring boot 1.5以上去除了location属性,可采用@Component的方式注册为组件,然后使用@PropertySource来指定自定义的资源目录。
记录:通过@ConfigurationProperties获取配置文件的相关配置属性注入bean中
相关文章
- spring security 注解@EnableGlobalMethodSecurity详解
- 【Spring Boot】Spring Boot之两种引入spring boot maven依赖的方式
- Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错
- [Spring Boot] @Component, @AutoWired and @Primary
- Spring调用spymemcached客户端的例子
- Spring JdbcTemplate CRUD增删改查操作
- Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明
- 成功解决YOLOv3测试——could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZE作记录
- 成功解决YOLOv3测试——could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZE作记录
- Spring+SpringMVC+Mybatis(开发必备技能)02、完整DML&DQL语句demo示例
- 完整的 Spring Boot 下载文件示例代码
- spring boot maven package 失败(invalid entry size (expected 4 but got 3 bytes))
- 全网最完美地解决使用spring boot的@Value(“${xxx}“)注解时报出的错误:Could not resolve placeholder ‘xxx‘ in value “${xxx}“
- Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解
- RabbitMQ与Spring的框架整合之Spring Boot实战
- spring boot RESTFul API拦截 以及Filter和interceptor 、Aspect区别
- spring boot 启动错误:Could not resolve placeholder
- Spring Boot应用程序属性
- Spring JDBC调用存储过程
- Spring Boot面试题
- 启动Spring boot项目报错:java.lang.IllegalArgumentException: LoggerFactory is not a Logback