在springboot下读取自定义properties配置文件的方法

chaos1973-newworld      2022-05-07     598

关键词:

SpringBoot工程默认读取application.properties配置文件。如果需要自定义properties文件,如何读取呢?

一、在resource中新建.properties文件

在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下。如图remote.properties所示

技术图片

二、编写配置文件

1
2
remote.uploadFilesUrl=/resource/files/
remote.uploadPicUrl=/resource/pic/

三、新建一个配置类RemoteProperties.java

1
2
3
4
5
6
7
8
9
@Configuration
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false)
@PropertySource("classpath:config/remote.properties")
@Data
@Component
public class RemoteProperties {
  private String uploadFilesUrl;
  private String uploadPicUrl;
}

其中

@Configuration 表明这是一个配置类
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false) 该注解用于绑定属性。prefix用来选择属性的前缀,也就是在remote.properties文件中的“remote”,ignoreUnknownFields是用来告诉SpringBoot在有属性不能匹配到声明的域时抛出异常。
@PropertySource("classpath:config/remote.properties") 配置文件路径
@Data 这个是一个lombok注解,用于生成getter&setter方法,详情请查阅lombok相关资料
@Component 标识为Bean

四、如何使用?

在想要使用配置文件的方法所在类上表上注解EnableConfigurationProperties(RemoteProperties.class)
并自动注入

1
2
@Autowired
RemoteProperties remoteProperties;

在方法中使用 remoteProperties.getUploadFilesUrl()就可以拿到配置内容了。

1
2
3
4
5
6
7
8
9
10
11
@EnableConfigurationProperties(RemoteProperties.class)
@RestController
public class TestService{
  @Autowired
  RemoteProperties remoteProperties;
 
  public void test(){
    String str = remoteProperties.getUploadFilesUrl();
    System.out.println(str);
  }
}

这里str就是配置文件中的”/resource/files/”了。

PS:下面看下 Spring-boot中读取config配置文件的两种方式

了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件的信息。

Spring-Boot读取配置文件的方式:

一.读取核心配置文件信息application.properties的内容

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

核心配置文件application.properties内容如下:

1
test.msg=Hello World SpringBoot

方式一:使用@Value方式(常用)

1
2
3
4
5
6
7
8
9
10
11
12
13
package Solin.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
  @Value("${test.msg}")
  private String msg;
  @RequestMapping("/index1"
  public String index1(){
    return "方式一:"+msg;
  }
}

注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。
访问:http://localhost:8088/index1时得到:"方式一:Hello World SpringBoot"

方式二:使用Environment方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package Solin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
  @Autowired
  private Environment env;
    
  @RequestMapping("/index2"
  public String index2(){
    return "方式二:"+env.getProperty("test.msg");
  }
}

注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。
访问:http://localhost:8088/index2时得到:"方式二:Hello World SpringBoot"

二.读取自定义配置文件信息,例如:author.properties

为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件author.properties

resources/author.properties内容如下:

1
2
author.name=Solin
author.age=22

创建管理配置的实体类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package Solin.controller;
  
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
  
//加上注释@Component,可以直接在其他地方使用@Autowired来创建其实例对象
@Component
@ConfigurationProperties(prefix = "author",locations = "classpath:author.properties")  
public class MyWebConfig{
  private String name;
  private int age;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}

注意:

    在@ConfigurationProperties注释中有两个属性:

locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以author.开头)

    使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。

创建测试Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package Solin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller 
public class ConfigController {
  @Autowired
  private MyWebConfig conf;
    
