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. EIGRP和OSPF__EIGRP

    EIGRP解释 1.Enhanced Interior Gateway Routing Protocol 即增强内部网关路由协议.EIGRP同内部网关路由选择协议(IGRP)一样,是Cisco公司的私 ...

  2. linux 一分钟搭建zookeeper linux 单机版(亲测可用)

    wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gzt ...

  3. docker(4)解决pull镜像速度缓慢

    前言 上一篇讲到pull 镜像,但是pull镜像的时候下拉的速度实在感人,有什么解决办法吗?我们只需将docker镜像源修改为国内的 将docker镜像源修改为国内的: 在 /etc/docker/d ...

  4. Pytest(11)allure报告

    前言 allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. mac环境: ...

  5. windows下使用HyperV安装Centos7虚拟机

    以前都是用的VM(VMWare)安装虚拟机, 然鹅, 现在电脑装了Docker需要开启Windows的HyperV, 而我使用的VM版本(14)和HyperV 是不兼容的, 于是搜索引擎搜索了一下解决 ...

  6. Eclipse配置MySQL连接工具

    1.项目名称右键新建文件夹lib 2.用鼠标将mysql-connector-java-5.1.15-bin.jar移动到lib文件夹中 3.选择Copy files点击OK 4.右键移动过来的mys ...

  7. 2019牛客暑期多校训练营(第十场)F.Popping Balloons(线段树)

    题意:现在给你n个点 现在让你横着划三条线间距为r 然后竖着划三条线间距同样为r 现在让你求经过最多的点数 思路:我们首先建一棵关于y区间的线段树 然后枚举x轴 每次更新重叠的点 然后再更新回去 找一 ...

  8. 【2020杭电多校】Distinct Sub-palindromes 找规律

    题目链接:Distinct Sub-palindromes 题意: 给你一个长度n,你需要找出来一些串,这些串由A...Z和a...z构成.我们设长度为n的所有串中所包含回文子串最少的数量为ans.问 ...

  9. 避坑!js正确地使用fill()初始化二维数组

    先介绍一下坑 fill()方法都知道,填充数组 比如: let a = new Array(5).fill(0); console.log(a); // 输出结果为[0, 0, 0, 0, 0] 当我 ...

  10. iOS网页调试

    iOS上安装Chrome 打开Chrome://inspect,选择开始收集日志 新选项卡中访问目标站点 切换回日志收集页面,即可看到日志信息 https://blog.chromium.org/20 ...