参考文章】:SpringBoot之@EnableConfigurationProperties分析

【参考文章】:在Spring Boot中使用 @ConfigurationProperties 注解, @EnableConfigurationProperties

1. pom

        <!-- 通过资源文件注入属性配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

2. main方法上添加注解

  @EnableConfigurationProperties(配置类1.class)

  @EnableConfigurationProperties({配置类1.class,配置类2.class})

  该注解是用来开启对@ConfigurationProperties注解配置Bean的支持,也就是@EnableConfigurationProperties注解告诉Spring Boot 能支持@ConfigurationProperties。

@SpringBootApplication
@EnableConfigurationProperties(SystemConfig.class)
public class BootApplication { public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BootApplication.class, args);
System.out.println("start ok");
}

3. 配置类

  配置参数在 application.yml 文件中,系统自动读取该文件,所以不必指定配置文件;

  @ConfigurationProperties (prefix = "system",ignoreInvalidFields = false ,ignoreUnknownFields = true) 

  prefix :表示参数 key 的前缀;

  ignoreInvalidFields :忽略无效的属性,默认为 false;

  ignoreUnknownFields :忽略未知的属性,默认为 true;

  如果需要指定配置文件,则需要使用添加 @PropertySource 注解(测试时读取不到配置文件,待解决);

  @PropertySource(value = "filepath",encoding = "utf8",ignoreResourceNotFound = false)

  value:文件路径;

  encoding:文件编码格式;

  ignoreResourceNotFound:忽略未找到的资源文件,默认为false;

@ConfigurationProperties(prefix = "system")
public class SystemConfig {
private Integer type; public SystemConfig() {
System.out.println("SystemConfig");
} public Integer getType() {
return type;
} public void setType(Integer type) {
this.type = type;
}
}

4. 配置文件

4.1 yml 文件

system:
type: 2

4.2 properties 文件

system.type=2

5. 常见问题

5.1 是否添加@Configuration 注解(原因暂不明确)

  初始化其他Bean时如果依赖配置类,则配置类不能添加@Configuration注解,否则spring会提示有两个bean,必须指定一个bean;

  实例化其他Bean时如果不依赖配置类,配置类添加@Configuration注解也可以正常运行;

Spring boot 配置文件参数映射到配置类属性的更多相关文章

  1. Spring boot运行原理-自定义自动配置类

    在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...

  2. Spring Boot 配置文件和命令行配置

    Spring Boot 属于约定大于配置,就是说 Spring Boot 推荐不做配置,很多都是默认配置,但如果想要配置系统,使得软件符合业务定义,Spring Boot 可以通过多种方式进行配置. ...

  3. 详解Spring Boot配置文件之多环境配置

    一. 多环境配置的好处: 1.不同环境配置可以配置不同的参数~ 2.便于部署,提高效率,减少出错~ 二. properties多环境配置 1. 配置激活选项 spring.profiles.activ ...

  4. spring mvc \ spring boot 允许跨域请求 配置类

    用@Component 注释下,随便放个地方就可以了 package com.chinaws.wsarchivesserver.core.config; import org.springframew ...

  5. Springboot 系列(二)Spring Boot 配置文件

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 不管是通过官方提供的方式获取 Spring ...

  6. 史上最全的Spring Boot配置文件详解

    Spring Boot在工作中是用到的越来越广泛了,简单方便,有了它,效率提高不知道多少倍.Spring Boot配置文件对Spring Boot来说就是入门和基础,经常会用到,所以写下做个总结以便日 ...

  7. Spring Boot 配置文件中的花样,看这一篇足矣!

    在快速入门一节中,我们轻松的实现了一个简单的RESTful API应用,体验了一下Spring Boot给我们带来的诸多优点,我们用非常少的代码量就成功的实现了一个Web应用,这是传统的Spring应 ...

  8. Spring Boot 配置文件中的花样

    原文:https://www.cnblogs.com/didispace/p/11002732.html 在快速入门一节中,我们轻松的实现了一个简单的RESTful API应用,体验了一下Spring ...

  9. springboot批量读取参数映射到实体类

    spring读取配置参数可以通过${name}的方式获取,如properties文件中存在如下配置 person.username=xi 则可通过${person.username}获取其对应的值xi ...

随机推荐

  1. SQLyog简介及其功能(附百度云盘下载地址)

    一.软件简介 SQLyog 是一个快速而简洁的图形化管理MYSQL数据库的工具,它能够在任何地点有效地管理你的数据库.SQLyog是业界著名的Webyog公司出品的一款简洁高效.功能强大的图形化MyS ...

  2. python的int方法实现数据类型转换

    int方法默认以十进制来实现数据类型的转换: 举例: str1=" #给定的内容最好是纯数字,当然也可以是数字再掺杂点别的,最好别掺杂,因为会报错 print(type(str1),str) ...

  3. windows 活动目录双向信任配置

    活动目录A:ess.com 192.168.1.20/24 活动目录B:ups.com  192.168.1.30/24 Step1:在活动目录B的域控制器上配置域的林双向信任关系,并且可传递,如下图 ...

  4. MQTT服务器本地搭建

    1.1 初认识MQTT协议. 2.1 下载压缩包 前往EMQ下载地址:http://emqtt.com/downloads ,下载您的系统的版本,一般选择稳定版. 2.2 解压并运行 C:\Users ...

  5. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  6. message [Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property

    springmvc前台字符串,后台Date类型字段.时间强转失败 数值:18年12月31日 15:43:21 解决方法,给时间字段加注释 @DateTimeFormat(pattern = " ...

  7. linux下sort命令使用详解---linux将文本文件内容加以排序命令

    转载自:http://www.cnblogs.com/hitwtx/archive/2011/12/03/2274592.html linux下sort命令使用详解---linux将文本文件内容加以排 ...

  8. 四、Vmware虚拟机三种网络模式详解

    转载自: http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note 1. ...

  9. 通过github安装crawley出现的问题

    http://www.cnblogs.com/hbwxcw/p/7086188.html

  10. python 实现 Fortran的读取10*0以及换行读问题

    思路,用read来全部读取,然后替换带*的元素来解决.代码如下 import numpy as np import re inf = open('SF.usr') title = inf.readli ...