网上搜集和整理如下(自己已验证过)

1. war包在tomcat中加载外部配置文件

war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次更新war包而不用更新配置文件的目的。
SpringBoot配置文件可以放置在多种路径下,不同路径下的配置优先级有所不同。
  可放置目录(优先级从高到低)

  • 1.file:./config/ (当前项目路径config目录下);
  • 2.file:./ (当前项目路径下);
  • 3.classpath:/config/ (类路径config目录下);
  • 4.classpath:/ (类路径config下).
  1. 优先级由高到底,高优先级的配置会覆盖低优先级的配置;
  2. SpringBoot会从这四个位置全部加载配置文件并互补配置;

想要满足不更新配置文件的做法一般会采用1 和 2,但是这些对于运行在独立tomcat下的war包并不比作用。
我这里采用的是SpringBoot的Profile配置。
在application.properties中增加如下配置:

spring.profiles.active=test1
  • 再在tomcat根目录下新建一个名为config的文件夹,新建一个名为application-test1.properties的配置文件。
  • 完成该步骤后,Profile配置已经完成了。
  • 然后还需要将刚刚新建的config文件夹添加到tomcat的classpath中。
  • 打开tomcat中catalina.properties文件,在common.loader处添加**${catalina.home}/config**,此处的config即之前新建的文件夹名称。
  • 如此,大功告成。程序启动之后,每次配置文件都会从config/application-test1.properties加载。

2.自定义配置文件

如果你不想使用application.properties作为配置文件,怎么办?完全没问题

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
或者
java -jar -Dspring.config.location=D:\config\config.properties springbootrestdemo-0.0.1-SNAPSHOT.jar 
当然,还能在代码里指定
@SpringBootApplication
@PropertySource(value={"file:config.properties"})
public class SpringbootrestdemoApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootrestdemoApplication.class, args);
}
}

3.(扩展)这里详细介绍下spring.profiles.active

默认配置 src/main/resources/application.properties

app.window.width=500
app.window.height=400

指定具体环境的配置

src/main/resources/application-dev.properties

app.window.height=300

src/main/resources/application-prod.properties

app.window.width=600
app.window.height=700
简单的应用
package com.logicbig.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; @Component
public class ClientBean {
@Value("${app.window.width}")
private int width;
@Value("${app.window.height}")
private int height; @PostConstruct
private void postConstruct() {
System.out.printf("width= %s, height= %s%n", width, height);
}
}
package com.logicbig.example; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ExampleMain { public static void main(String[] args) {
SpringApplication.run(ExampleMain.class, args);
}
}
运行
$ mvn spring-boot:run

width= 500, height= 400
$ mvn -Dspring.profiles.active=dev spring-boot:run width= 500, height= 300
$ mvn -Dspring.profiles.active=prod spring-boot:run width= 600, height= 700
 

springboot加载外部配置文件的更多相关文章

  1. 19、属性赋值-@PropertySource加载外部配置文件

    19.属性赋值-@PropertySource加载外部配置文件 加载外部配置文件的注解 19.1 [xml] 在原先的xml 中需要 导入context:property-placeholder 声明 ...

  2. SpringBoot加载子模块配置文件的方法

    这两天开始学习SpringBoot框架,按照官方的文档,很轻易地就把单模块的项目启动了,但在使用maven搭建多模块的时候遇到了子模块配置文件没有加载的问题 项目架构是这样的 zero |-ws |- ...

  3. 脚本命令加载外部配置文件夹conf

    加载log4j配置文件 Log4iConfigurer类 public class Log4iConfigurer { private static boolean INITIALIZED = fal ...

  4. SpringBoot系列——加载自定义配置文件

    前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...

  5. 【Spark】SparkStreaming-加载外部配置文件

    SparkStreaming-加载外部配置文件 spark加载配置文件_百度搜索 Spark加载外部配置文件 - CSDN博客 spark读取配置文件中的配置 - CSDN博客 spark加载prop ...

  6. Springboot 加载配置文件源码分析

    Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...

  7. 【Java Web开发学习】Spring加载外部properties配置文件

    [Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...

  8. jar\war\SpringBoot加载包内外资源的方式,告别FileNotFoundException吧

    工作中常常会用到文件加载,然后又经常忘记,印象不深,没有系统性研究过,从最初的war包项目到现在的springboot项目,从加载外部文件到加载自身jar包内文件,也发生了许多变化,这里开一贴,作为自 ...

  9. 【Win 10 应用开发】加载外部的 srt 字幕

    据说系统内置的多媒体功能支持 srt. ssa 等字幕,老周测试过几种格式的字幕均能加载. SRT 字幕是最简单的字幕结构,甚至你用记事本都能做出来,就是分为几行来写. 第一行是字幕的编号,应该是从1 ...

随机推荐

  1. 代码质量管理sonarqube部署使用

    一.sonarqube的部署 1.下载sonaqube:https://www.sonarqube.org/downloads/ 根据需要下载特定版本: 2.如果通过sonar-scanner进行代码 ...

  2. MongoDB分片集群机制及原理

    1. MongoDB常见的部署架构 * 单机版 * 复制集 * 分片集群 2. 为什么要使用分片集群 * 数据容量日益增大,访问性能日渐下降,怎么破? * 新品上线异常火爆,如何支撑更多用户并发? * ...

  3. 官网下载mysql的方法

    mysql官网    http://www.mysql.com/ 方法一:    (1)登陆官网 (2)把页面拉到最底部,点击Downloads(GA) 下边的MySQL Community Serv ...

  4. 初识python:time 模版

    语法及示例代码如下: import time # time 时间戳,1970年到当前时间的秒数 print('time:',time.time()) # sleep 延时.睡眠(s) print('s ...

  5. Java|从Integer和int的区别认识包装类

    https://blog.csdn.net/darlingwood2013/article/details/96969339?utm_medium=distribute.pc_relevant.non ...

  6. spring cloud feign 报错 feign.FeignException$MethodNotAllowed: status 405 reading 解决

    1.前言 出现报错 feign.FeignException$MethodNotAllowed: status 405 reading XXXXX 需要检查 接口的请求参数是否一致 请求参数是否正确添 ...

  7. IDEA开启热部署

    双击shift,查找Registry

  8. IE8和IE9下textarea滚动选中的问题

    在IE8和IE9下如果textarea设置了样式overflow-y:auto;就不可以滚动选中了,应该样式写成overflow:auto;有了纵向滚动实际上就不会出现横向滚动的情况,也没有必要ove ...

  9. Mybatis实现分包定义数据库

    Mybatis实现分包定义数据库 背景 业务需求中需要连接两个数据库处理数据,需要用动态数据源.通过了解mybatis的框架,计划 使用分包的方式进行数据源的区分. 原理 前提: 我们使用mybati ...

  10. RHCSA 第六天

    一.  创建下列用户.组和组成员资格: 1.创建名为 sysmgrs 的组 2.创建用户 natasha 同时指定sysmgrs作为natasha的附加组 3.创建用户 harry 同时指定 sysm ...