Spring boot reads project parameter configuration

Ask Youlong time 2021-10-14 04:49:48
spring boot reads project parameter


1、 brief introduction

spring boot Some parameters can be configured when the project starts , We can do that by implementing ApplicationRunner or CommandLineRunner Interface , And rewrite run Method to get these configurations , If there are multiple classes defined in the project and these two classes are implemented , adopt @Order Annotations to specify their execution order .

spring boot The project can specify project operation parameters at startup ,eg: java -jar --user=wenyl

2、ApplicationRunner

2.1、 The source code parsing

ApplicationRunner Of run The method parameter is ApplicationArguments, There are five internal methods

public interface ApplicationArguments {
String[] getSourceArgs();
Set<String> getOptionNames();
boolean containsOption(String name);
List<String> getOptionValues(String name);
List<String> getNonOptionArgs();
}

We add startup parameters as follows

image.png

getSourceArgs() Space as separator , Get all configurations , And returns... As a string

image.png

getOptionNames() Method to get the configuration item ( Configure items to -- start ) The name of , The value returned by this method is set aggregate , Expected value of a configuration item , It could be an array , It needs to be defined separately, but name It's all the same

image.png

getOptionValues() Method is used to get the value of the configuration item , It returns an array , This means that we can define different values for the same configuration item , As we added name attribute , Two values are defined

image.png

getNonOptionArgs() Method is used to obtain the configuration item parameters , In the test configuration , There is one hello The parameter is No -- It's the only prefix you get

image.png

2.2、 Case study

@Component
public class MessageApplicationRunner1 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(" Start loading ");
List<String> nonOptionArgs = args.getNonOptionArgs();
Set<String> optionNames = args.getOptionNames();
for(String optionName:optionNames){
System.out.println(" Parameters -"+optionName+" value :"+ args.getOptionValues(optionName));
}
String[] sourceArgs = args.getSourceArgs();
System.out.println(" End loading ");
}
}

3、CommandLineRunner

CommandLineRunner No, ApplicationRunner So many operations , Just separated by spaces , Interception configuration ,2.1 The same configuration in , The information obtained is as follows

image.png

@Component
public class MessageApplicationRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(args);
}
}

4、ApplicationRunner and CommandLineRunner The difference between

ApplicationRunner Of run The parameters of the method are ApplicationArguments, and CommandLineRunner Of run The parameters of the method are String type

image.pngimage.png


title :spring boot Read the project parameter configuration
author : wenyl
Address : http://www.wenyoulong.com/articles/2021/09/27/1632708755896.html

版权声明
本文为[Ask Youlong time]所创,转载请带上原文链接,感谢
https://javamana.com/2021/10/20211002150219410G.html

  1. What are the new features of Java 10?
  2. What are the new features of Java 9?
  3. Is the report too ugly? Try this open source project reportplus!
  4. Spring 2021 report released: who is the real brother?
  5. À bientôt, docker.
  6. Recommend a super easy to use markdown editor
  7. Java 17 new features overview
  8. New developer friendly features after Java 8
  9. Concise and efficient eclipse collections API
  10. Practice of deploying spring boot microservices on kubernetes
  11. Garbage collectors in the new JDK: Shenandoah, ZGC and improved G1
  12. À bientôt, docker.
  13. Oracle exécute le format syntaxique d'une procédure stockée ou d'un lot
  14. [Azure Cloud Service] Azure Cloud Service ajoute des champs personnalisés pour le rôle Web (hôte IIS) (ajoute le champ user agent dans l'en - tête de demande http au Journal de sortie IIS)
  15. The Linux kernel will be implemented with the latest zstd
  16. JetBrains publishes the raw data of the developer ecosystem survey in 2021
  17. Linux 5.16 will support KVM and disable amd PSF function
  18. Linux 5.16 brings DisplayPort 2.0 initial support for amdgpu driver
  19. Announcement of the election results of the technical advisory committee of the Linux foundation in 2021
  20. Java虚拟机|JVM【适合初学者入门】
  21. Spring Cloud 2021.0.0-M2 发布
  22. [Azure Cloud Service] Azure Cloud Service ajoute des champs personnalisés pour le rôle Web (hôte IIS) (ajoute le champ user agent dans l'en - tête de demande http au Journal de sortie IIS)
  23. Spring Cloud 2021.0.0-M2 發布
  24. Java虛擬機|JVM【適合初學者入門】
  25. Spring Cloud 2021.0.0-m2 Release
  26. Machine virtuelle Java | JVM [convient aux débutants]
  27. 10412 Xiaopeng cars announced sales results in September
  28. Apprendre le mécanisme des événements Java
  29. Java script RPC (07) - timeout timeout Processing from zero
  30. Introduction à la page Web de rabbitmq (3)
  31. MySQL read - Write Separation Cluster Construction and Integration with mycat
  32. Analyse du principe du conteneur du CIO printanier
  33. Java面向对象基础
  34. Java communique avec les capteurs industriels via socket et DTu, RTU
  35. Xiaopeng automobile delivered 10412 units in September, with a monthly sales volume of more than 10000 for the first time
  36. Java Object Oriented Foundation
  37. Introduction au développement Android PDF, un tour pour vous apprendre à comprendre netty
  38. Hadoop Foundation - 01 - concepts de base
  39. Hadoop Foundation - 02 - Basic Structure
  40. Hadoop Foundation - 03 - hdfs (Hadoop Distributed File System) Basic Concepts
  41. Hadoop Foundation - 04 - hdfs API Programming
  42. Hadoop Foundation - 05 - hdfs Project (word Frequency Statistics)
  43. Hadoop Foundation - 06 - hdfs Data Read and write
  44. The "monthly test" report card of the new car built under the lack of core: Xiaopeng and Weilai took the lead in "breaking 10000", and the ideal plummeted by 25%
  45. Introduction to making arch linux software package
  46. Hard core observation 407 HTTPS everywhere browser extension is about to retire
  47. How to use busybox on Linux
  48. In 2021, the talent incentive plan of Linux foundation open source software School Park was officially launched
  49. It's not windows or Linux. Shrink is the "God operating system"
  50. Install anydesk on Ubuntu Linux
  51. 2021, can we recommend using Linux to play games?
  52. not exist:org.springframework.kafka.listener.AbstractMessageListenerContaingetContainerProperties()
  53. Are you sure HTTPS is asymmetric encryption for content encryption? See the answers and reasons
  54. MySQL online slow log query
  55. Java JDK server installation
  56. 手把手教你使用Java开发在线生成pdf文档
  57. Questions d'entrevue dans la base de données MySQL (dernière version 2021)
  58. Java零基础小白入门必做题汇总(建议收藏)第一篇
  59. Day15 Java Foundation
  60. Day16 Java Foundation