[英]@PropertySource() is not working in bean defined in XML imported by @ImportResource()
Can not find correct answer to next situation in Internet. 在Internet中找不到对下一种情况的正确答案。 So, if it was already discussed somewher, just point out.
因此,如果已经进行了讨论,请指出。
I jave UnitTest that is run by Spring: 我喜欢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
contains some beans that use @PropertySource
. spring/app-core-config.xml
包含一些使用@PropertySource
bean。
For example, Service1Impl
: 例如,
Service1Impl
:
@Service
@PropertySource("classpath:system/service1.properties")
public class Service1Impl {
@Value("${event.ack.warning}")
private String eventAckWarningComm;
@Value("${event.ack.info}")
private String eventAckInfoComm;
}
So, few classes with similar usage of @PropertySource
are defined in spring/app-core-config.xml
. 因此,在
spring/app-core-config.xml
很少定义使用类似@PropertySource
类。
When I run mentioned UnitTest all works fine. 当我提到UnitTest时,一切正常。
But I need some additional Java configuration for specific UnitTest. 但是我需要一些用于特定UnitTest的其他Java配置。 So, I written next simple configuration for previous UnitTest:
因此,我为先前的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(){
...
}
}
But when I run this UnitTest, I get next exception: 但是,当我运行此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
Could someone explain me why @PropertySource
is not working in bean defined in XML imported by @ImportResource
? 有人可以解释一下为什么
@PropertySource
在@ImportResource
导入的XML中定义的bean中@ImportResource
吗?
After you have demarcated your class with respective propertysource you need to declare static propertySourcesPlaceHolderConfigurer bean like below in your configuration class 在用各自的propertysource划分了类之后,需要在配置类中声明如下所示的static propertySourcesPlaceHolderConfigurer bean
@Bean
public static PropertySourcesPlaceholderConfigurer xxxpropertyConfig() {
return new PropertySourcesPlaceholderConfigurer();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.