@PropertySource()在由@ImportResource()导入的XML中定义的bean中不起作用

时间:2016-07-25 19:12:04

标签: java spring spring-java-config

无法找到互联网下一个情况的正确答案。所以,如果已经讨论了一些,请指出。

我弹出由Spring运行的UnitTest:

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

spring/app-core-config.xml包含一些使用@PropertySource的bean。

例如,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配置。 所以,我为之前的UnitTest写了下一个简单的配置:

@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(){
    ...
  }
}

但是当我运行这个UnitTest时,我得到了下一个异常:

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

有人可以解释为什么@PropertySource无法在由@ImportResource导入的XML中定义的bean中工作吗?

1 个答案:

答案 0 :(得分:0)

在使用各自的propertysource划分您的类后,您需要在配置类中声明静态propertySourcesPlaceHolderConfigurer bean,如下所示

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