前面尝试了用spring boot 搭建入门的web, war 包工程,内置了一个tomcat。今天尝试直接用spring boot 实现一个jar工程,可以直接执行的 jar 工程,注意是非 web 工程,也就是普通的可以执行的jar ,后面可以跟参数,说白了就是通常用得很多的命令行模式,spring boot 对这个也有很好的支持, 废话就不多说了,码农就直接上代码了。这个代码是基于上一个程序拷贝过来改动的。可以参考原来的web版:
http://www.yihaomen.com/article/java/649.htm1. Maven配置文件变化
程序代码
<project xmlns="
http://maven.apache.org/POM/4.0.0" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yihaomen</groupId>
<artifactId>SpringBoot-002</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot-002 Non-Web Appliation</name>
<url>
http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 实现CommandLineRunner接口并实现run方法, 这是重点。
程序代码
package com.yihaomen.springboot;
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 com.yihaomen.springboot.service.MyCommandService;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private MyCommandService commandService;
public static void main(String[] args) throws Exception {
//不想看见 spring的logo
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
@Override
public void run(String... args) throws Exception {
if (args.length > 0 ) {
System.out.println(commandService.getMessage(args[0].toString()));
}else{
System.out.println(commandService.getMessage());
}
System.exit(0);
}
}
3. 自己随便写个service , 表示来处理各种命令。
程序代码
package com.yihaomen.springboot.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class MyCommandService {
@Value("${name:unknown}")
private String name;
public String getMessage() {
return getMessage(name);
}
public String getMessage(String name) {
return "Hello " + name;
}
}
4. 还想强调一点的是,如果在cmd命令下执行时,如果想去掉一些多余的日志,那就一定要配置logback了,这里用了logback, log4j的姊妹。 配置后放在 resource 目录下,logback.xml.
程序代码
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<logger name="org.springframework.web" level="error" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="com.yihaomen" level="error" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<root level="error">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
5. 打包,直接用maven , 到工程目录下执行 mvn clean package. 打包之后,可以直接执行.
6. 源码下载 maven 版本.
spring boot non web application,jar application