使用java -jar
命令启动jar包或者使用传统的war包启动方式,同时spring提供了可以执行spring scripts命令的命令行工具。
系统要求,Spring Boot 2.1.3
工具 | 版本 |
---|---|
Maven | 3.3 + |
Gradle | 4.4 + |
JDK | 8 + |
Spring Framework | 5.1.5 + |
parent
标签中的spring-boot-starter-parent
,如果是web项目的话,则需要添加spring-boot-starter-web
依赖。
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>myprojectartifactId>
<version>0.0.1-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.3.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
创建一个maven项目( 也可以使用gradle进行构建 ),pom文件引入以上依赖,创建下面这个Example.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "hello spring boot";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}
启动main函数,在浏览器上输入127.0.0.1:8080
,获得返回结果
hello spring boot
打包,在项目目录下输入mvn package
命令进行打包,如果不成功,可以先执行mvn clean
D:\learn-springboot>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.learn.springboot:learn-springboot >----------------
[INFO] Building learn-springboot 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ learn-springboot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ learn-springboot ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\WorkSpace\Xuww\GitHub Repository\learn-springboot\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ learn-springboot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\Xuww\GitHub Repository\learn-springboot\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ learn-springboot ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ learn-springboot ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ learn-springboot ---
[INFO] Building jar: D:\WorkSpace\Xuww\GitHub Repository\learn-springboot\target\learn-springboot-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) @ learn-springboot ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.466 s
[INFO] Finished at: 2019-03-01T16:48:13+08:00
[INFO] ------------------------------------------------------------------------
执行 jar tvf jar包名称
,可以查看jar包中详细目录和依赖的jar包,可以参考jar命令用法。
D:\learn-springboot\target>jar tvf learn-springboot-1.0-SNAPSHOT.jar
0 Fri Mar 01 16:48:12 CST 2019 META-INF/
538 Fri Mar 01 16:48:12 CST 2019 META-INF/MANIFEST.MF
0 Fri Mar 01 16:48:12 CST 2019 org/
0 Fri Mar 01 16:48:12 CST 2019 org/springframework/
...........
1381453 Wed Feb 13 05:32:56 CST 2019 BOOT-INF/lib/spring-web-5.1.5.RELEASE.jar
672558 Wed Feb 13 05:32:08 CST 2019 BOOT-INF/lib/spring-beans-5.1.5.RELEASE.jar
800464 Wed Feb 13 05:33:32 CST 2019 BOOT-INF/lib/spring-webmvc-5.1.5.RELEASE.jar
368947 Wed Feb 13 05:32:22 CST 2019 BOOT-INF/lib/spring-aop-5.1.5.RELEASE.jar
1099682 Wed Feb 13 05:32:30 CST 2019 BOOT-INF/lib/spring-context-5.1.5.RELEASE.jar
280409 Wed Feb 13 05:32:24 CST 2019 BOOT-INF/lib/spring-expression-5.1.5.RELEASE.jar
执行java -jar learn-springboot-1.0-SNAPSHOT.jar
,在浏览器上输入http://127.0.0.1:8080/
,可以看到一下效果
hello spring boot
@RestController 告诉spring,把结果直接以字符串形式返回给调用者。
@RequestMapping 提供路由信息。
@EnableAutoConfigration 加上该注解,springboot会基于依赖的jar包进行自动配置,如果依赖了spring-boot-starter-web
这个jar包,spring boot就会以web项目的配置进行启动。
自动配置注解是为了配合
starters
设计的,但是两者之间没有必要的联系,因为在starter依赖之外,仍然可以配置其他的jar包依赖,spring boot的自动配置会根据这些依赖决定使用哪种配置。