springboot2.0的新特性:java
Web Flux优点:web
https://start.spring.io/
从上述入口快速搭建。Dependency
能够选择ReactiveWeb
/springmvc
等(或后续在pom.xml配置依赖便可)。
选择springmvc依赖后的自动生成的pom以下:spring
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>cn.whbing.learn.springboot</groupId> <artifactId>springboot-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
说明:apache
<parent>
主要用于引入默认包,及管理依赖版本。spring-boot-dependencies
是顶级依赖。经过点击spring-boot-start-parent
查看:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.4.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent> <artifactId>spring-boot-starter-parent</artifactId> <packaging>pom</packaging>
spring-boot-dependencies /\ / \ spring-boot-parent spring-boot-starter-parent
两个核心依赖包:编程
spring-boot-starter-web
: 核心模块,包括自动配置支持、日志和YAML;spring-boot-starter-test
: 测试模块,包括JUnit、Hamcrest、Mockito。Spring Boot Maven插件
参考 Spring Boot干货系列:(一)优雅的入门篇
能够使用的命令(在配置插件pom的目录下):mvn spring-boot:run
segmentfault
spring-boot-maven-plugin
插件提供了许多方便的功能:tomcat
public static void main()
方法来标记为可运行类。1.无需springMVC配置web.xml
、无需配置tomcat
、无需组件扫描等,直接写一个class,加上controller注解便可:springboot
package cn.whbing.springboot.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/hello") public String index() { return "Hello World"; } }
注:8080端口若显示被占用。能够在application.properties
换一个端口,直接配置:mvc
server.port=8888
或者关闭进程app
kill -9 port
2.启动
mvn spring-boot:run
便可启动web页面。
web模块的打包支持jar
、war
等。都是支持java -jar
命令的。
注意:将package
改为war
后,须要创建webapp
/WEB_INF
/web.xml
目录和文件(空的也行,java同级)。
即以下目录:
webapp |-WEB_INF |-web.xml
在有spring-boot-maven-plugin
的同级,执行打包命令
mvn -Dmaven.test.skip clean package
会将主包和依赖打进jar包。--> springboot-demo-0.0.1-SNAPSHOT.jar
将其解压后发现MANIFESTI.MF
已经包含了入口文件。
Start-Class: cn.whbing.springboot.SpringbootDemoApplication
和使用assembly
用自带的jar-with-dependences
相同。
此外,除了默认寻找主函数入口外,咱们能够手动指定入口:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>cn.whbing.springboot.SpringbootDemoApplication</mainClass> </configuration> </plugin>
运行:
java -jar springboot-demo-0.0.1-SNAPSHOT.jar
便可启动web页面。
java同级目录下要加入
webapp |-WEB-INF |-web.xml
一样使用java -jar XX.war
多模块之间有依赖,直接打包会出错。这时,咱们须要在build
中在此加入依赖:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies><!--打包时依赖--> <dependency> <groupId>xx...</groupId> <artifactId>xx...</artifactId> </dependency> </dependencies> </plugin> </plugins> </build>
上述方式并不可取。咱们须要将该打包插件放在最外层的依赖模块(如B-->A--C,咱们就将插件放B的pom中),打包会将全部依赖都打入。