<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Some Tips Of Spring Boot

          共 3379字,需瀏覽 7分鐘

           ·

          2022-04-10 22:47

          須彌零一

          Accessing Application Arguments

          If you need to access the application arguments that were passed to?SpringApplication.run(…), you can inject a?org.springframework.boot.ApplicationArguments?bean. The?ApplicationArguments?interface provides access to both the raw?String[]?arguments as well as parsed?option?and?non-option?arguments, as shown in the following example:

          import?java.util.List;

          import?org.springframework.boot.ApplicationArguments;
          import?org.springframework.stereotype.Component;

          @Component
          public?class?MyBean?{

          ????public?MyBean(ApplicationArguments?args)?{
          ????????boolean?debug?=?args.containsOption("debug");
          ????????List<String>?files?=?args.getNonOptionArgs();
          ????????if?(debug)?{
          ????????????System.out.println(files);
          ????????}
          ????????//?if?run?with?"--debug?logfile.txt"?prints?["logfile.txt"]
          ????}

          }

          Spring Boot also registers a?CommandLinePropertySource?with the Spring?Environment. This lets you also inject single application arguments by using the?@Value?annotation.

          Using the ApplicationRunner or CommandLineRunner

          If you need to run some specific code once the?SpringApplication?has started, you can implement the?ApplicationRunner?or?CommandLineRunner?interfaces. Both interfaces work in the same way and offer a single?run?method, which is called just before?SpringApplication.run(…)?completes.

          The?CommandLineRunner?interfaces provides access to application arguments as a string array, whereas the?ApplicationRunner?uses the?ApplicationArguments?interface discussed earlier. The following example shows a?CommandLineRunner?with a?run?method:

          import?org.springframework.boot.CommandLineRunner;
          import?org.springframework.stereotype.Component;

          @Component
          public?class?MyCommandLineRunner?implements?CommandLineRunner?{

          ????@Override
          ????public?void?run(String...?args)?{
          ????????//?Do?something...
          ????}

          }

          If several?CommandLineRunner?or?ApplicationRunner?beans are defined that must be called in a specific order, you can additionally implement the?org.springframework.core.Ordered?interface or use the?org.springframework.core.annotation.Order?annotation.

          Application Exit

          Each?SpringApplication?registers a shutdown hook with the JVM to ensure that the?ApplicationContext?closes gracefully on exit. All the standard Spring lifecycle callbacks (such as the?DisposableBean?interface or the?@PreDestroy?annotation) can be used.

          In addition, beans may implement the?org.springframework.boot.ExitCodeGenerator?interface if they wish to return a specific exit code when?SpringApplication.exit()?is called. This exit code can then be passed to?System.exit()?to return it as a status code, as shown in the following example:

          import?org.springframework.boot.ExitCodeGenerator;
          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;
          import?org.springframework.context.annotation.Bean;

          @SpringBootApplication
          public?class?MyApplication?{

          ????@Bean
          ????public?ExitCodeGenerator?exitCodeGenerator()?{
          ????????return?()?->?42;
          ????}

          ????public?static?void?main(String[]?args)?{
          ????????System.exit(SpringApplication.exit(SpringApplication.run(MyApplication.class,?args)));
          ????}

          }

          Also, the?ExitCodeGenerator?interface may be implemented by exceptions. When such an exception is encountered, Spring Boot returns the exit code provided by the implemented?getExitCode()?method.


          ---- END ----



          歡迎關注我的公眾號“須彌零一”,原創(chuàng)技術文章第一時間推送。


          瀏覽 45
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产一区在线观看视频 | 蜜芽成人社区 | 夜色福利精品 | 久久久久久久中文字幕 | 最新国产亚洲免费在线视频 |