springboot笔记04——读取配置文件+使用slf4j日志
前言
springboot常用的配置文件有yml和properties两种,当然有必要的时候也可以用xml。我个人更加喜欢用yml,所以我在这里使用yml作为例子。yml或properties配置文件可以为我们完成很多工作,我们只需要从配置文件中读取出相关的属性值用起来就行了。
读取配置文件+idea使用Slf4j记录日志
1、idea安装lombok插件(slf4j需要)
file -> settings -> plugins
查找lombok安装,然后重启idea。
2、创建一个springboot项目,引入相关依赖
<!-- 使用@ConfigurationProperties-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2、修改application.yml
person:
name: Kobe Bryant
age: 18
salary: 1235478.22
car:
carName: 奔驰S3200
carBrand: 奔驰
hobbit:
- 篮球
- singing
- dancing
daughters:
k1: d1
k2: d2
birthDate: 1980/01/01
marriage: true
#时间戳统一转换
spring:
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
3、创建实体类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component //导入Spring IOC容器中管理
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
private String name;
private int age;
private double salary;
private Car car;
private List<String> hobbit;
private Map<String, Object> daughters;
private Date birthDate;
private Boolean marriage;
}
@ConfigurationProperties(prefix = "person")
将整个配置文件映射到对象中,转化为bean。
它可以和@PropertySource 标注一起使用,指定映射哪一个个配置文件。
如果我们没有指定属性文件的地址,Springboot 默认读取 application.properties/application.yml 中的属性文件名。
例如:
@PropertySource("classpath:xxx.yml")
@ConfigurationProperties(prefix = "person")
就会映射xxx.yml中以person为前缀的属性。
Car.java
import lombok.Data;
@Data
public class Car {
private String carName;
private String carBrand;
}
ps: @Data 是lombok的注解,使用该注解会在运行时自动为该实体类生成getter、setter、toString 方法。
3、创建控制类
@Slf4j
@RestController
@EnableConfigurationProperties(Person.class)
public class PropertiesController {
@Autowired
private Person person;
@RequestMapping(value="/getProperties")
public Person getProperties() {
log.info("/getProperties");
return person;
}
}
@EnableConfigurationProperties(Person.class)这句注解的作用是使ConfigurationProperties生效,把Person类注入到spring的IOC容器中。
关于EnableConfigurationProperties,在SpringBoot的注释中是这样说明的:为带有@ConfigurationProperties注解的Bean提供有效的支持。这个注解可以提供一种方便的方式来将带有@ConfigurationProperties注解的类注入为Spring容器的Bean。
4、测试
输出配置文件内容

INFO级别日志

springboot笔记04——读取配置文件+使用slf4j日志的更多相关文章
- SpringBoot学习笔记:读取配置文件
SpringBoot学习笔记:读取配置文件 配置文件 在以往的项目中,我们主要通过XML文件进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息.在SpringBo ...
- python学习笔记之读取配置文件【转自https://my.oschina.net/u/3041656/blog/793467】
[转自https://my.oschina.net/u/3041656/blog/793467] 最近在接触利用python来写测试框架,本人也是个刚接触python,所以是个小菜鸟,今天开始,一点点 ...
- springboot 控制台程序读取配置文件(原创)
首先新建一个springboot项目,此处省略. 1.新建一个application.properties person.name=kevin person.age=6 person.sex=male ...
- SpringBoot两种读取配置文件的方式
方式一 @Value("${custom.group}") private String customGroup; 方式二 @Autowired private Environme ...
- springboot 使用consul 读取配置文件(遇到的坑太多,没记录)
最终成功版. pom引入mavn依赖: <!--consul--> <dependency> <groupId>org.springframework.cloud& ...
- springboot学习笔记:3.配置文件使用概要
Spring Boot允许外化(externalize)你的配置,这样你能够在不同的环境下使用相同的代码. 你可以使用properties文件,YAML文件,环境变量和命令行参数来外化配置.使用@Va ...
- springboot工程读取配置文件application.yml的写法
现在流行springboot框架的项目,里面的默认配置文件为application.yml,我们怎样读取这个配置文件呢? 先贴上我得配置文件吧 目录结构 里面内容 1 写读取配置文件的工具类 @Con ...
- springboot工程读取配置文件application.yml的写法18045
现在流行springboot框架的项目,里面的默认配置文件为application.yml,我们怎样读取这个配置文件呢? 先贴上我得配置文件吧 目录结构 里面内容 1 写读取配置文件的工具类 @Con ...
- SpringBoot 整合 slf4j 日志打印
划水时间,记录一下用到的相关slf4j 日志打印,如何实现配置输出.本地保存log日志文件... 我使用的是SpringBoot框架,slf4j 类库已经包含到了 SpringBoot 框架中,所有, ...
随机推荐
- git如何查找某个包含指定字符串的commit hash值?
答: git shortlog --format='%H|%cn|%s' | grep '需要查找的字符串内容'
- android studio 把 ionic 打包时修改应用名称、修改应用图标、修改启动画面,升级打包
在项目中resources中替换成自己的图标和启动画面即可 在config.xml 修改包名 打正式包和升级打包同原生的类似,在Androidmanifest.xml修改版本号和版本名
- shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中
shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...
- kinova roslaunch kinova_bringup kinova_robot.launch kinova_robotType:=j2s7s300
luo@luo-ThinkPad-W530:~$ luo@luo-ThinkPad-W530:~$ luo@luo-ThinkPad-W530:~$ luo@luo-ThinkPad-W530:~$ ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- Spring MVC 保存并获取属性参数
在开发控制器的时候,有时也需要保存对应的数据到这些对象中去,或者从中获取数据.而Spring MVC给予了支持,它的主要注解有3个:@RequestAttribute.@SessionAttribut ...
- dockerfile里面的内容编写顺序,必须严格按照顺序编写,否则编译不成
完整文档请参照https://www.cnblogs.com/effortsing/p/10480972.html 这里只介绍dockerfile里面的步骤顺序 cat>/home/jenkin ...
- 01点睛Spring MVC 4.1-搭建环境
转发:https://www.iteye.com/blog/wiselyman-2213906 1.1 简单示例 通篇使用java config @Controller声明bean是一个控制器 @Re ...
- 03.linux入门命令
1.linux命令的格式 命令 [选项] [参数] eg: ls ls -l ls -l /home 注: a.选项与参数不一定存在 b.选项用 "-" 来指明 c.命令,选项,参 ...
- 网络I/O模型
事件驱动模型 与传统编程模式不同,事件驱动程序在启动之后,就在那等待,等待什么呢?等待被事件触发.传统编程下也有“等待”的时候,比如在代码块D中,你定义了一个input(),需要用户输入数据.但这与下 ...