@Profile cause Unable to start EmbeddedWebApplicationContext

…衆ロ難τιáo~ 提交于 2019-12-11 04:29:39

问题


im trying to use @Profile functionality to separate production/dev environment configuration and 'tests' config. But when I add @Profile to my configuration class I get:

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 class looks like this:

@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);
}
}

ive tried to set active profile to production in application-production.properties

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

or cmd command: mvn spring-boot:run -Dspring.profiles.active=production

nothing helps. Ofcourse everything works when I remove @Profile, but then I my tests are using production database ; )


回答1:


If you add the profile your whole application basically stops working because your main entry point is annotated with @Profile.

I suggest you let Spring Boot do its work at the moment it appears as if you are trying to work very hard around Spring Boot and you are making things, imho, too complex.

Spring Boot will autodetect Spring Data JPA and the fact that you have Spring Web on your classpath. So remove @EnableJpaRepositories and @EnableWebMvc and don't let your class extends WebMvcConfigurerAdapter.

Spring boot by default will load the application.properties for you instead of putting in in properties either place it in the root of your classpath or config. At least remove the @PropertySource as Spring Boot will just load it. If you want to keep the properties path add the spring.config.location property which then points to your properties directory.

Finally I would probably also rename the file to PhoenixApplication but that is just me. That should leave you with something like

@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);
    }
}

Now simply put your production configuration in the application.properties and put another one in src/test/resources to contain your test configuration. At runtime only the first will be available when testing the latter will override properties from the first.

If you really want to use profiles I would suggest doing it the other way around, configure for production and override for test. Then simply add @ActiveProfiles to your test case.

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

This will start a test which will load the default application.properties and a application-test.properties which you can simply place in src/test/resources.



来源:https://stackoverflow.com/questions/25746080/profile-cause-unable-to-start-embeddedwebapplicationcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!