spring boot资源文件配置读取
一般业务配置,尽量新建自己的配置文件,来读取,而不是配置在application.properties或application-*.properties或yml/yaml配置中。
application.properties或application-*.properties中如果配置了中文内容,必须得转成Unicode码,否则读取乱码。
转Unicode码可以使用jdk自带工具,cmd切换到jdk下的bin目录,打开native2ascii.exe,然后在下面输入你的中文,回车显示Unicode码,复制到配置文件中来。
整个资源文件中文切换Unicode:cmd切换到jdk下的bin目录,输入native2ascii -encoding utf-8 源文件名.properties 目标文件名.properties
springboot默认properies配置文件内部源代码使用ISO-8859-1编码,即使更改这文件属性编码也是无效的,详细可以参考这篇博文:https://blog.csdn.net/formemorywithyou/article/details/96473169
但yml/yaml中却可以配置中文,正常读取,具体源码也可参考上述地址。
而自定义配置文件,可以指定编码获取,所以,建议业务配置新建自己的配置文件存储,读取。
另外,如果同时存在application yml和properties配置文件,properties中的配置会覆盖yml中相同配置。
分三种方式,读取资源配置
1,通过@value("${key}")获取配置值
在application.properties中,配置:demo.title=\u6d4b\u8bd5\u6d4b\u8bd5
这里因为是springboot默认配置,所以中文要转Unicode码,不然乱码,见上面文字说明。
然后,在test类中的对应属性上通过@value("${demo.title}")加载
注意,如果配置在自定义文件中,还需要指定资源文件位置,这里是直接配置在application.properties默认配置中的。
@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
@Value("${demo.title}")
private String title;
@Test
public void testProperties()throws Exception{
System.out.println(title);
}
}
2,通过key的前缀,统一获取配置值
继续读取【在application.properties中,配置:demo.title=\u6d4b\u8bd5\u6d4b\u8bd5】
新建个TestPropertis类,类头部通过注解@ConfigurationProperties(prefix="demo")设置properties key的前缀
类内部通过属性key后缀来获取值
@Component
@ConfigurationProperties(prefix="demo")
public class TestPropertis { private String title; //setter getter...
}
注意:使用注解@ConfigurationProperties需要引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
在读取的类中通过@Resource引入TestProperties
@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
@Resource
private TestPropertis testPropertis;
@Test
public void testProperties2()throws Exception{
System.out.println("TEST----------"+testPropertis.getTitle());
}
}
3、自定义配置文件,通过key前缀统一获取
在src/main/resources新建一个demo.properties,文件属性确保UTF-8
在文件中配置示例:
demo.name=小小灰
demo.sex=M
然后新建一个类:TestDemoPropertis,类头部加入3个注解
@Component //生成实例,方便springboot调用
@ConfigurationProperties(prefix="demo") //统一设置资源文件key的前缀 //类中的每个字段为key的对应后缀
@PropertySource(value={"classpath:demo.properties"},encoding="utf-8")
//指定配置资源文件位置,这里是string数组,可以多个
//encoding必须加上,如果要读取中文,不然乱码,因为springboot源码加载properties是以ISO-8859-1读取的
@Component
@ConfigurationProperties(prefix="demo")
@PropertySource(value={"classpath:demo.properties"},encoding="utf-8")
public class TestDemoPropertis { private String name;
private String sex;
//setter getter...
}
注意:使用@ConfigurationProperties要引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
最后,同上一种在类中通过Resource引入TestDemoPropertis
@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
@Resource
private TestDemoPropertis testDemoPropertis;
@Test
public void testProperties3()throws Exception{
System.out.println("TEST DEMO----------"+testDemoPropertis.getName()+"-----"+testDemoPropertis.getSex());
}
}
spring boot资源文件配置读取的更多相关文章
- spring 及 spring boot 资源文件配置
Spring配置文件引入xml文件: <import resource=" " />标签使用总结 https://www.cnblogs.com/javahr/p/83 ...
- Spring Boot属性文件配置文档(全部)
This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...
- spring boot 日志文件配置(logback-spring.xml)亲测可用!
问题描述:如何配置springboot项目,通过日志配置,使之输出自定义日志. 详细文章:https://blog.csdn.net/gebitan505/article/details/701421 ...
- SpringBoot - 资源文件配置读取
Examp1:读取核心配置文件信息application.properties的内容 方法一:使用@Value方式(常用) 1.application.properties中自定义参数 test.ms ...
- Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置
通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...
- Spring Boot 外部化配置(一)- Environment、ConfigFileApplicationListener
目录 前言 1.起源 2.外部化配置的资源类型 3.外部化配置的核心 3.1 Environment 3.1.1.ConfigFileApplicationListener 3.1.2.关联 Spri ...
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- Spring boot 的自动配置
Xml 配置文件 日志 Spring Boot对各种日志框架都做了支持,我们可以通过配置来修改默认的日志的配置: #设置日志级别 logging.level.org.springframework=D ...
随机推荐
- “今日头条杯”首届湖北省大学程序设计竞赛--F. Flower Road
题目链接:点这 github链接:(包含数据和代码,题解):点这 链接:https://www.nowcoder.com/acm/contest/104/E来源:牛客网 题目描述 (受限于评测机,此题 ...
- CSS:CSS 尺寸 (Dimension)
ylbtech-CSS:CSS 尺寸 (Dimension) 1.返回顶部 1. CSS 尺寸 (Dimension) CSS 尺寸 (Dimension) 属性允许你控制元素的高度和宽度.同样,它允 ...
- 2.3 Gulp
在前端工程化中最重要的就是流程管理,借用 gulp 可以很方便的基于流的方式定义流程任务,并将任务串联起来,本节中将详细介绍 gulp ,包括: gulp 介绍 gulp 是什么 gulp 能够解决哪 ...
- 使用Netfilter进行数据包分析
#include <linux/init.h>#include <linux/module.h>#include <linux/skbuff.h>#include ...
- POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)
题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...
- flex上下、左右居中
tip:1)flex是用于div布局用的,作用于一级子元素(父元素写样式,作用于子元素) 2)弹性盒模型 3)英文解释justify-content: 对齐内容(内容一般写在主轴上)align-ite ...
- 拾遗:Go 代码结构
#define @ $ 概述 Go 编码通常将所有代码放置在同一个工作区 一个工作区包含多个版本仓库,使用 Git 等工具控制 每个仓库包含一个或多个包 每个包由同一目录中的一份或多份源代码组成 包的 ...
- 【Java多线程系列五】列表类
一些列表类及其特性 类 线程安全 Iterator 特性 说明 Vector 是 fail-fast 内部方法用synchronized修饰,因此执行效率较低 1. 线程安全的列表类并不意味着调用它 ...
- 【Linux】- 守护进程的启动方法
转自:Linux 守护进程的启动方法 Linux中"守护进程"(daemon)就是一直在后台运行的进程(daemon). 本文介绍如何将一个 Web 应用,启动为守护进程. 一.问 ...
- 快速高效学习Java编程在线资源Top 20(转载)
想要加强你的编程能力吗?想要提升你的 Java 编程技巧和效率吗? 不用担心.本文将会提供快速高效学习 Java 编程的 50 多个网站资源: 开始探索吧: 1.MKyong:许多开发者在这里可以找到 ...