【问题标题】:How can I avoid Spring Boot from loading EmbeddedWebApplicationContext?如何避免 Spring Boot 加载 EmbeddedWebApplicationContext?
【发布时间】:2015-03-23 09:35:53
【问题描述】:

我有一个带有 Camel-HTTP 的 Spring Boot 集成应用程序。由于 Camel-HTTP 依赖于 geronimo-servlet,因此 Spring Boot 正在尝试加载 Web 应用程序上下文。

如何强制 Spring 不加载 EmbeddedWebApplicationContext

我已尝试使用 @EnableAutoConfiguration(exclude = ...) 注释排除在 org.springframework.boot.autoconfigure.web 中找到的所有 AutoConfiguration 类。

【问题讨论】:

    标签: java spring spring-mvc apache-camel spring-boot


    【解决方案1】:

    您可以使用SpringApplicationBuilder 类显式禁用加载网络环境和上下文, 即在您的主要课程中:

    public static void main(String[] args) {
        new SpringApplicationBuilder(MainConfig.class).web(false).run(args);
    }
    

    【讨论】:

    • 成功了!我对SpringApplication而不是SprintApplicationBuilder做了同样的事情,但它正在检测servlet并创建web上下文。有了builder,它工作得很好。谢谢
    • 如果 javax.servlet.Servlet 或 org.springframework.web.context.ConfigurableWebApplicationContext 在类路径中,这将不起作用。最好调用 contextClass 方法来指定 AnnotationConfigApplicationContext。
    • @ConstantinoCronemberger web(false) 调用在 initialize(sources) 方法之后,该方法作为 SAB/SA 构造函数的一部分调用,所以不应该是这种情况?
    • 在我的例子中,webEnvironment 被 application.yml 类中的“web-environment”属性覆盖。就我而言,我确实有一个 Web 应用程序,但我想创建另一个入口点,以便能够在不启动 Tomcat 的情况下运行代码的特定部分。
    【解决方案2】:

    你可以尝试使用@ContextConfiguration注解:

    @ContextConfiguration(loader = SpringApplicationCtxtLoader.class, classes = annotatedClass.class)
    

    annotatedClass.classannotated 的类,例如:@Component@Service@Repository

    this 回答中,这是用于测试目的的建议方法,但我认为它可能会对您有所帮助

    【讨论】:

    • 这可能适用于测试,但不适用于 Spring Boot 应用程序本身
    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 2014-10-04
    • 1970-01-01
    • 2016-10-22
    • 2019-04-30
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多