如何在Spring Boot中更改配置变量名称?

huangapple go评论105阅读模式
英文:

How to change config variable names in spring boot?

问题

我正在使用 Spring Boot 构建一个独立的应用程序。我希望它能够读取用户提供的属性文件,但我想自定义命令行 API。我不想使用 --spring.config.location=...,而想使用 --config=...。Spring Boot 是否支持这种类型的定制?我该如何实现这一点?

英文:

I'm building a standalone app with spring boot. I want it read properties file provided by user but i want to customize command line API. i don't want --spring.config.location=... instead i would like --config=.... does spring boot support this kind of customization? how can i achieve this?

答案1

得分: 1

是的,可以使用--config而不是--spring.config.location=...,但这会带来一些成本。

假设配置文件位于_C:/temp/myconfig.properties_

  1. my.message = Hello outside

使用你自己的 ConfigurationProperties

  1. @Configuration
  2. @PropertySource("${config}")
  3. @ConfigurationProperties(prefix = "my")
  4. class ApplicProps {
  5. private String message;
  6. public String getMessage() {
  7. return message;
  8. }
  9. public void setMessage(String message) {
  10. this.message = message;
  11. }
  12. }

然后你应该能够像这样启动应用程序:

java -jar your_application.jar --config=file:C:/temp/myconfig.properties


另一种方法是通过命令行选项参数来实现。由于--config作为一个参数传入,你可以这样启动应用程序:

  1. public static void main(String[] args) {
  2. // 允许的参数
  3. String[] acceptedArgs = new String[1];
  4. for (String arg: args) {
  5. if(arg.startsWith("--config")){
  6. // 将 --config 转换为默认的 --spring.config.location
  7. acceptedArgs[0] = arg.replace("--config","--spring.config.location");
  8. }
  9. }
  10. SpringApplication.run(YourApp.class, acceptedArgs); // <-- 使用 acceptedArgs
  11. }
英文:

Yes, it is possible to --config instead of --spring.config.location=... but it comes with a cost.

Assume C:/temp/myconfig.properties

  1. my.message = Hello outside

Use your own ConfigurationProperties

  1. @Configuration
  2. @PropertySource(&quot;${config}&quot;)
  3. @ConfigurationProperties(prefix = &quot;my&quot;)
  4. class ApplicProps {
  5. private String message;
  6. public String getMessage() {
  7. return message;
  8. }
  9. public void setMessage(String message) {
  10. this.message = message;
  11. }
  12. }

then you should be able to start your application like:

java -jar your_application.jar --config=file:C:/temp/myconfig.properties


An other way to do this is to play with the command line option arguments.
And because --config comes in as an argument you could start you application like:

  1. public static void main(String[] args) {
  2. // allowed arguments
  3. String[] acceptedArgs = new String[1];
  4. for (String arg: args) {
  5. if(arg.startsWith(&quot;--config&quot;)){
  6. // convert --config to the default --spring.config.location
  7. acceptedArgs[0] = arg.replace(&quot;--config&quot;,&quot;--spring.config.location&quot;);
  8. }
  9. }
  10. SpringApplication.run(YourApp.class, acceptedArgs); // &lt;-- use acceptedArgs
  11. }

huangapple
  • 本文由 发表于 2020年10月20日 00:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64431840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定