SpringBoot读取配置文件的内容
1.@Value读取
在springboot项目中,如果要读取配置文件application.properties或application.yml文件的内容,可以使用自带的注解@Value。以properties方式为例说明,yml方式同上:
1.1单层内容
application.properties配置文件内容:
name=huihui
age=22
sex=1
读取方式:
//导入的类org.springframework.beans.factory.annotation.Value;
@Value("${name}")
private String name; @Value("${age}")
private Integer age; @Value("${sex}")
private String sex;
1.2多层内容
application.properties配置文件内容:
user.name=huihui
user.age=22
user.sex=1
读取方式:
//导入的类org.springframework.beans.factory.annotation.Value;
@Value("${user.name}")
private String name; @Value("${user.age}")
private Integer age; @Value("${user.sex}")
private String sex;
对于多层内容,读取时必须指定到最后一级。如上例必须到path,否则会报错。实际上单层和多层没有区别,只要指定到最后一级即可。
1.3设置默认值
对于有些配置,如果没有在配置文件中配置,那么在启动时就会报错,它也可以设置默认值。
以上面的多层配置为例,并没有配置user.addr的值,在注入时指定默认值:
@Value("${user.name}")
private String name;
@Value("${user.age}")
private Integer age;
@Value("${user.sex}")
private String sex;
@Value("${user.addr:湖北武汉}")
private String addr;
由此可见,要指定默认值,只需要在注入的时候在参数后面添加“:”及默认值即可。
2.@Value+@PropertySource读取
上述只是读取application的配置文件,那么如何读取自定义的配置文件呢?那就需要使用另外一个注解@PropertySource,指定配置文件的路径和名称。
2.1读取properties文件内容
在resources目录下新建testAAA.properties文件,内容如下:
user.name=huihui
user.age=22
user.sex=1
使用组件的方式读取:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@PropertySource("classpath:testAAA.properties")
public class TestService { @Value("${user.name}")
private String name; @Value("${user.age}")
private Integer age; @Value("${user.sex}")
private String sex; public void test() {
System.out.println(name);
System.out.println(age);
System.out.println(sex);
}
}
2.1读取yml文件内容
对于yml的内容,如果还是采用上面的方式,是无法读取到内容的,还会报错。需要在上述基础上指定自定义的工厂类。
1)新建一个类,继承DefaultPropertySourceFactory类重写其方法
package com.zys.springboottestexample.config; import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource; import java.io.IOException;
import java.util.List; public class PropertySourceFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
if (resource == null) {
return super.createPropertySource(name, resource);
}
List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
return sources.get(0);
}
}
2)在读取时指自定义的工厂类
@Component
@PropertySource(value="classpath:testAAA.yml", factory = PropertySourceFactory.class)
public class TestService { @Value("${user.name}")
private String name; @Value("${user.age}")
private Integer age; @Value("${user.sex}")
private String sex; public void test() {
System.out.println(name);
System.out.println(age);
System.out.println(sex);
}
}
yml的内容同上,可粘贴:
user:
name: huihui
age: 22
sex: 1
3.@Value+@ConfigurationProperties读取
3.1读取默认的application.properties文件
当对同一对象有多个属性时,每次在使用这些值时都重复的使用Value注解注入,有些麻烦,实际上也可以把这些属性注入到bean上,需要使用的地方直接使用此bean即可。读取application.yml文件可参考此方法,是一样的读取方式。
properties内容:
user.name=huihui
user.age=22
user.sex=1
读取内容:
@Component
@ConfigurationProperties(prefix="user")
@Data
public class TestService { String name;
String age;
String sex; public void test() {
System.out.println(name);
System.out.println(age);
System.out.println(sex);
}
}
需要注意的是,读取时,属性必须有set方法。
3.2读取自定义的properties文件
要读取自定义的文件,根据第二章的思想,如果可以指定文件的位置和名称就可以读取。但ConfigurationProperties注解并不能指定,这就需要PropertySource结合来指定自定义的配置文件。
@Component
@ConfigurationProperties(prefix="user")
@PropertySource("classpath:testAAA.properties")
@Data
public class TestService { private String name; private Integer age; private String sex; public void test() {
System.out.println(name);
System.out.println(age);
System.out.println(sex);
}
}
对于yml的内容,参考第二章自行。
SpringBoot读取配置文件的内容的更多相关文章
- SpringBoot读取配置文件源码探究
1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...
- springboot读取配置文件中的信息
在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...
- SpringBoot 读取配置文件的值 赋给静态变量
需求:写了一个工具类,但是工具类中的一些变量需要放到配置文件中,而这个工具类中的变量与方法都是静态的,这个时候我需要一个办法将配置文件中的相关配置读取过来赋值给这些静态变量.找了一些文章,试了一些方法 ...
- Springboot读取配置文件的两种方法
第一种: application.yml配置中的参数: zip: Hello Springboot 方法读取: @RestController public class ControllerTest ...
- springboot读取配置文件的顺序
前言 今天测试一些东西,发现配置文件连接的数据库一直不正常,数据也不对,今天请教了之后,原来springboot的配置文件加载不仅仅是项目内的配置文件. 正文 项目目录是这样的:文件夹下有:项目,ap ...
- springboot 读取配置文件
读取配置文件 在以前的项目中我们主要在 XML 文件中进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息. 在 Spring Boot 中我们不再需要使用这种方式 ...
- SpringBoot读取配置文件三步走
1首先新建application.properties文件 cn.qdl.demo.url=http://localhost:8080 2写一个类包上面的配置文件,类名随便取 public class ...
- SpringBoot 读取配置文件及profiles切换配置文件
读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 先创 ...
- 【springboot读取配置文件】@ConfigurationProperties、@PropertySource和@Value
概念: @ConfigurationProperties : 是springboot的注解,用于把主配置文件中配置属性设置到对于的Bean属性上 @PropertySource :是spring的注解 ...
随机推荐
- mysql错误(Incorrect key file for table)
Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to repair it mysql错误:mysql需要 ...
- Linux-yum安装和相关命令
Linux-yum安装和相关命令 一 yum yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包,能够从指定的服务 ...
- 一周精彩内容分享(第 1 期):"世纪逼空大战"
这里记录过去一周,我看到的值得分享的东西. 一方面是整理记录一下自己一周的学习,另一方面也是期待自己有更多的输出,有更多的价值. 周刊开源(Github:wmyskxz/weekly),欢迎提交 is ...
- mysql查询太慢,我们如何进行性能优化?
老刘是即将找工作的研究生,自学大数据开发,一路走来,感慨颇深,网上大数据的资料良莠不齐,于是想写一份详细的大数据开发指南.这份指南把大数据的[基础知识][框架分析][源码理解]都用自己的话描述出来,让 ...
- Codeforces Round #697 (Div. 3) G. Strange Beauty (DP,数学)
题意:给你一组数,问你最少删去多少数,使得剩下的数,每个数都能整除数组中其它某个数或被数组中其它某个数整除. 题解:我们直接枚举所有因子,\(dp[i]\)表示\(i\)在数组中所含的最大因子数(当我 ...
- python的@是什么(函数装饰器)
可以看菜鸟教程:https://www.runoob.com/w3cnote/python-func-decorators.html 还有一个实例:https://zhuanlan.zhihu.com ...
- python+selenium+bs4爬取百度文库内文字 && selenium 元素可以定位到,但是无法点击问题 && pycharm多行缩进、左移
先说一下可能用到的一些python知识 一.python中使用的是unicode编码, 而日常文本使用各类编码如:gbk utf-8 等等所以使用python进行文字读写操作时候经常会出现各种错误, ...
- A - Promotions
题目详见http://7xjob4.com1.z0.glb.clouddn.com/3f644de6844d64706eb36baa3a0c27b0 这题是普通的拓扑排序,要把每一层的都保存下来. # ...
- Codeforces Round #686 (Div. 3) E. Number of Simple Paths (思维,图,bfs)
题意:有一个\(n\)个点,\(n\)条边的图,问你长度至少为\(1\)的简单路径有多少条. 题解:根据树的性质,我们知道这颗树一定存在一个环,假如一棵树没有环,那么它的所有长度不小于\(1\)的简单 ...
- C# 静态类 单例模式 对比
公司的类都需要使用单例模式实现,这个可以节省资源,避免重复对象的生成.但是静态类也可以做到这一点,而且写起来更简洁,于是查阅相关资料,希望弄明白两者的差别. 1.单例模式可以在用到的时候初始化,而静态 ...