如何在不使用参数args的情况下启动Spring Web Boot程序?

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

how can I start a spring web boot program without parameter args?

问题

我有一个需求,即在不使用参数args的情况下通过Swing按钮启动Spring Boot程序。

startHttpServerBtn.addActionListener((e) 
                -> new Thread(()
                -> BootWebApplication.start()).start());

我该如何实现它?谢谢。

英文:

I have a demand that starting the spring boot program without paragram args using a swing button.

startHttpServerBtn.addActionListener((e)
                -> new Thread(()
                -> BootWebApplication.start()).start());

How can i relaize it? Thanks.

答案1

得分: 2

假设您的 BootWebApplication 类使用 @SpringBootApplication 进行了注解,并且具有由 Spring Initializer 生成的默认 public static void main 方法。
然后您可以轻松地使用或不使用程序 args 来启动 Spring Boot 应用。
带有 args 的代码示例:

startHttpServerBtn.addActionListener((e)
                -> new Thread(() -> {
        String[] args = new String[1];
        args[0] = "--spring.profiles.active=dev";
        BootWebApplication.main(args);
    }).start());

要在没有 args 的情况下运行,只需传递空字符串数组: BootWebApplication.main(new String[0]);

英文:

Let's assume your BootWebApplication class annotated with @SpringBootApplication and have default public static void main method generated by Spring Initializer. <br>
Then you can easy start Spring Boot app with or without program args.<br>
Code example with args:

startHttpServerBtn.addActionListener((e)
                -&gt; new Thread(() -&gt; {
        String[] args = new String[1];
        args[0] = &quot;--spring.profiles.active=dev&quot;;
        BootWebApplication.main(args)
    }).start());

To run without args just pass empty String array: BootWebApplication.main(new String[0]);

huangapple
  • 本文由 发表于 2020年9月8日 11:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63786638.html
匿名

发表评论

匿名网友

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

确定