简体   繁体   English

Spring PropertyPlaceholderConfigurer不注入值

[英]Spring PropertyPlaceholderConfigurer doesn't inject values

I've already spent several hours googling but still can do nothing. 我已经花了几个小时进行谷歌搜索,但仍然无能为力。

Using debugger, I can find out that user and password in my datasource are not replaced with values from properties file, but parsed as is like ${jdbc.user} and ${jdbc.password} respectively. 使用调试器,我可以发现数据源中的用户和密码未替换为属性文件中的值,而是分别按${jdbc.user}${jdbc.password}进行解析。

What am I doing wrong? 我究竟做错了什么?

This is jdbc.properties file (located at src/main/resources): 这是jdbc.properties文件(位于src / main / resources):

jdbc.username=user
jdbc.password=password

And here's spring configuration xml: 这是spring配置xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>jdbc.properties</value>
    </property>
</bean>

... ...

And that's dependency from pom.xml: 这是来自pom.xml的依赖项:

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.5.RELEASE</version>
</dependency>

The problem is the way you launch your application. 问题是您启动应用程序的方式。

XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("Parent.xml",SomeClass.class)); 
final XmlBeanFactory springBeanFactory = new XmlBeanFactory(new ClassPathResource(getClass().getSimpleName() + ".xml", getClass()), parent); 

Don't use a BeanFactory use an ApplicationContext . 不要使用BeanFactory使用ApplicationContext

String context = getClass().getSimpleName() + ".xml";
ApplicationContext parent = new ClassPathXmlApplicationContext("Parent.xml");
ApplicationContext child = new ClassPathXmlApplicationContext(new String[] {context}, parent);

A BeanFactory is just a factory for beans, nothing more nothing less. BeanFactory只是bean的工厂,仅此而已。 The ApplicationContext is a BeanFactory on steroids. ApplicationContext是类固醇上的BeanFactory It has a complete lifecycle and has special types of beans that are called during that lifeccycle. 它具有完整的生命周期,并具有在该生命周期中调用的特殊类型的bean。

Check this section of the reference guide. 检查参考指南的这一部分

Another tip, use the namespace to configure the placeholder support instead of the bean declaration and it is always wise to prefix it with from where/how you want it to be loaded. 另一个技巧是,使用名称空间而不是bean声明来配置占位符支持,并且明智的做法是在其希望加载的位置/方式上添加前缀。

<context:property-placeholder location="classpath:/jdbc.properties" />

A final word on the placeholder support. 关于占位符支持的最终决定。 This class is a BeanFactoryPostProcessor and it will process bean definitions and replace the placeholders in them. 此类是BeanFactoryPostProcessor ,它将处理Bean定义并替换其中的占位符。 However it does this only for beans in the same application context!. 但是,它仅对同一应用程序上下文中的bean执行此操作! If you defined this bean in the parent and expect it to replace placeholders in the child context then that isn't going to happen. 如果您在父级中定义了该bean,并期望它在子级上下文中替换占位符,那么那将不会发生。

You need to tell Spring to search the classpath for the properties file, otherwise it will not be able to find it. 您需要告诉Spring在类路径中搜索属性文件,否则它将无法找到它。 Change the property file location from jdbc.properties to classpath:/jdbc.properties . 将属性文件的位置从jdbc.properties更改为classpath:/jdbc.properties

Try this resolver instead: 尝试使用此解析器:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有PropertyPlaceholderConfigurer bean的Spring @Configuration文件无法解析@Value注释 - Spring @Configuration file with PropertyPlaceholderConfigurer bean doesn't resolve @Value annotation 自定义 PropertyPlaceholderConfigurer 不解析嵌入的属性值 - Custom PropertyPlaceholderConfigurer doesn't resolve embedded property values Spring PropertyPlaceholderConfigurer和具有多个值的键 - Spring PropertyPlaceholderConfigurer and keys with multiple values PropertyPlaceholderConfigurer 不适用于 ClassPathXmlApplicationContext - PropertyPlaceholderConfigurer doesn't working with ClassPathXmlApplicationContext Spring:无法使用PropertyPlaceholderConfigurer加载junit上的属性 - Spring: can't load properties on junit with PropertyPlaceholderConfigurer 春季黄瓜不提供服务 - Spring cucumber doesn't inject service Rest + Spring AOP +接口未注入 - Rest + Spring AOP + interface doesn't inject Spring注释@Inject不起作用 - Spring annotation @Inject doesn't work 如何将tomcat context.xml参数值获取到Spring PropertyPlaceholderConfigurer中 - How to get tomcat context.xml Parameters values into spring PropertyPlaceholderConfigurer 在Spring中使用PropertyPlaceholderConfigurer创建具有不同值的多个类实例 - creating multiple instances of class with diffrent values using PropertyPlaceholderConfigurer in Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM