ser*_*ces 2 java spring properties-file
在@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;
}
}
Run Code Online (Sandbox Code Playgroud)
applicationContext.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>
Run Code Online (Sandbox Code Playgroud)
Test.properties :(在类路径中)
name=foo
age=10
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码行加载applicationContext.xml文件时,它工作正常:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Run Code Online (Sandbox Code Playgroud)
我看到印有以下几行:
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
Run Code Online (Sandbox Code Playgroud)
我没有问题加载test.properties文件,其中"myBean1"从属性文件中获取$ {name}和{age}的值.
我遇到的问题是当我尝试加载SpringConfig时:
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Run Code Online (Sandbox Code Playgroud)
SpringConfig类将applicationContext.xml称为:@ImportResource("classpath:applicationContext.xml")
我看到的错误是:
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)
Run Code Online (Sandbox Code Playgroud)
简而言之,当我使用ClassPathXmlApplicationContext加载applicationContext时,我没有问题:
工作:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
但是当它被SpringConfig引用时,我看到applicationContext没有看到我的属性文件.
不工作:
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
请注意,test.properties位于我的类路径中,它与缺少的属性文件或其他内容无关.
我不知道是否必须对AnnotationConfigApplicationContext使用不同的方法,因为我需要将myBean1自动连接到SpringConfig,而myBean1使用test.properties文件中的一些属性.
使用时
new AnnotationConfigApplicationContext(SpringConfig.class);
Run Code Online (Sandbox Code Playgroud)
您正在注册两个PlaceholderConfigurerSupport用于解析属性的bean.一个通过Java
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)
和一个通过导入的XML
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:test.properties</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
在PropertySourcesPlaceholderConfigurer解析属性时,bean是快速失败的.也就是说,如果它没有找到匹配项,它将抛出异常(实际上,执行解析的嵌套类将执行此操作).
由于您PropertySourcesPlaceholderConfigurer尚未使用any进行初始化locations,因此它没有任何来源可以解决${name}或${age}(它有一些来源,但不是您的test.properties文件).
正确设置bean以查找属性文件
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("test.properties"));
return propertySourcesPlaceholderConfigurer;
}
Run Code Online (Sandbox Code Playgroud)
或完全删除它.如果删除它,Spring将使用PropertyPlaceholderConfigurerXML中正确配置的bean.
| 归档时间: |
|
| 查看次数: |
11320 次 |
| 最近记录: |