spring - @Profile 导致无法启动 EmbeddedWebApplicationContext

标签 spring spring-boot spring-java-config

我尝试使用@Profile功能来分离生产/开发环境配置和“测试”配置。但是当我将 @Profile 添加到我的配置类时,我得到:

Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:124)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at mypackage.configuration.PhoenixConfiguration.main(PhoenixConfiguration.java:26)
Caused by: org.springframework.context.ApplicationContextException: Unable to start     EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:174)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:147)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:121)
    ... 7 more

配置类如下所示:

@Configuration
@EnableAutoConfiguration
@ComponentScan("mypackage")
@EnableJpaRepositories(basePackages = "mypackage.repository")
@EntityScan(basePackages = "mypackage.phoenix.domain")
@PropertySource("classpath:properties/application-production.properties")
@EnableWebMvc
@Profile("production")
public class PhoenixConfiguration extends WebMvcConfigurerAdapter{

public static void main(String[] args) throws Exception {
    SpringApplication.run(PhoenixConfiguration.class, args);
}
}

我尝试在 application-development.properties 中将事件配置文件设置为生产

spring.profiles.active=production (with and without " )

或cmd命令:mvn spring-boot:run -Dspring.profiles.active=product

没有任何帮助。当然,当我删除 @Profile 时,一切正常,但随后我的测试使用的是生产数据库; )

最佳答案

如果您添加配置文件,您的整个应用程序基本上会停止工作,因为您的主入口点是用 @Profile 注释的。

我建议你让 Spring Boot 来完成它的工作,此时看起来你正在努力围绕 Spring Boot 工作,但恕我直言,你正在让事情变得太复杂。

Spring Boot 将自动检测 Spring Data JPA 以及您的类路径上有 Spring Web 的事实。因此,请删除 @EnableJpaRepositories@EnableWebMvc,并且不要让您的类扩展 WebMvcConfigurerAdapter

Spring boot 默认情况下会为您加载 application.properties,而不是放入属性中,或者将其放置在类路径或配置的根目录中。至少删除 @PropertySource 因为 Spring Boot 只会加载它。如果您想保留 properties 路径,请添加 spring.config.location 属性,该属性随后指向您的 properties 目录。

最后,我可能还会将该文件重命名为 PhoenixApplication 但这只是我的想法。这应该会给你留下类似的东西

@Configuration
@EnableAutoConfiguration
@ComponentScan("mypackage")
@EntityScan(basePackages = "mypackage.phoenix.domain")
public class PhoenixApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(PhoenixApplication.class, args);
    }
}

现在只需将您的生产配置放入 application.properties 中,并将另一个配置放入 src/test/resources 中以包含您的测试配置。在运行时,只有第一个在测试时可用,后者将覆盖第一个的属性。

如果您确实想使用配置文件,我建议您以相反的方式进行操作,为生产进行配置并为测试覆盖。然后只需将 @ActiveProfiles 添加到您的测试用例中即可。

@ActiveProfiles("test")
@SpringApplicationConfiguration(classes=PhoenixApplication.class)
public class YourTest {}

这将启动一个测试,该测试将加载默认的 application.propertiesapplication-test.properties,您可以将其简单地放置在 src/test/中资源

关于spring - @Profile 导致无法启动 EmbeddedWebApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25746080/

相关文章:

java - 使用显示错误的 hibernate 更新 mysql 表

spring-integration-kafka 配置消费者从指定分区接收消息

spring-boot - 我如何配置gradle以启动和停止在构建完成后仍然有效的Spring Boot微服务?

java - Spring boot中 Autowiring 接口(interface)错误

java - 使用 Java Config 时的 Spring Batch 表前缀

java - Spring JavaConfig配置异常: A ServletContext is required to configure default servlet handling

javascript - Spring MVC : Need elegant solution to handle escaping and unescaping of strings passed into Front End

java - 如何在 Swagger-UI "Value"字段中仅显示必需的参数

java - Spring Boot HTTPServletRequest 无法通过测试正确使用

java - 将原型(prototype)注入(inject)单例(java配置+注释)