1 前言
并不是每个应用都是Web应用,也不是每个Springboot都是常驻应用,使用Springboot也能快速开发命令行(CommandLine)应用。
2 如何开发
引入Springboot依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
启动类:
@SpringBootApplication
public class NonewebApplication {
public static void main(String[] args) {
SpringApplication.run(NonewebApplication.class, args);
}
}
实现一个CommandLineRunner就可以了,注意把这个类注入:
@Component
public class AppCommandRunner implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(AppCommandRunner.class);
@Override
public void run(String... args) throws Exception {
logger.info("pkslow commandLine runner");
}
}
Springboot会为我们执行这个类的run方法,这是程序的入口,后续的其它逻辑,就看业务了。
3 总结
通过Springboot的CommandLineRunner,我们可以开发非Web项目,又能使用Springboot为我们提供的特性,十分方便。