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 注解。

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. 埃氏筛+线段树——cf731F

    从2e5-1依次枚举每个数作为主显卡,然后分段求比它大的数的个数,这里的复杂度是调和级数ln2e5,即埃氏筛的复杂度.. #include<bits/stdc++.h> using nam ...

  2. LIBRARY_PATH是编译时候用的,LD_LIBRARY_PATH是程序运行是使用的

    LD_LIBRARY_PATH与LIBRARY_PATH的区别 看起来很像,但是完全是两码事. LIBRARY_PATH is used by gcc before compilation to se ...

  3. git 问题整理 fetch -p 修剪远程分支

    修剪远程分支 问题描述 前提:假设现在远程有三个分支master.testA.testB 问题:使用命令 git fetch origin 获取到远程分支,然后使用命令 git branch -r 来 ...

  4. B606 ChangeNet

    @echo off Setlocal Enabledelayedexpansion title B606 ChangeNet echo Checking... set inside=F&set ...

  5. on() 不支持hover事件

    因为 .hover() 是 jQuery 自己定义的事件… 是为了方便用户绑定调用 mouseenter 和 mouseleave 事件而已,它并非一个真正的事件,所以当然不能当做 .bind() 中 ...

  6. 3.1 开始使用 redux

    前面我们介绍了 flux 架构以及其开源实现 redux,在这一节中,我们将完整的介绍 redux: redux 介绍 redux 是什么 redux 概念 redux 三原则 redux Store ...

  7. Linux分页机制

    地址长度 在Linux下,unsigned long可以与地址的长度保持一致,即32位系统下unsigned long为32位,而64位系统下为64位长. 虚拟地址的分解 如图所示,通过XXX_SHI ...

  8. 2019杭电多校第三场hdu6606 Distribution of books(二分答案+dp+权值线段树)

    Distribution of books 题目传送门 解题思路 求最大值的最小值,可以想到用二分答案. 对于二分出的每个mid,要找到是否存在前缀可以份为小于等于mid的k份.先求出这n个数的前缀和 ...

  9. 第九篇 数据表设计和保存item到json文件

    上节说到Pipeline会拦截item,根据设置的优先级,item会依次经过这些Pipeline,所以可以通过Pipeline来保存文件到json.数据库等等. 下面是自定义json #存储item到 ...

  10. pytest-mark跳过

    import pytestimport sysenvironment='android' @pytest.mark.skipif(environment=="android",re ...