成功运行Spring getting started guide for WebSocket后,我非常希望将此应用程序构建为WAR并将其部署到Tomcat中。我已尽力通过遵循Spring getting started guide for converting a Spring Boot JAR to a WAR来修改项目以构建为WAR文件,但是当我部署生成的WAR时,Tomcat会抛出异常。
我正在使用带有Gradle 1.10的STS 3.4.0进行开发(在以前在Eclipse中使用Maven构建时无法成功部署之后我切换到了这个)。 Gradle报告成功构建的WAR文件,但是当我尝试将其部署到Tomcat时,会抛出以下异常:
09-Feb-2014 17:30:27.537 SEVERE [localhost-startStop-3] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/gs-messaging-stomp-websocket-0.1.0]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:699)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1763)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/websocket/WebSocketAutoConfiguration$TomcatWebSocketConfiguration.class]: Initialization of bean failed; nested exception is java.lang.ArrayStoreException: org.apache.catalina.valves.RemoteIpValve
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:658)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:355)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:129)
at org.springframework.boot.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:90)
at org.springframework.boot.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:53)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:181)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5237)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
Caused by: java.lang.ArrayStoreException: org.apache.catalina.valves.RemoteIpValve
at org.springframework.boot.autoconfigure.web.ServerProperties$Tomcat.customizeTomcat(ServerProperties.java:212)
at org.springframework.boot.autoconfigure.web.ServerProperties.customize(ServerProperties.java:117)
at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:68)
at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:54)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 27 more
我在Windows 8.1 64位上运行Tomcat 8.0.0-RC10。我相信相关的应用程序文件是:
Application.java:
包你好;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
HelloWebXml.java(代替实际的web.xml):
package hello;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.SpringBootServletInitializer;
public class HelloWebXml extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
最后,build.gradle:
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC1")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
war {
baseName = 'gs-messaging-stomp-websocket'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT")
compile("org.springframework.boot:spring-boot-starter-websocket:0.5.0.BUILD-SNAPSHOT")
compile("org.springframework:spring-messaging:4.0.0.RELEASE")
compile("com.fasterxml.jackson.core:jackson-databind")
testCompile("junit:junit:4.11")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.10'
}
我必须说,我发现这个问题似乎与tomcatEmbeddedServletContainerFactory有关,这让我感到讽刺而且有点令人沮丧。因此,一个功能的唯一目的(我相信)是允许应用程序在servlet容器的外部运行---我完全没有兴趣的功能 - 阻止我将应用程序正确部署到servlet容器中
所有帮助将不胜感激。
答案 0 :(得分:3)
在Syer先生的回答的帮助下(抱歉,我试图提升,但我对这个论坛太新了),以及this post关于如何将资源标记为“已提供”的信息,我终于把这个建筑作为一场战争,我可以在Tomcat成功部署。
经过一些实验后,似乎需要对build.gradle进行以下更改:
configurations {
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
eclipse.classpath.plusConfigurations += configurations.provided
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT") {
exclude module: 'spring-boot-starter-tomcat'
}
compile("org.springframework.boot:spring-boot-starter-websocket:0.5.0.BUILD-SNAPSHOT") {
exclude module: 'spring-boot-starter-tomcat'
}
provided group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '0.5.0.BUILD-SNAPSHOT'
compile("org.springframework:spring-messaging:4.0.0.RELEASE")
compile("com.fasterxml.jackson.core:jackson-databind")
testCompile("junit:junit:4.11")
}
当我仅应用'排除模块'更改时,我得到一个编译时错误,抱怨“找不到javax.servlet.ServletException的类文件”---这让我很困惑,因为我认为这是我的一部分Java 7 JDK。
当我仅应用“提供的组”更改时,它实际上并没有从我的战争中删除spring-boot-starter-tomcat jar。
答案 1 :(得分:1)
你需要“提供”嵌入式tomcat依赖项(现在你在“编译”配置中有spring-boot-starter-tomcat,传递给spring-boot-starter-web)。如果您对嵌入式服务器没有用处,可以将其完全删除。 Gradle有标准的习惯用法,用于排除传递依赖关系,并且在不同的配置中添加它们,我相信。