  @RequestMapping("/test"
  public @ResponseBody String test() {
    return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge(); 
  }
}

注意:由于在Conf类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。

访问:http://localhost:8088/test时得到:"Name:Solin---Age:22"

springboot读取自定义properties文件

@Configuration@ComponentpublicclassPropertiesConfig{privatestaticfinalString[]properties={"/application.properties"};privatestaticPropertiesprops;privatestaticMap<Object,Object>propertiesMap=new 查看详情

springboot自定义配置文件数据源

参考技术ASpringBoot支持动态的读取文件,留下的扩展接口org.springframework.boot.env.EnvironmentPostProcessor。这个接口是spring包下的,使用这个进行配置文件的集中管理,而不需要每个项目都去配置配置文件。这种方法也是springboot框架留... 查看详情

自定义jar配置文件问题?

java开发的简单工具类jar包,没有spring等任何框架,在springboot中引入使用,怎样灵活读取springboot的配置文件?或者说在以下情况下怎样都能读取到spingboot的配置文件:1、application.properties;2、application.yml;3、springboot打成jar包启... 查看详情

尝试在spring boot中将env变量读取到不在application.property中的自定义属性文件

】尝试在springboot中将env变量读取到不在application.property中的自定义属性文件【英文标题】:tryingtoreadenvvariablestocustompropertiesfilenotinapplication.propertyinspringboot【发布时间】:2021-01-0510:45:27【问题描述】:我正在尝试从custom.properties... 查看详情

从自定义 gradle 插件中读取 Spring Properties 文件

...时间】:2021-08-2107:23:13【问题描述】:我们已经编写了在springboot项目中使用的自定义gradle插件,我们在buildscript期间将该插件作为类路径加载,然后将该插件应用到boot项目的build.gradle中。我的问题是在g 查看详情

springboot的自定义配置

参考技术ASpringBoot免除了项目中大部分的手动配置,对一些特定情况,我们可以通过修改全局配置文件以适应具体生产环境,可以说,几乎所有的配置都可以写在application.properties文件中,SpringBoot会自动加载全局配置文件,从而... 查看详情

springboot读取自定义配置文件

自定义配置文件my-config.properties1bfxy.title=bfxy2bfxy.name=hellospringboot!3bfxy.attr=12345  配置文件类appconfig.java1importorg.springframework.beans.factory.annotation.Autowired;2importorg.springfram 查看详情

springboot使用自定义的properties

springboot使用application.properties默认了很多配置。但需要自己添加一些配置的时候,我们应该怎么做呢。若继续在application.properties中添加如:wisely.name=zhangsanwisely.gender=malewisely.age=20定义配置类:@ConfigurationProperties(prefix="wisely")publi 查看详情

springboot实现自定义国际化

...建目录login.properties,login_zh_CN.properties,login_en_US.properties2.SpringBoot自动配置好了管理国际化资源文件的组件;配置前缀spring.messages.basename=i18n.login@Co 查看详情

springboot读取application.properties文件方法

首先在配置文件application.properties定义如下内容:aaa=123然后在控制器中编写:(只能在控制器中才能自动注入) 定义@Autowired即可自动注入 Enviroment,然后用env.getProperty("参数名")即可获取任意的配置参数。  查看详情

springboot加载自定义properties原理

参考技术Aspringboot自定义属性文件通过value注解引入,和@autowired不同的是,它是由ConfigurationClassPostProcessor这个BeanDefinitionRegistryPostProcessor来处理,属性文件的读取和注入是在BeanDefinition级别,对象实例化之前。我们建一个简单的... 查看详情

springboot的读取properties文件的方式

转载:https://www.imooc.com/article/18252一、@ConfigurationProperties方式自定义配置类:PropertiesConfig.javapackagecom.zyd.property.config;importjava.io.UnsupportedEncodingException;importjava.util.ArrayList;import 查看详情

springboot不同环境读取不同配置

具体做法:不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中;prod环境下的配置配置在application-prod.properties中。在application.properties中指定使用哪一个文件1、application-dev.properties(dev环境下... 查看详情

springboot如何读取配置文件中的属性

Springboot自定义属性注入SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties或者application.yml(application.yaml)进行配置。SpringBoot默认会从resources目录下加载a... 查看详情

曹工说springboot源码--怎么从properties文件读取bean

写在前面的话相关背景及资源:曹工说SpringBoot源码(1)--BeanDefinition到底是什么,附spring思维导图分享曹工说SpringBoot源码(2)--BeanDefinition到底是什么,咱们对着接口,逐个方法讲解曹工说SpringBoot源码(3)--手动注册BeanDefinition... 查看详情

springboot配置的优先级

...ExternalizedConfiguration为了能让应用在不同的环境下运行,SpringBoot允许自定义配置文件,如properties文件、yaml文件、系统环境变量参数、命令行参数。配置文件的覆盖优先级如下DeveloperTools提供了一些开发帮助工具,在build.gradle添加... 查看详情

[springboot2019-10-23]自定义springboot自动配置

1.创建springboot3个关键类1.1属性读取类:用于从配置文件中读取配置信息packagecom.shijt.springbootdemo.jdbc;importorg.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix="mysql.jdbc")pu 查看详情

springboot2系列教程|yaml配置文件详解(代码片段)

自定义属性加载首先构建SpringBoot项目,不会的看这篇旧文使用IDEA构建SpringBoot工程。首先在项目根目录src>>resource>>?application.properties文件下加入以下自定义属性:#防止读取乱码spring.http.encoding.charset=UTF-8#项目启动端口serv... 查看详情