我正在研究这个话题。是否有可能利用Spring的优势来形成一个控制台应用程序?我的意思是,使用作用域、自动装配等功能(找到了一些基本的“设置上下文、初始化和执行”的示例,但没有更多的信息)。
在这个话题上找不到任何教程或有用的信息。
我正在研究这个话题。是否有可能利用Spring的优势来形成一个控制台应用程序?我的意思是,使用作用域、自动装配等功能(找到了一些基本的“设置上下文、初始化和执行”的示例,但没有更多的信息)。
在这个话题上找不到任何教程或有用的信息。
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);
}
}
Spring是一个框架,提供了依赖注入机制。它还支持服务层、Web层和业务层的分离,但我相信这不是你想要的。
所以答案是是。您可以利用提供的依赖注入服务来创建控制台应用程序而不会遇到任何问题。当然,它提供的MVC模型对您实际上没有用处。