java Spring不加载属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30549350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring not loading properties
提问by serverfaces
Spring not loading properties when applicationContext.xml is referenced in a @Configuration class
在 @Configuration 类中引用 applicationContext.xml 时,Spring 不会加载属性
@Configuration
@ImportResource("classpath:applicationContext.xml")
public class SpringConfig {
@Autowired
private MyBean1 myBean1;
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean(name = "myBean2")
public MyBean2 myBean2() {
MyBean2 bean2 = new MyBean2();
return bean2;
}
}
applicationContext.xml:
应用上下文.xml:
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:test.properties</value>
</property>
</bean>
<bean id="myBean1" class="com.foo.bar.MyBean1">
<property name="name" value="${name}" />
<property name="age" value="${age}" />
</bean>
Test.properties:(in the classpath)
Test.properties:(在类路径中)
name=foo
age=10
When I load the applicationContext.xml file using the following lines of code, it worked:
当我使用以下代码行加载 applicationContext.xml 文件时,它起作用了:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
I see the following lines printed:
我看到打印了以下几行:
2015-05-30 10:01:08.277 [INFO] org.springframework.beans.factory.config.PropertyPlaceholderConfigurer:172 - Loading properties file from class path resource [test.properties]
2015-05-30 10:01:08.292 [INFO] org.springframework.beans.factory.support.DefaultListableBeanFactory:596 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5c001eb0: defining beans [placeholderConfig,myBean1]; root of factory hierarchy
I have no issues loading the test.properties file with "myBean1" getting the value of ${name} and {age} from the properties file.
我在加载 test.properties 文件时没有问题,“myBean1”从属性文件中获取 ${name} 和 {age} 的值。
The issue I have is when I try to load SpringConfig:
我遇到的问题是当我尝试加载 SpringConfig 时:
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
The SpringConfig class refers to applicationContext.xml as: @ImportResource("classpath:applicationContext.xml")
SpringConfig 类将 applicationContext.xml 引用为:@ImportResource("classpath:applicationContext.xml")
The error I am seeing is:
我看到的错误是:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'myBean1' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'name' in string value "${name}"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
In a nutshell, when I load applicationContext using ClassPathXmlApplicationContext, then I have no issue:
简而言之,当我使用 ClassPathXmlApplicationContext 加载 applicationContext 时,我没有问题:
Working:
在职的:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
But when it is referred by SpringConfig, then I see that applicationContext does not see my properties file.
但是当它被 SpringConfig 引用时,我看到 applicationContext 没有看到我的属性文件。
Not Working:
不工作:
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Please note that test.properties is in my classpath and it is nothing to do with the properties file missing or something.
请注意 test.properties 在我的类路径中,它与属性文件丢失或其他无关。
I don't know if I have to use a different approach for AnnotationConfigApplicationContext as I need myBean1 to be auto-wired into SpringConfig and myBean1 uses some properties from the test.properties file.
我不知道是否必须对 AnnotationConfigApplicationContext 使用不同的方法,因为我需要将 myBean1 自动连接到 SpringConfig 中,而 myBean1 使用 test.properties 文件中的一些属性。
回答by Sotirios Delimanolis
When using
使用时
new AnnotationConfigApplicationContext(SpringConfig.class);
you're registering two PlaceholderConfigurerSupport
beans that are used to resolve properties. One through Java
您正在注册两个PlaceholderConfigurerSupport
用于解析属性的 bean。一通Java
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
and one through the imported XML
和一个通过导入的 XML
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:test.properties</value>
</property>
</bean>
The PropertySourcesPlaceholderConfigurer
bean is fail-fast when it comes to resolving properties. That is, if it doesn't find a match, it will throw an exception (actually the nested class that does the resolution will do that).
在PropertySourcesPlaceholderConfigurer
解析属性时,bean 是快速失败的。也就是说,如果它没有找到匹配项,它将抛出异常(实际上,执行解析的嵌套类会这样做)。
Since your PropertySourcesPlaceholderConfigurer
hasn't been initialized with any locations
, it doesn't have any sources from where to resolve ${name}
or ${age}
(it has some sources, but not your test.properties
file).
由于您PropertySourcesPlaceholderConfigurer
尚未使用 any 进行初始化locations
,因此它没有任何来源可以解析${name}
或${age}
(它有一些来源,但不是您的test.properties
文件)。
Either set up your bean correctly to find your properties file
正确设置您的 bean 以找到您的属性文件
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("test.properties"));
return propertySourcesPlaceholderConfigurer;
}
or remove it entirely. If you do remove it, Spring will use the correctly configured PropertyPlaceholderConfigurer
bean from XML.
或完全删除它。如果确实删除了它,Spring 将使用PropertyPlaceholderConfigurer
XML 中正确配置的bean。