2014-09-09 2 views
2

@Profile 기능을 사용하여 프로덕션/dev 환경 구성과 '테스트'구성을 분리하려고합니다. 내 구성 클래스에 @profile를 추가 할 때 내가 얻을 :@Profile 원인 EmbeddedWebApplicationContext를 시작할 수 없습니다

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-production.properties 생산에 활성화 된 프로파일을 설정하려고

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

또는 cmd를 명령 mvn spring-boot:run -Dspring.profiles.active=production

아무것도 할 수 없습니다. 물론 @Profile을 제거하면 모든 것이 작동하지만 테스트에서는 프로덕션 데이터베이스를 사용하고 있습니다.)

답변

2

프로필을 추가하면 기본 진입 점에 @Profile이라는 주석이 달려 있기 때문에 전체 응용 프로그램이 기본적으로 작동을 멈 춥니 다.

스프링 부트가 스프링 부트를 중심으로 열심히 노력하고 있고, 너무 복잡해 보이는 것처럼 보이는 순간에 스프링 부트로 하여금 작업을하게 할 것을 제안합니다.

스프링 부트는 스프링 데이터 JPA와 클래스 경로에 스프링 웹이 있다는 사실을 자동으로 감지합니다. 따라서 @EnableJpaRepositories@EnableWebMvc을 삭제하고 수업을 WebMvcConfigurerAdapter까지 확장하지 마십시오.

기본적으로 스프링 부트는 속성을 입력하는 대신 application.properties을로드합니다. 클래스 패스 또는 config의 루트에 배치하십시오. 최소한 Spring Boot가로드하기 때문에 @PropertySource을 제거하십시오. properties 경로를 유지하려면 spring.config.location 속성을 추가 한 다음 properties 디렉토리를 가리 킵니다.

마지막으로 파일의 이름을 PhoenixApplication으로 바꾸 겠지만 그저 저뿐입니다. 즉 이제 단순히 application.properties에서 생산 구성을 넣어

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

같은 것을 당신을두고 테스트 구성을 포함 src/test/resources 다른 1 점을 추가하는 듯합니다. 런타임시 첫 번째 옵션 만 사용할 수 있습니다.

프로필을 실제로 사용하려면 프로덕션 환경을 구성하고 테스트를 재정의하는 것이 좋습니다. 그런 다음 @ActiveProfiles을 테스트 케이스에 간단히 추가하십시오.

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

은 기본 application.properties를로드 테스트를 시작하고 application-test.properties는 당신은 단순히 src/test/resources에 배치 할 수 있습니다.

+0

답을위한 thx! 봄철 부츠가하는 일의 양은 '정상적인'봄 사용자에게는 약간 놀랍습니다.) 어쨌든 @EnableJpaRepositories를 사용해야했습니다. – freakman

+0

'PhoenixApplication'이 jpa 저장소 인터페이스가있는 패키지를 다루지 않는 한, 그렇게하지 않아도됩니다. 일반적으로 응용 프로그램 클래스를 상위 패키지에 넣습니다. (당신의 경우에는'mypackage') 그래서 모든 것을 가져올 빈'@ ComponentScan'을 사용할 수 있습니다. 이것은 또한 기본으로 구성된 EnableJpaRepositories에 의해 사용되는 기본입니다. –

+0

아아 네, PhoenixApplication 클래스는 mypackage.configuration에 있으며 저장소는 mypackage.repository에 있습니다. – freakman

관련 문제