spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了

引入pom依赖

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency> author.name=zhangsan
author.age=20

再在配置类开头加上@PropertySource("classpath:your.properties"),其余用法与加载yml的配置一样。

@Component
@PropertySource(value = {"classpath:static/config/authorSetting.properties"},
ignoreResourceNotFound = false, encoding = "UTF-8", name = "authorSetting.properties")
public class AuthorTest { @Value("${author.name}")
private String name;
@Value("${author.age}")
private int age;
}

@PropertySource 中的属性解释 
1.value:指明加载配置文件的路径。 
2.ignoreResourceNotFound:指定的配置文件不存在是否报错,默认是false。当设置为 true 时,若该文件不存在,程序不会报错。实际项目开发中,最好设置 ignoreResourceNotFound 为 false。 
3.encoding:指定读取属性文件所使用的编码,我们通常使用的是UTF-8。

当我们使用 @Value 需要注入的值较多时,代码就会显得冗余,于是 @ConfigurationProperties 登场了

@Component
@ConfigurationProperties(prefix = "author")
@PropertySource(value = {"classpath:static/config/authorSetting.properties"},
ignoreResourceNotFound = false, encoding = "UTF-8", name = "authorSetting.properties")
public class AuthorTest { private String name;
private int age; }
@RestController
@EnableConfigurationProperties
public class DemoController { @Autowired
AuthorTest authorTest; @RequestMapping("/")
public String index(){
return "author's name is " + authorTest.getName() + ",ahtuor's age is " + authorTest.getAge();
}
}

使用 @EnableConfigurationProperties 开启 @ConfigurationProperties 注解。

————————————————
版权声明:本文为CSDN博主「卡梅丽多」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yuhan_0590/article/details/85100246

spring-boot-configuration-processor 是干啥用的的更多相关文章

  1. 【Spring Boot】Spring Boot之使用 Spring Boot Configuration Processor 完成设置自定义项目属性自动补全

    一.引入Maven坐标 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  2. 解决spring boot1.5以上版本@ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not.."

    Springboot1.5以上版本,在使用 @ConfigurationProperties注解的时候会提示“Spring Boot Configuration Annotation Processo ...

  3. spring boot Configuration Annotation Proessor not found in classpath

    出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationPropertie ...

  4. Spring Boot Configuration Annotation Proessor not found in classpath解决办法

    From: https://www.cnblogs.com/whtgjy/p/9438317.html 出现spring boot Configuration Annotation Proessor ...

  5. 使用@ConfigurationProperties注解 提示 “Spring Boot Configuration Annotation Processor not found in classpath ”

    解决方案: 在 pom.xml 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  6. spring boot configuration annotation processor not found in classpath

    <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spri ...

  7. 解决Spring Boot Configuration Annotation Processor not found in classpath

    问题截图: 解决方式: 在pom.xml文件中添加这些依赖 <dependency> <groupId>org.springframework.boot</groupId ...

  8. 2、spring boot 配置文件

    配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoot自 ...

  9. Spring Boot 配置元数据指南

    1. 概览 在编写 Spring Boot 应用程序时,将配置属性映射到 Java bean 上是非常有用的.但是,记录这些属性的最好方法是什么呢? 在本教程中,我们将探讨 Spring Boot C ...

  10. 【串线篇】spring boot配置文件大全【上】

    一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: • application.properties • application.yml 配置文件的作用:修改SpringB ...

随机推荐

  1. Mysql——日期函数,时间操作(汇总)

    英文文档连接:https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html 中文文档连接:https://www.docs4 ...

  2. 什么是弹性公网IP?

    弹性公网IP(Elastic IP Address,简称EIP),是可以独立购买和持有的公网IP地址资源.目前,EIP可绑定到专有网络类型的ECS实例.专有网络类型的私网SLB实例.专有网络类型的辅助 ...

  3. Kubernetes---Pod状态示例

    ⒈Pod 中只有一个容器并且正在运行,容器成功退出 ·记录事件完成  ·如果restartPolicy为:  Always:重启容器:Pod phase仍为Running  OnFailure:Pod ...

  4. 题目15 链表中倒数第K个节点

    ///////////////////////////////////////////////////////////////////////////////////// // 5. 题目15 链表中 ...

  5. REST架构中的HTTP动词

    POST 增 DELETE 删 PUT 改 GET 查 GET(SELECT):从服务器取出资源(一项或多项). POST(CREATE):在服务器新建一个资源. PUT(UPDATE):在服务器更新 ...

  6. 【AC自动机】单词

    [题目链接] https://loj.ac/problem/10060 [题意] 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. ...

  7. 优化方法总结以及Adam存在的问题(SGD, Momentum, AdaDelta, Adam, AdamW,LazyAdam)

    优化方法总结以及Adam存在的问题(SGD, Momentum, AdaDelta, Adam, AdamW,LazyAdam) 2019年05月29日 01:07:50 糖葫芦君 阅读数 455更多 ...

  8. 调研task_struct结构体

    进程的描述PCB task_struct——PCB的一种,在linux中描述进程的结构体叫做task_struct. task_struct内容分类: 标识符:描述本进程的唯一标识符,用来区别其他进程 ...

  9. MongoDB的复合唯一索引

    一 创建 JavaScript Shell db.room.ensureIndex({'floor':1,'num':1}) Spring Data @Data // lombok @Document ...

  10. ScrumBasic开发记录

    ScrumBasic 是基于asp.net core 1.0的开源敏捷管理软件.目前第一版.目前只有很基础的东西.希望我能将这个项目演变下去. 地址:https://github.com/CAH-Fl ...