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读取配置文件的内容的更多相关文章

  1. SpringBoot读取配置文件源码探究

    1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...

  2. springboot读取配置文件中的信息

    在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...

  3. SpringBoot 读取配置文件的值 赋给静态变量

    需求:写了一个工具类,但是工具类中的一些变量需要放到配置文件中,而这个工具类中的变量与方法都是静态的,这个时候我需要一个办法将配置文件中的相关配置读取过来赋值给这些静态变量.找了一些文章,试了一些方法 ...

  4. Springboot读取配置文件的两种方法

    第一种: application.yml配置中的参数: zip: Hello Springboot 方法读取: @RestController public class ControllerTest ...

  5. springboot读取配置文件的顺序

    前言 今天测试一些东西,发现配置文件连接的数据库一直不正常,数据也不对,今天请教了之后,原来springboot的配置文件加载不仅仅是项目内的配置文件. 正文 项目目录是这样的:文件夹下有:项目,ap ...

  6. springboot 读取配置文件

    读取配置文件 在以前的项目中我们主要在 XML 文件中进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息. 在 Spring Boot 中我们不再需要使用这种方式 ...

  7. SpringBoot读取配置文件三步走

    1首先新建application.properties文件 cn.qdl.demo.url=http://localhost:8080 2写一个类包上面的配置文件,类名随便取 public class ...

  8. SpringBoot 读取配置文件及profiles切换配置文件

    读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 先创 ...

  9. 【springboot读取配置文件】@ConfigurationProperties、@PropertySource和@Value

    概念: @ConfigurationProperties : 是springboot的注解,用于把主配置文件中配置属性设置到对于的Bean属性上 @PropertySource :是spring的注解 ...

随机推荐

  1. CF 1405E Fixed Point Removal【线段树上二分】

    CF 1405E Fixed Point Removal[线段树上二分]  题意: 给定长度为\(n\)的序列\(A\),每次操作可以把\(A_i = i\)(即值等于其下标)的数删掉,然后剩下的数组 ...

  2. hdu 4738 Caocao's Bridges(割边)

    题目链接 用tarjan求桥上的最小权值 #include<bits/stdc++.h> #define ll long long int using namespace std; inl ...

  3. poj2778 DNA Sequence(AC自动机+矩阵快速幂)

    Description It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's ve ...

  4. codeforces 128B. String

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. HDU 4289 Control(最大流+拆点,最小割点)

    题意: 有一群恐怖分子要从起点st到en城市集合,你要在路程中的城市阻止他们,使得他们全部都被抓到(当然st城市,en城市也可以抓捕).在每一个城市抓捕都有一个花费,你要找到花费最少是多少. 题解: ...

  6. Balanced Numbers SPOJ - BALNUM

    代码+注释 1 //我感觉这道题最扯的就是我因为输入而TLE了半天,懵逼死了,想破脑袋也没想到因为输入TLE了半天 2 //题意:求区间内数字满足"奇数各数出现偶数次,偶数各数出现奇数次&q ...

  7. 334A Candy Bags

    A. Candy Bags time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. Bootstrap 中的 aria-label 和 aria-labelledby

    正常情况下,form表单的input组件都有对应的label.当input组件获取到焦点时,屏幕阅读器会读出相应的label里的文本. <form> <div class=" ...

  9. 牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解

    题意: 标记为\(1-n\)的竹子,\(q\)个询问,每次给出\(l,r,x,y\).要求为砍区间\(l,r\)的柱子,要求砍\(y\)次把所有竹子砍完,每次砍的时候选一个高度,把比他高的都砍下来,并 ...

  10. volatile的内存屏障的坑

    请看下面的代码并尝试猜测输出: 可能一看下面的代码你可能会放弃继续看了,但如果你想要彻底弄明白volatile,你需要耐心,下面的代码很简单! 在下面的代码中,我们定义了4个字段x,y,a和b,它们被 ...