springBoot项目自定义命名application.properties配置文件名称

canca2年前 (2022-05-10)Spring342

为⽅便期间,更改名称后的properties⽂件仍然放置在resource下(相当于classpa:/ 在classpath的根⽬录下)即可;

解决⽅法

解决⽅法⼀:

将更改properties⽂件名的项⽬使⽤Maven⼯具打成JAR包,然后在DOS命令⾏启动项⽬:

java -jar myDemo.jar --spring.config.name=config_demo

仍然是以JAR的形式来启动项⽬,在项⽬启动前设置好环境变量;

set SPRING_CONFIG_NAME=config_demo
java -jar myDemo.jar

上⾯两种⽅法需要在项⽬启动命令中更改环境变量,⽐较⿇烦。下⾯介绍最实⽤的使⽤更改代码的⽅式来实现,使得springboot项⽬启动

加载时默认去读取更改名称后的properties⽂件:

通过SpringApplicationBuilder类的properties(String… defaultProperties)⽅法来实现,代码如下:

@SpringBootApplication
public class DemoSpringbootApplication {
   public static void main(String[] args) {
       new SpringApplicationBuilder(DemoApplication.class)
               .properties("spring.config.name:config_demo")
               .build()
               .run(args);
   }
}

相关文章

Springboot中的context-path

1.定义Context path of the application. 应⽤的上下⽂路径,也可以称为项⽬路径,是构成url地址的⼀部分。2.如何配置SpringBoot 2.0.0.RELEASE版...

SpringBoot中的application.properties引用pom.xml中变量

SpringBoot中的application.properties引用pom.xml中变量

在SpringBoot的默认配置文件application.properties中引用pom.xml中的变量在application.properties中我们引用了hostname这个变量,其中@h...

配置文件application.properties参数详解

springboot提供了许多启动器starter,大部分的启动器都有配置属性,这些配置属性一般可以在这里找到:   xxxxxxxx-autoconfigure-xxxxx.jar/M...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。