一、概述

Properties 文件是我们可以用来存储项目特定信息的常用方法。理想情况下,我们应该将其保留在 jar 包之外,以便能够根据需要对配置进行更改。

在这个教程中,我们将研究在 Spring Boot 应用程序 中从 jar 外部位置加载 Properties 文件的各种方法。

二、使用默认位置

按照惯例,Spring Boot 按以下优先顺序在四个预定位置查找外部化配置文件 --- application.propertiesapplication.yml :{#crayon-5c73a186c8530009937282}

  • 当前目录的 /config 子目录
  • 当前目录
  • 一个类路径 /config
  • 类路径根

因此,application.properties 中定义并放置在当前目录的 /config 子目录中的属性将被加载。 这也会在发生冲突时覆盖其他位置的属性。

三、使用命令行

如果上述约定对我们不起作用,我们可以直接在命令行中配置位置

java -jar app.jar --spring.config.location=file:///Users/home/config/jdbc.properties

我们还可以传递应用程序搜索文件的文件夹位置:

java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config

最后,另一种方法是通过 Maven 插件 运行 Spring Boot 应用程序。

在那里,我们可以使用 -D 参数:

mvn spring-boot:run -Dspring.config.location="file:///Users/home/jdbc.properties"

四、使用环境变量

现在假设我们不能更改启动命令。

很棒的是 Spring Boot 还会读取环境变量 SPRING_CONFIG_NAMESPRING_CONFIG_LOCATION

export SPRING_CONFIG_NAME=application,jdbc
export SPRING_CONFIG_LOCATION=file:///Users/home/config
java -jar app.jar

请注意,仍将加载默认文件。但是环境特定的属性文件优先以防发生属性冲突。

  1. 使用应用程序属性

如我们所见,我们必须在应用程序启动之前定义 spring.config.namespring.config.location 属性,因此在 application.properties 文件(或 YAML 对应文件)中使用它们将没有影响。

Spring Boot 在 2.4.0 版本中修改了属性的处理方式。

与此更改一起,团队引入了一个新属性,允许直接从应用程序属性导入其他配置文件:

spring.config.import=file:./additional.properties,optional:file:/Users/home/config/jdbc.properties
  1. 以编程方式

如果我们想要编程访问,我们可以注册一个 PropertySourcesPlaceholderConfigurer bean:

public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = 
      new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("/Users/home/conf.properties"));
    properties.setIgnoreResourceNotFound(false);
    return properties;
}

在这里,我们使用 PropertySourcesPlaceholderConfigurer 从自定义位置加载属性。

七、从 Fat Jar 中排除文件

Maven Boot 插件会自动将 src/main/resources 目录下的所有文件包含到 jar 包中。

如果我们不想让某个文件成为 jar 的一部分,我们可以使用一个简单的配置来排除它:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/conf.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

在这个例子中,我们过滤掉了 conf.properties 文件,使其不包含在生成的 jar 中。

八、小结

本文展示了 Spring Boot 框架本身如何为我们处理 externalized configuration

通常,我们只需要将属性值放在正确的文件和位置。但我们也可以使用 Spring 的 Java API 进行更多控制。

与往常一样,示例的完整源代码可在 GitHub 上 获得。

posted @ 2022-10-13 20:04 hunterzhang 阅读(2758) 评论(1) 推荐(0) 编辑
摘要: 1。概述 在这篇快速文章中,我们将重点介绍如何在 Spring Security 和 Spring MVC 中手动验证用户的身份。 2。Spring Security 简单地说,Spring Security 将每个经过身份验证的用户的主体信息保存在 ThreadLocal 中——表示为 Authe 阅读全文
posted @ 2022-10-09 09:24 hunterzhang 阅读(661) 评论(2) 推荐(0) 编辑
摘要: 一、概述 在本教程中,我们将学习如何从 JUnit 4 迁移到最新的 JUnit 5 版本,并介绍两个版本的库之间的差异。 有关使用 JUnit 5 的一般指南,请参阅我们的文章 此处。 二、JUnit 5 的优势 让我们从以前的版本 JUnit 4 开始,它有一些明显的限制: 单个 jar 库包含 阅读全文
posted @ 2022-10-08 20:17 hunterzhang 阅读(811) 评论(0) 推荐(0) 编辑
摘要: What?什么是 Mock Server Mock 是模拟的意思。在测试中,通常表述为:对测试过程中不容易构造或者不容易获取的物件,用一个虚拟的物件来进行模拟的一个过程。能够提供 Mock 功能的服务就叫作 Mock Server。 Mock Server 通过模拟真实的服务,提供对来自客户端请求的 阅读全文
posted @ 2022-10-07 21:40 hunterzhang 阅读(248) 评论(0) 推荐(0) 编辑
摘要: 1 概述 Spring Cloud Consul 项目为 Spring Boot 应用程序提供了与 Consul 的轻松集成。 Consul 是一个工具,它提供组件来解决微服务架构中一些最常见的挑战: 服务发现——自动注册和注销服务实例的网络位置 健康检查——检测服务实例何时启动并运行 分布式配置— 阅读全文
posted @ 2022-10-04 11:36 hunterzhang 阅读(665) 评论(0) 推荐(0) 编辑
摘要: 我们知道类似 Java 等半编译半解释型语言编译生成的都是类似中间态的字节码,所以在 Java 里面我们想要实现程序工作的动态扩展,可以通过 Java 的字节码编辑技术([[动态代理#ASM]]/[[动态代理#CGLIB]]),并结合 JVM 的 [[字节码动态加载#^bc6dd8]] 实现动态修改 阅读全文
posted @ 2022-10-03 19:12 hunterzhang 阅读(1150) 评论(0) 推荐(0) 编辑
摘要: #include #include using namespace std;int a[1000][1000];void printM(int m,int n){ for(int startX=0,startY=0,endX=m,endY=n;startX*2 =startY) ... 阅读全文
posted @ 2013-08-24 20:56 hunterzhang 阅读(181) 评论(0) 推荐(0) 编辑
摘要: #include #include #include using namespace std;int main(){ //定义超大二维数组方法一 int *p=new int[10000*10000]; p[2*1000+100]=1; cout<<p[2*1000+100]<<endl; //定义超大二维数组方法二 int **q=new int*[1000]; for(int i=0;i<1000;i++) q[i]=new int[1000]; q[1][1]=1; cout<<q[1][1]<<endl; //十... 阅读全文
posted @ 2013-08-24 13:35 hunterzhang 阅读(2163) 评论(0) 推荐(0) 编辑
摘要: #include #include #include #include using namespace std;int n=100,m=100000;double random(){ return (double)rand()/RAND_MAX;}int random(int m){ return (int)(random()*(m-1)+0.5);}int main(){ srand(time(NULL)); cout<<n<<' '<<m<<endl; for(int i=0;i<m;i++) { if(rand()%2 阅读全文
posted @ 2013-08-22 16:25 hunterzhang 阅读(474) 评论(0) 推荐(0) 编辑
摘要: 在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作。以下是C字符串和C++中string的区别:C字符串string对象(C++)所需的头文件名称或或需要头文件原因为了使用字符串函数为了使用string类声明方式char name[20];string name;初始化方式char name[20]="nihao";string name = "nihao";必须声明字符串长度么?是否使用一个null字符么?是否字符串赋值的实现方式strcpy(name,"John");name = &quo 阅读全文
posted @ 2013-07-08 13:09 hunterzhang 阅读(1245) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示