Spring Boot可以用作命令行应用程序和服务器吗?

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

Can Spring Boot use as command line app and as server?

问题

可以将Spring Boot应用程序用作带有命令行开关的命令行应用程序,同时也用作服务器吗?我希望用户能够使用命令参数调用应用程序,执行所需的操作,然后退出。

但是,我还希望应用程序具有作为服务器启动并监听REST请求的能力。例如,对于命令行用法:

'./myapp -n:1 -b:2',

而对于服务器模式:

'./myapp -a:server',

它会将应用程序启动为服务器。

英文:

Is it possible to use a Spring Boot app as a command-line application with command-line switches, as well as a server? I would like users to be able to invoke the app with command arguments, execute the desired action, and then exit.

However, I would also like the app to have the ability to start as a server and listen for REST requests. For example, for the command-line usage:

'./myapp -n:1 -b:2',

And for the server mode:

 './myapp -a:server'`,

it would start the app as a server.

答案1

得分: 3

当然可以。关于这个主题基本上有一个讨论 链接,但基本上你可以定义一个程序参数,然后在 main 方法中创建一个简单的 if...else 来满足你的需求:

public static void main(String[] args) {
    if ("n1".equals(args[0])) {
        // foo
    } else {
        SpringApplication.run(Bar.class, args);
    }
}
英文:

Of course you can. There was a discussion for basically this topic here, but basically you can define a program argument and then create a simple if...else in main method for your needs:

public static void main(String[] args) {
    if ("n1".equals(args[0])) {
        // foo
    } else {
        SpringApplication.run(Bar.class, args);
    }
}

答案2

得分: 2

你可以使用SpringApplicationBuilder并根据属性设置web参数为NONE/SERVLET,或者修改传入的参数以添加一个参数,以便Spring Boot知道该如何处理。无论哪种方式都可以工作。

public static void main(String[] args) {

  var server = ObjectUtils.containsElement(args, "-a:server");
  var type = server ? WebApplicationType.SERVLET : WebApplicationType.NONE;
  var application = new SpringApplicationBuilder()
     .sources(Bar.class)
     .web(type).build();
  application.run(args);
}

或者,你可以修改传入的参数,添加spring.main.web-application-type,其值基于你传入的内容。

public static void main(String[] args) {

  var server = ObjectUtils.containsElement(args, "-a:server");
  var param = "spring.main.web-application-type=" + (server ? "SERVLET" : "NONE");
  var argsToUse = ObjectUtils.addObjectToArray(args, param);
  SpringApplication.run(Bar.class, argsToUse);
}
英文:

You've got options, you can use the SpringApplicationBuilder and based on the property you can set the web parameter to NONE/SERVLET or you could modify the incoming arguments to add a parameter so Spring Boot knows what to do. Either way should work.

public static void main(String[] args) {

  var server = ObjectUtils.containsElement(args, "-a:server");
  var type = server ? WebApplicationType.SERVLET : WebApplicationType.NONE;
  var application = new SpringApplicationBuilder()
     .sources(Bar.class)
     .web(type).build();
  application.run(args);
}

Or you could modify the incoming arguments and add spring.main.web-application-type with the value based on what you pass in.

public static void main(String[] args) {

  var server = ObjectUtils.containsElement(args, "-a:server");
  var param = "spring.main.web-application-type=" + ( server ? "SERVLET" : "NONE");
  var argsToUse = ObjectUtils.addObjectToArray(args, param);
  SpringApplication.run(Bar.class, argsToUse);
}

huangapple
  • 本文由 发表于 2023年6月15日 16:55:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480797.html
匿名

发表评论

匿名网友

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

确定