2016-07-25 1 views
0

인터넷의 다음 상황에 대한 정답을 찾을 수 없습니다. 그래서 이미 논의 된 적이 있다면 그냥 지적하십시오.@PropertySource()가 @ImportResource()에서 가져온 XML에 정의 된 bean에서 작동하지 않습니다.

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:spring/app-core-config.xml") 
public class ConcurrentProcessingTest extends AbstractLSTest { 
    public void testMethod1(){ 
    ... 
    } 
} 

spring/app-core-config.xml@PropertySource을 사용하는 일부 콩이 포함

나는 봄에 의해 실행되는 유닛 테스트를 jave. 예를 들어

, Service1Impl은 :

@Service  
@PropertySource("classpath:system/service1.properties") 
public class Service1Impl { 
    @Value("${event.ack.warning}") 
    private String eventAckWarningComm; 

    @Value("${event.ack.info}") 
    private String eventAckInfoComm; 
} 

그래서, @PropertySource 유사한 사용과 몇 가지 클래스는 spring/app-core-config.xml에 정의되어 있습니다.

위에서 언급 한 UnitTest를 실행할 때 모두 정상적으로 작동합니다.

그러나 특정 UnitTest에 대한 추가 Java 구성이 필요합니다. 그래서, 내가 쓴 다음 간단한 이전 유닛 테스트에 대한 구성 : @PropertySource은 콩에 정의 된 작동하지 않는 이유

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'event.ack.warning' in string value "${event.ack.warning}" 
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) 
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) 
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:194) 
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:158) 
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:800) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:871) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 
    ... 184 more 

누군가가 나를 설명 할 수 없습니다 :이 유닛 테스트를 실행할 때

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {ConcurrentProcessingTest.AppCoreConfiguration.class}) 
public class ConcurrentProcessingTest extends AbstractLSTest { 

    @Configuration 
    @ImportResource("classpath:spring/app-core-config.xml") 
    //@PropertySource("classpath:system/service1.properties") -- if uncommented, UT works 
    //But it is annoying to add all propery-files here 
    static class AppCoreConfiguration { 
    //Here I want to add extra configuration in Java style 
    } 

    public void testMethod1(){ 
    ... 
    } 
} 

그러나, 나는 다음 예외가 @ImportResource으로 가져온 XML에서? 당신은 각각의 propertysource으로 클래스를 경계가 후

+0

'에서 다음과 같은 정적 propertySourcesPlaceHolderConfigurer 빈을 선언 할 필요가 다른 콩. –

+0

@M Hm ....하지만 순수 XML 구성에서는 @PropertySource가 Beans (Services)에 잘 작동합니다. @ImportResource() 관련 기능에 버그가 있습니까? –

+0

'@Configuration' 클래스 이외에'@ PropertySource'를 정의하고 읽는 것을 고려할 때'PropertyClassParser'에서'PropertySource'가 처리되기 때문에 다른 유형의 빈에 대해서도 그렇지 않습니다. 버그 야. '@ PropertySource'를 제외한 다른 것은'@Configuration' 클래스가 구성 관련이면서 서비스 나 다른 것과 관련이 없기 때문에 이해가되지 않습니다. –

답변

0

은 만 Configuration` 어떤 임의하지 @ '로 주석 콩에서 작동 구성 클래스 PropertySource` @

@Bean 
    public static PropertySourcesPlaceholderConfigurer xxxpropertyConfig() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    }