用WAR的方式部署SpringBoot项目
接SpringBoot 快速入门(Eclipse)
步骤一:部署方式
Springboot 和 web 应用程序不一样,其本质上是一个 Java 应用程序,那么又如何部署呢?
通常来说,Springboot 部署会采用两种方式:全部打包成一个jar,或者打包成一个war。
下面是war的方式。
步骤二:可运行项目
该项目是springboot入门的小demo
步骤三:修改 Application类
application修改如下代码
新加@ServletComponentScan注解,并且继承SpringBootServletInitializer 。(规定这么改)
package cn.xdf.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication @ServletComponentScan public class Application extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
步骤四:修改 pom.xml文件
pom.xml修改为如下代码,主要两个改动
新加打包成war的声明: <packaging>war</packaging>
<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.xdf</groupId> <artifactId>springbootTest</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot </name> <description>springboot </description> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.5</version> </plugin> </plugins>
步骤五:创建war包
在 E:\PROJECT3\springbootTest 目录下,shift+右键,“在此处打开命令窗口”
输入命令: mvn clean package,生产war文件。(该命令表示 删除target目录及内容后重新打包)
这样就在 target 目录下 生成了一个 springbootTest-0.0.1-SNAPSHOT.war 文件
步骤六:重命名war包 , 然后部署
如果用 springboot-0.0.1-SNAPSHOT.war 这个文件名部署,那么访问的时候就要在路径上加上springbootTest-0.0.1-SNAPSHOT。 所以把这个文件重命名为 ROOT.war
然后把它放进tomcat 的webapps目录下。
注:ROOT.war 并不是指访问的时候要使用 /ROOT/hello ,而是直接使用/hello 进行访问,ROOT表示根路径。
步骤七:启动并测试
运行tomcat下的 bin目录里的startup.bat(Windows下的tomcat), 然后就可以启动了. 启动后访问如下地址测试:
http://127.0.0.1:8080/hello
注意点:tomcat启动失败
...... 严重: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NoSuchMethodError: org.apache.tomcat.util.res.StringManager.getManager(Ljava/lang/Class;)Lorg/apache/tomcat/util/res/StringManager; at org.apache.tomcat.websocket.WsWebSocketContainer.<clinit>(WsWebSocketContainer.java:79) at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131) at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5506) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 10 more 七月 24, 2018 3:00:16 下午 org.apache.catalina.startup.HostConfig deployWAR 严重: Error deploying web application archive D:\tomcat7-8080-eclipse\webapps\ROOT.war java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]] at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:905) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) 七月 24, 2018 3:00:16 下午 org.apache.catalina.startup.HostConfig deployWAR 信息: Deployment of web application archive D:\tomcat7-8080-eclipse\webapps\ROOT.war has finished in 2,494 ms 七月 24, 2018 3:00:16 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-bio-8080"] 七月 24, 2018 3:00:16 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-bio-8089"] 七月 24, 2018 3:00:16 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 2571 ms
该错是由于application启动类中嵌入的tomcat与部署war的tomcat冲突引起的,需要在pom.xml添加配置,来避免冲突!
在pom.xml添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
然后,重复步骤四、五、六。