简体   繁体   English

强制Spring Boot不使用EmbeddedWebApplicationContext?

[英]Forcing Spring Boot to NOT use EmbeddedWebApplicationContext?

I have a Spock test case where I want to load a Spring application. 我有一个Spock测试用例,我想在其中加载Spring应用程序。 I have a very rudimentary Groovy class that is my config object: 我有一个非常基本的Groovy类,它是我的配置对象:

@Configuration
@EnableAutoConfiguration
class TestConfig {
  @Bean
  Map createSillyMsgMap() {
    ["sillyMsg" : "This is a silly message"]
  }
  public static void main(String[] args) {
    println "TestConfig.main(${args})"
  }
}

Obviously, this is isn't very useful, but it serves as an example. 显然,这不是很有用,但仅作为示例。 For convenience, I've also added the main method in that class. 为了方便起见,我还在该类中添加了main方法。 My test class just tries to instantiate this as a Spring Boot Application. 我的测试类只是尝试将其实例化为Spring Boot Application。 Something like this: 像这样:

class AppContextSpec extends Specification {
  def "testSpringApplication"() {
    given: "a Spring Configuration"
    SpringApplication app = new SpringApplication(TestConfig)
    app.headless = true 
    app.webEnvironment = false
    app.applicationContextClass = AnnotationConfigApplicationContext

    expect: "the Spring Application to be able to start"
    ConfigurableApplicationContext ctxt = app.run((String[]) ["blah"])
  }
}

I'm trying to force Spring Boot to NOT use the EmbeddedWebApplicationContext by explicitly setting the webEnvironment property to false. 我试图通过将webEnvironment属性显式设置为false来强制Spring Boot不使用EmbeddedWebApplicationContext。 But no matter what I do, Spring Boot insists on starting a Tomcat server, and it does seem to pull in other resources in the source tree that are marked with @Component and/or @Configuration. 但是,不管我做什么,Spring Boot坚持要启动Tomcat服务器,并且它的确确实在源树中引入了其他带有@Component和/或@Configuration标记的资源。 There are several other application contexts on the classpath, and certainly jar files that imply web service kind of stuff, but I'm very surprised that what's on the classpath should take precedence over explicit configuration through the webEnvironment property. 类路径上还有其他一些应用程序上下文,当然也有暗示Web服务的jar文件,但是让我感到惊讶的是,类路径上的内容优先于通过webEnvironment属性进行的显式配置。 I'm using Gradle 1.12 as the build system, and the Spring Boot version is 1.1.4, and I'm using Groovy 2.3.6 with Spock 0.7-groovy-2.0. 我正在使用Gradle 1.12作为构建系统,并且Spring Boot版本是1.1.4,并且正在将Groovy 2.3.6与Spock 0.7-groovy-2.0一起使用。 Any help with this is appreciated. 任何帮助,不胜感激。

Am I doing something completely out of the norm here? 我在这里做的事情完全不合常理吗? Thanks. 谢谢。

In case where you are not writing test and therefore you can't use @ContextConfiguration 如果您不编写测试,因此不能使用@ContextConfiguration

Instead of simple SpringApplication.run(MyDomainSpringConfig.class, args); 而不是简单的SpringApplication.run(MyDomainSpringConfig.class, args);

Construct and run your SpringApplication like this... 像这样构造并运行您的SpringApplication ...

SpringApplication application = new SpringApplication(MyDomainSpringConfig.class);
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
application.run(args);

Verified in Spring Boot 1.2.1 在Spring Boot 1.2.1中验证

You probably shouldn't be trying to bootstrap the Spring Boot application context on your own like you are doing. 您可能不应该像自己那样尝试自行引导Spring Boot应用程序上下文。

The following code creates an integration testing environment without starting the embedded servlet container (although if you want to start the embedded servlet container it would be very easy to do with a couple more annotations): 以下代码在不启动嵌入式servlet容器的情况下创建了一个集成测试环境(尽管如果要启动嵌入式servlet容器,使用另外两个批注非常容易):

@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = TestConfig.class)
class ExampleSpec extends Specification {

    // ...

}

You'll also have to have the 'org.spockframework:spock-spring:0.7-groovy-2.0' dependency present on your test classpath 您还必须在测试类路径上存在'org.spockframework:spock-spring:0.7-groovy-2.0'依赖项

Check out this part of the Spring Boot official documentation and this SO question 查看Spring Boot官方文档的这一部分以及这个 SO问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 强制Spring Boot 2使用JDK代理失败 - Forcing Spring Boot 2 to use JDK Proxy failed Spring Boot:由于缺少 EmbeddedServletContainerFactory bean 无法启动 EmbeddedWebApplicationContext - Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean 如何避免Spring Boot加载EmbeddedWebApplicationContext? - How can I avoid Spring Boot from loading EmbeddedWebApplicationContext? 春季启动应用程序:ApplicationContextException:无法启动EmbeddedWebApplicationContext - spring-boot application: ApplicationContextException: Unable to start EmbeddedWebApplicationContext 由于缺少 EmbeddedServletContainerFactory bean,Spring Boot 无法启动 EmbeddedWebApplicationContext - Spring Boot Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean Spring boot -- 由于缺少 EmbeddedServletContainerFactory bean 无法启动 EmbeddedWebApplicationContext - Spring boot -- Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean 带Web套接字的Spring Boot微服务:由于缺少EmbeddedServletContainerFactory bean而无法启动EmbeddedWebApplicationContext - Spring Boot Microservice w/ Web Sockets: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean Spring Boot 强制无头模式 - Spring boot forcing headless mode 强制maven spring boot项目使用旧版本的依赖项而不是其他依赖项的新版本 - Forcing maven spring boot project to use older version of a dependency instead of a new version from another dependency 使用 Spring 云值时 EmbeddedWebApplicationContext 的 ClassNotFoundException - ClassNotFoundException for EmbeddedWebApplicationContext when Spring cloud valut is used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM