接SpringBoot 快速入門(Eclipse)java
步驟一:部署方式web
Springboot 和 web 應用程序不同,其本質上是一個 Java 應用程序,那麼又如何部署呢?
一般來講,Springboot 部署會採用兩種方式:所有打包成一個jar,或者打包成一個war。
下面是war的方式。spring
步驟二:可運行項目apache
該項目是springboot入門的小demotomcat
步驟三:修改 Application類springboot
application修改以下代碼websocket
新加@ServletComponentScan註解,而且繼承SpringBootServletInitializer 。(規定這麼改)app
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文件eclipse
pom.xml修改成以下代碼,主要兩個改動
新加打包成war的聲明: <packaging>war</packaging>webapp
<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>
而後,重複步驟4、5、六。