【发布时间】:2017-05-24 22:08:25
【问题描述】:
很抱歉,如果您之前读过这篇文章,我会完全重写......
我将一个项目迁移到一个 Spring-Boot 项目并在 Tomcat 中执行集成测试,结果在它完成后运行。在 Eclipse 和 Maven 中运行它也是如此。 Maven 的缺点是构建过程停止,只有 Ctrl+C 有帮助,这实际上也停止了 Maven。
这是来自pom.xml的插件:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<finalName>at.a1.iap.spagat.aggregator</finalName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<!-- required to make four soapui-test-classes work -->
<forkMode>pertest</forkMode>
<includes>
<include>**/*ITest.java</include>
</includes>
<!-- no need to exclude and if you exclude no tests will be
run <excludes> <exclude>**/*Test.java</exclude> </excludes> -->
</configuration>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<mappings>
<mapping>
<namespace>http://www.agama.tv/ws/emp</namespace>
<targetPackage>at.a1.iap.spagat.aggregator.external.agama</targetPackage>
</mapping>
</mappings>
<sourceDirectory>${basedir}/src/main/resources/wsdl</sourceDirectory>
<outputDirectory>${basedir}/src/gen/java</outputDirectory>
<testCases>false</testCases>
<serverSide>true</serverSide>
<subPackageByFileName>false</subPackageByFileName>
</configuration>
<executions>
<execution>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
经过大量摆弄后,我注意到这两个 bean 导致了这个(实际上只是其中一个):
@Bean
public ServletRegistrationBean servletWs(ServletContext servletContext) {
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/ws/*");
servletRegistrationBean.setLoadOnStartup(1);
servletRegistrationBean.addInitParameter("dispatchOptionsRequest", "true");
servletRegistrationBean.setName("general-dispatcher");
return servletRegistrationBean;
}
@Bean
public ServletRegistrationBean servletUi(ServletContext servletContext) {
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/ui/*");
servletRegistrationBean.setLoadOnStartup(1);
servletRegistrationBean.setName("pagestuff-dispatcher");
FilterRegistration.Dynamic welcomeFilter = servletContext.addFilter("WelcomeFilter", new WelcomeFilter("./ui/index"));
welcomeFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/");
return servletRegistrationBean;
}
魔法线是
servletRegistrationBean.setLoadOnStartup(1);
如果我将其注释掉,Tomcat 会按预期停止。那么为什么我不应该这样做,我应该怎么做呢?
如果您需要更多代码摘录或其他信息,请随时提出。
这是带有嵌入式 Tomcat 的 Spring-Boot 1.4.1
【问题讨论】:
标签: tomcat spring-boot integration-testing