SpringBoot总结
运行方式
直接在IDE中运行入口类
@SpringBootApplication
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
通过spring-boot-maven-plugin 打成可执行jar包,通过java -jar运行
打包插件(通过start.spring.io创建的工程默认就有)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
可通过Terminal利用命令mvn clean package 来执行,可直接java -jar,或将jar包放在外部tomcat里运行。
[INFO]
[INFO] — spring-boot-maven-plugin:2.2.0.RELEASE:repackage (repackage) @ springboot-01 —
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 55.759 s
[INFO] Finished at: 2019-11-04T22:05:26+08:00
[INFO] ------------------------------------------------------------------------
通过mvn spring-boot:run运行
打包部署
jar包
(SpringBoot官方建议通过SpringBoot提供的打包插件将项目打包成一个可执行的jar包pom.xml文件中没写packaging标签的话默认是jar包(在这个jar包里面已经内置了Tomcat,可以直接当一个web跑),方便部署和管理。)
如上述的打包插件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- ... -->
<packaging>jar</packaging>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
通过mvn clean package
java -jar 文件名.jar
D:\JAVA\IdeaProjects\springboot-01>dir
D:\JAVA\IdeaProjects\springboot-01> cd target
D:\JAVA\IdeaProjects\springboot-01\target>dir
D:\JAVA\IdeaProjects\springboot-01\target>java -jar springboot-01-0.0.1-SNAPSHOT.jar
在外部tomcat中运行,找到本机tomcat安装路径,bin目录下找到startup.bat,开启,运行后到浏览器里打开项目注意端口号后加项目名,访问页面。最后,shutdown.bat关闭tomcat服务器。
直接按照电脑系统中的路径找到jar包,或用命令行窗口执行命令,开启tomcat与上述的步骤一样。
注意, 如果你使用了src/main/webapp目录,打成jar包会忽略掉该目录下的所有文件,这种情况下只能选择打war包的方式
war包
- 修改pom.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 重点... -->
<packaging>war</packaging>
<dependencies>
<!-- 必须将tomcat标记为provided,阻止将内置的tomcat包打到war包中,以防与外部tomcat容器中的包冲突 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 支持JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test -->
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.注册启动类