spring-boot默認(rèn)提供內(nèi)嵌的tomcat,所以打包直接生成jar包盖袭,用java -jar命令就可以啟動(dòng)。但是遥金,有時(shí)候我們更希望一個(gè)tomcat來(lái)管理多個(gè)項(xiàng)目,這種情況下就需要項(xiàng)目是war格式的包而不是jar格式的包蒜田。spring-boot同樣提供了解決方案稿械,只需要簡(jiǎn)單的幾步更改就可以了,這里提供maven項(xiàng)目的解決方法:
1.將項(xiàng)目的啟動(dòng)類(lèi)Application.java繼承SpringBootServletInitializer并重寫(xiě)configure方法
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
2.在pom.xml文件中物邑,project下面增加package標(biāo)簽
<packaging>war</packaging>
3.還是在pom.xml文件中溜哮,dependencies下面添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
這樣,只需要以上3步就可以打包成war包色解,并且部署到tomcat中了茂嗓。需要注意的是這樣部署的request url需要在端口后加上項(xiàng)目的名字才能正常訪問(wèn)。spring-boot更加強(qiáng)大的一點(diǎn)就是:即便項(xiàng)目是以上配置科阎,依然可以用內(nèi)嵌的tomcat來(lái)調(diào)試述吸,啟動(dòng)命令和以前沒(méi)變,還是:mvn spring-boot:run锣笨。
如果需要在springboot中加上request前綴蝌矛,需要在application.properties中添加server.contextPath=/prefix/即可。其中prefix為前綴名错英。這個(gè)前綴會(huì)在war包中失效入撒,取而代之的是war包名稱(chēng),如果war包名稱(chēng)和prefix相同的話椭岩,那么調(diào)試環(huán)境和正式部署環(huán)境就是一個(gè)request地址了茅逮。
到這一步都參考:http://blog.csdn.net/james_wade63/article/details/51009423
一般這樣配置都不會(huì)出現(xiàn)問(wèn)題,但有些時(shí)候判哥,還是會(huì)報(bào)錯(cuò)献雅,那是因?yàn)閠omcat運(yùn)行版本不同:
例如我在tomcat8.0.30版本下,配置如下:
<!-- 打war包時(shí)加入此項(xiàng)塌计, 告訴spring-boot tomcat相關(guān)jar包用外部的挺身,不要打進(jìn)去 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.4.0.RELEASE</version>
<scope>provided</scope>
</dependency>
這樣配置在外部tomcat是8.0.30版本下正常運(yùn)行,但是我的服務(wù)器是7.0.69就會(huì)報(bào)錯(cuò):
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/zhaolixiang]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:4088)
at org.apache.catalina.startup.HostConfig.reload(HostConfig.java:1539)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1512)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1748)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:333)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1371)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1543)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1553)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1521)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at org.springframework.boot.context.web.SpringBootServletInitializer.createSpringApplicationBuilder(SpringBootServletInitializer.java:140)
at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:104)
at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:85)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5573)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 12 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
... 18 more
解決版本锌仅,修改代碼:
<!-- 打war包時(shí)加入此項(xiàng)章钾, 告訴spring-boot tomcat相關(guān)jar包用外部的,不要打進(jìn)去 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.3.0.RELEASE</version>
<scope>provided</scope>
</dependency>
就成功了H惹邸伍玖!