idea中springBoot启动报错Field personProperties in com.example.controller.HelloController required a bean

在读取application.yml 之后注入到HelloController 中,启动报如下错误

Field personProperties in com.example.controller.HelloController required a bean of type 'com.example.PersonProperties' that could not be found. 



有两种解决办法

第一种: 导致这个的原因是获取 .yml的属性类中没有加@Component



第二种:  当然,如果你在Application的启动类中加入了@EnableConfigurationProperties({PersonProperties.class})注解,那就不需要在PersonProperties这个类中加@Component这个注解了

 import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import ymlConfig.YmlConfig;

    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
    @EnableConfigurationProperties({PersonProperties.class})
//没错,@EnableConfigurationProperties注解里指出的PersonProperties.class就是读取yml配置文件的类。
//@ComponentScan("")
    public class ReadApplication extends SpringBootServletInitializer{

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(ReadApplication.class);
        }

        public static void main(String[] args) {
            SpringApplication.run(ReadApplication.class, args);
        }
    }
/*
application.yml中的配置
name: 小胖

        person:
        name: 小胖
        age: 22*/

//@Component
@ConfigurationProperties(prefix = "person")
public class PersonProperties {
    private String name;
    private int age;


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
springboot学习资料 一、 Spring介绍 1 1.1、SpringBoot简介 1 1.2、系统要求: 1 二、快速入门 1 2.1、创建一个Maven工程 1 2.2、pom文件引入依赖 2 2.3、编写HelloWorld服务 2 2.4、@RestController 3 2.5、@EnableAutoConfiguration 3 2.6 SpringApplication.run(HelloController.class, args); 3 @SpringbootApplication 3 2.7、SpringBoot启动方式1 4 2.8、SpringBoot启动方式2 4 三、 Web开发 5 3.1、静态资源访问 5 3.2、全局捕获异常 5 3.3、渲染Web页面 6 3.4、使用Freemarker模板引擎渲染web视图 6 3.4.1、pom文件引入: 6 3.4.2、后台代码 6 3.4.3、前台代码 7 3.4.4、Freemarker其他用法 7 3.4.5、Freemarker配置 8 3.5、使用JSP渲染Web视图 8 3.5.1、pom文件引入以下依赖 8 3.5.2、在application.properties创建以下配置 9 3.5.3、后台代码 9 四、 数据访问 9 4.1、springboot整合使用JdbcTemplate 9 4.2、springboot整合使用mybatis 9 4.3、springboot整合使用springjpa 9 4.4、springboot整合多数据源 9 五、 事物管理 9 5.1.1springboot整合事物管理 9 5.1.2SpringBoot分布式事物管理 9 六、 日志管理 9 6.1使用log4j记录日志 9 6.2使用AOP统一处理Web请求日志 9 七、 缓存支持 9 7.1注解配置与EhCache使用 9 使用Redis做集式缓存 9 八、 其他内容 9 8.1、使用@Scheduled创建定时任务 9 8.2、使用@Async实现异步调用 9 8.3、自定义参数 9 8.4、多环境配置 9 8.5、修改端口号 9 8.6、SpringBoot yml 使用 9 8.7、发布打包 9

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值