是否可以使用 Spring 创建控制台应用程序?

App*_*ler 5 java spring console-application

目前正在研究这个课题。是否可以利用 Spring 的好处来形成 Console 应用程序?我的意思是,使用范围、自动装配等(找到了一堆基本的“设置上下文、初始化和运行”的例子,但仅此而已)?

找不到有关此主题的任何教程或有用信息。

Gri*_*ick 5

CommandLineRunner例子。如果你运行这个 Spring Boot,run 方法将是入口点。

package com.mkyong;

import com.mkyong.service.HelloMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import static java.lang.System.exit;

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {

    @Autowired
    private HelloMessageService helloService;

    public static void main(String[] args) throws Exception {

        //disabled banner, don't want to see the spring logo
        SpringApplication app = new SpringApplication(SpringBootConsoleApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);

    }

    // Put your logic here.
    @Override
    public void run(String... args) throws Exception {

        if (args.length > 0) {
            System.out.println(helloService.getMessage(args[0].toString()));
        } else {
            System.out.println(helloService.getMessage());
        }

        exit(0);
    }
}
Run Code Online (Sandbox Code Playgroud)

细节


小智 3

以下是 Spring 文档的链接:
3. 开发 Spring Shell 应用程序
4. 使用 Spring Shell 的简单示例应用程序

  • 链接已损坏 (4认同)