解决spring-boot 打成jar包后 启动时指定参数无效的问题

spring-boot打成jar启动时指定参数无效

今天后台项目进行修改,使用spring.profiles来指定启动时使用的配置文件。

在项目中添加好配置文件后使用java -jar .\base-exec.jar –spring.profiles.active=dev –server.port=9121启动时参数注入不进去。

检查配置文件书写的规则,这里把规则说一下

我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。

对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

application-dev.properties:开发环境

application-test.properties:测试环境

application-prod.properties:生产环境

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

如:spring.profiles.active=test就会加载application-test.properties配置文件内容

下面,以不同环境配置不同的服务端口为例,进行样例实验。

针对各环境新建不同的配置文件application-dev.properties、application-test.properties、application-prod.properties

在这三个文件均都设置不同的server.port属性,如:dev环境设置为8080,test环境设置为9090,prod环境设置为80

application.properties中设置spring.profiles.active=dev,就是说默认以dev环境设置

测试不同配置的加载:

执行java -jar xxx.jar,可以观察到服务端口被设置为8080,也就是默认的开发环境(dev)

执行java -jar xxx.jar –spring.profiles.active=test,可以观察到服务端口被设置为9090,也就是测试环境的配置(test)

执行java -jar xxx.jar –spring.profiles.active=prod,可以观察到服务端口被设置为80,也就是生产环境的配置(prod)

按照上面的实验,可以如下总结多环境的配置思路:

application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置

application-{profile}.properties中配置各个环境不同的

检查setAddCommandLineProperties配置

在应用中管理配置并不是一个容易的任务,尤其是在应用需要部署到多个环境中时。通常会需要为每个环境提供一个对应的属性文件,用来配置各自的数据库连接信息、服务器信息和第三方服务账号等。通常的应用部署会包含开发、测试和生产等若干个环境。不同的环境之间的配置存在覆盖关系。测试环境中的配置会覆盖开发环境,而生产环境中的配置会覆盖测试环境。Spring 框架本身提供了多种的方式来管理配置属性文件。Spring 3.1 之前可以使用 PropertyPlaceholderConfigurer。

Spring 3.1 引入了新的环境(Environment)和概要信息(Profile)API,是一种更加灵活的处理不同环境和配置文件的方式。不过 Spring 这些配置管理方式的问题在于选择太多,让开发人员无所适从。Spring Boot 提供了一种统一的方式来管理应用的配置,允许开发人员使用属性文件、YAML 文件、环境变量和命令行参数来定义优先级不同的配置值。

Spring Boot 所提供的配置优先级顺序比较复杂。按照优先级从高到低的顺序,具体的列表如下所示。

命令行参数。

通过 System.getProperties() 获取的 Java 系统参数。

操作系统环境变量。

从 java:comp/env 得到的 JNDI 属性。

通过 RandomValuePropertySource 生成的“random.*”属性。

应用 Jar 文件之外的属性文件。(通过spring.config.location参数)

应用 Jar 文件内部的属性文件。

在应用配置 Java 类(包含“@Configuration”注解的 Java 类)中通过“@PropertySource”注解声明的属性文件。

通过“SpringApplication.setDefaultProperties”声明的默认属性。

Spring Boot 的这个配置优先级看似复杂,其实是很合理的。比如命令行参数的优先级被设置为最高。

这样的好处是可以在测试或生产环境中快速地修改配置参数值,而不需要重新打包和部署应用。

SpringApplication 类默认会把以“–”开头的命令行参数转化成应用中可以使用的配置参数,如 “–name=Alex” 会设置配置参数 “name” 的值为 “Alex”。如果不需要这个功能,可以通过 “SpringApplication.setAddCommandLineProperties(false)” 禁用解析命令行参数。

检查setAddCommandLineProperties配置

public static void main(String[] args) {    SpringApplication springApplication = new SpringApplication(WebApplication.class);    springApplication.run(args);}

检查args参数是否传入,我的项目的问题就在这

public static void main(String[] args) {     new SpringApplication.run(WebApplication.class);}

spring-boot 项目打包后无法通过命令行传入参数

java -jar .\tk-provider.jar --spring.profiles.active=test

本想用测试环境的配置文件运行项目可项目启动时一直是使用dev配置文件运行。

java -jar .\tk-provider.jar --spring.profiles.active=testSLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/D:/softwareWorkspace/hqtx/HqService/ServiceProvider/tk-provider/target/tk-provider.jar!/BOOT-INF/lib/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/D:/softwareWorkspace/hqtx/HqService/ServiceProvider/tk-provider/target/tk-provider.jar!/BOOT-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]====================> : Spring Boot 初始化环境变量  .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v2.0.4.RELEASE)2018-08-23 16:14:48.494  INFO 349004 --- [           main] com.hq.tk.TkApplication                  : Starting TkApplication v1.0-SNAPSHOT on longqin with PID 349004 (D:\softwareWorkspace\hqtx\HqService\ServiceProvider\tk-provider\target\tk-provider.jar started by Administrator in D:\softwareWorkspace\hqtx\HqService\ServiceProvider\tk-provider\target)2018-08-23 16:14:48.497 DEBUG 349004 --- [           main] com.hq.tk.TkApplication                  : Running with Spring Boot v2.0.4.RELEASE, Spring v5.0.8.RELEASE2018-08-23 16:14:48.498  INFO 349004 --- [           main] com.hq.tk.TkApplication                  : The following profiles are active: dev

尝试了无数遍启动都是出现: The following profiles are active: dev,快要崩溃了。后来冷静想了想 命令行的参数是通过 main函数中的args参数接收的,立马去查看启动类,果然。

一开始的写法,springApplication.run 没有传入args参数

public static void main(String[] args) {        SpringApplication springApplication = new SpringApplication(SsoApplication.class);        //监听生命周期        springApplication.addListeners(new SpringBootApplicationStartup());        springApplication.run();    }

更改

public static void main(String[] args) {        SpringApplication springApplication = new SpringApplication(SsoApplication.class);        //监听生命周期        springApplication.addListeners(new SpringBootApplicationStartup());        springApplication.run(args);    }

再次打包运行,出现 :The following profiles are active: test

java -jar .\tk-provider.jar --spring.profiles.active=testSLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/D:/softwareWorkspace/hqtx/HqService/ServiceProvider/tk-provider/target/tk-provider.jar!/BOOT-INF/lib/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/D:/softwareWorkspace/hqtx/HqService/ServiceProvider/tk-provider/target/tk-provider.jar!/BOOT-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]====================> : Spring Boot 初始化环境变量  .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v2.0.4.RELEASE)2018-08-23 16:30:58.961  INFO 348708 --- [           main] com.hq.tk.TkApplication                  : Starting TkApplication v1.0-SNAPSHOT on longqin with PID 348708 (D:\softwareWorkspace\hqtx\HqService\ServiceProvider\tk-provider\target\tk-provider.jar started by Administrator in D:\softwareWorkspace\hqtx\HqService\ServiceProvider\tk-provider\target)2018-08-23 16:30:58.964 DEBUG 348708 --- [           main] com.hq.tk.TkApplication                  : Running with Spring Boot v2.0.4.RELEASE, Spring v5.0.8.RELEASE2018-08-23 16:30:58.966  INFO 348708 --- [           main] com.hq.tk.TkApplication                  : The following profiles are active: test

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

而它的种子,就是它生命的延续,继续承受风,

解决spring-boot 打成jar包后 启动时指定参数无效的问题

相关文章:

你感兴趣的文章:

标签云: