002-Spring Boot将WAR文件部署到Tomcat
一、概述
springboot 带有内置Tomcat 服务器,可以直接将项目打包成jar运行,如果在需要把项目打成war包,使用外置tomcat部署。下面是将springboot项目部署为war项目的一些步骤。
二、Springboot程序打成war包
1、在pom.xml中将打包形式 jar 修改为war
<packaging>war</packaging>
2、将springboot内置的Tomcat依赖移除,但是为了我们在本机测试方便,我们还需要引入它,所以配置如下:
原生Tomcat依赖移除
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
如果是thymeleaf 模板引擎依赖移除
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
3、如果jsp,servlet等需求,需添加tomcat-servelt-api依赖
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>7.0.42</version> <scope>provided</scope> </dependency>
4、修改入口方法 继承一个SpringBootServletInitializer类,并且覆盖configure方法
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(application名称.class); } }
5、配置启动即可【可不用配置】
<!-- Tomcat --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <!--设置访问路径--> <path>/${project.artifactId}</path> <!--访问端口--> <port>8081</port> </configuration> </plugin>
注:<scope>provided</scope>表示在编译和测试时使用(不加它,打的包中会指定tomcat,用tomcat部署时会因tomcat版本报错;而加上它,打包时不会把内置的tomcat打进去)
还要注意:spring-boot项目使用的jdk版本要和tomcat的jdk版本一致(都是1.8);tomcat的lib中el-api.jar版本最好要是javax.el-api-3.0.0.jar版本,防止低版本冲突。
需要注意的是这样部署的request url需要在端口后加上项目的名字才能正常访问。
spring-boot更加强大的一点就是:即便项目是以上配置,依然可以用内嵌的tomcat来调试,启动命令和以前没变
三、maven中三种classpath
编译,测试,运行
1.compile:默认范围,编译测试运行都有效
2.provided:在编译和测试时有效
3.runtime:在测试和运行时有效
4.test:只在测试时有效
5.system:在编译和测试时有效,与本机系统关联,可移植性差
四、问题
配置完上述,使用tomcat可以运行,但是直接运行main提示不可用缺少servlet等
网上答案,修改spring-boot-starter-tomcat 为compile,此时可以使用,但是打包时候,多了tomcat,再部署到tomcat容器时候出现问题。
参看解决方案:
https://github.com/spring-projects/spring-boot/issues/5666:文章提示看以下文章
https://stackoverflow.com/questions/32531422/spring-boot-jsp-error-noclassdeffounderror:提示以下几种解决方案
There's a bug in IntelliJ that means that provided dependencies aren't added to the classpath. Assuming you want to stick with IDEA, you have a few options: Manually configure the classpath in IDEA Run the samples on the command line using mvn spring-boot:run Remove all occurrences of <scope>provided</scope> from the pom. This will mean that app can't be deployed as a war to Tomcat or similar EDIT: The bug is fixed and the server will start normally, as long as you tick the Include dependencies with "Provided" scope checkbox in the run configuration, below classpath.
可以看到
方法一、在IDEA中手动配置类路径【推荐】
在Edit Configurations中,找到自己的application项目use classpath of module,选中include dependcenies with "provided" scope,即可
对于Spring-Boot项目默认是启用的,如果默认没启用所以报错
旧版本的idea,没有此选项,新的版本已经修复了,可以在官网下载新版本IntelliJ IDEA 2018.1.4,默认是勾选Include dependencies with “Provided” scope的,这样就不用每次注释了!
方法二、使用mvn spring-boot:run在命令行上运行【推荐】
方式三、移除<scope>provided</scope>,但是不能再war包上使用。【不推荐】
使用spring-boot 2.0.4
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.1.Final</version> </dependency>
相关文章
- Spring 使用注解进行 Bean 管理 | Spring学习笔记2.0
- Spring Framework 源码学习笔记(六)
- Spring IOC容器的初始化过程
- Spring入门介绍
- idea配置Tomcat_tomcat docbase
- Maven配置Tomcat_maven和tomcat的区别
- spring官方文档 中文_Spring软件
- Spring-boot_Spring Boot
- spring-websocket实现聊天室功能
- spring boot项目中handlerinterceptor autowired 为空解决办法
- spring的AOP(五)—-Spring AOP 编程(AspectJ )详解编程语言
- Spring Boot Tomcat配置详解编程语言
- 服务安装安装Linux Tomcat服务,轻松管理Web应用程序(linux的tomcat)
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
- 使用Spring boot 嵌入的tomcat不能启动: Unregistering JMX-exposed beans on shutdown详解编程语言
- spring使用Spring整合Redis和Jedis构建高性能应用(redisjedis与)
- Linux查看Tomcat端口的方法(linux查看tomcat端口)
- Linux下查看Tomcat日志的方法(linux查看tomcat日志)
- Linux下快速安装Tomcat服务器(linux下安装tomcat)
- 搭建Tomcat连接MySQL数据库的快速指南(tomcat连接mysql数据库)
- 安装Linux上的Tomcat服务器(linux安装tomcat)
- 在Linux下轻松启动Tomcat(linux下启动tomcat)
- Tomcat与MySQL助力Web应用程序开发(tomcat和mysql)
- Tomcat加Redis提升程序效率的新黄金搭档(tomcat加redis)