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. JavaScript中是如何定义私有变量的

    前言 JavaScript并不像别的语言,能使用关键字来声明私有变量. 我了解的JavaScript能用来声明私有变量的方式有两种,一种是使用闭包,一种是使用WeakMap. 闭包 闭包的描述有很多种 ...

  2. 关于数位DP的学习

    ---恢复内容开始--- 因为最近做比赛经常会出现数位DP,便尝试着去学学看数位DP. 先给出两篇论文的链接: <数位计数问题解法研究> <浅谈数位类统计问题> 然后也是寻找了 ...

  3. 神奇C语言的字串处理库函数

    头文件:#incldue<string.h> 定义:strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地 ...

  4. Codeforces Round #696 (Div. 2) C. Array Destruction (贪心,multiset)

    题意:有\(n\)个数,首先任选一个正整数\(x\),然后在数组中找到两个和为\(x\)的数,然后去掉这两个数,\(x\)更新为两个数中较大的那个.问你最后时候能把所有数都去掉,如果能,输出最初的\( ...

  5. Is It A Tree? POJ - 1308

    题意: 题目给你一组单向边,当遇到输入0 0就证明这是一组边,当遇到-1 -1就要停止程序.让你判断这是不是一棵树 题解: 题目很简单,但是程序要考虑的很多 1.因为是一颗树,所以肯定不能出现环,这个 ...

  6. Codeforces Round #667 (Div. 3) E. Two Platforms (双指针)

    题意:有\(n\)个点往下落,你可以在最下面放两个长度为\(k\)的板子,问做多能接到多少个点. 题解:这题给纵坐标\(y\)完全没有用,我们先对横坐标\(x\)排序,然后从左边开始枚举,用\(l[i ...

  7. Detect the Virus ZOJ - 3430 AC自动机

    One day, Nobita found that his computer is extremely slow. After several hours' work, he finally fou ...

  8. 牛客编程巅峰赛S1第5场 - 黄金&钻石&王者 B.牛牛的字符串 (DP)

    题意:有一个字符串\(s\),我们可以选择\(s_{i}\),如果\(s_{i+k}>s_{i}\),那么就可以交换\(s_{i}\)和\(s_{i+k}\),问最多能够交换多少次. 题解:因为 ...

  9. OpenStack Train版-4.安装placement放置服务

    安装placement放置服务 创建placement数据库 mysql -uroot CREATE DATABASE placement; GRANT ALL PRIVILEGES ON place ...

  10. python之字符串replace的方法

    1.描述 replace()方法把字符串中的old(旧字符串)替换成new(新字符串),如果有指定第三个参数max,则替换的不超过max次 2.语法 str.replace(old,new[,max] ...