无效的 bean 定义:无法解析占位符

Invalid bean definition: Could not resolve placeholder

我是第一次使用 springframework。所以写了一个小程序来测试 IoC 中生成的实例变量的值。但是我遇到以下错误:

Feb 24, 2019 10:40:13 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'loadingObject' defined in class path resource [Spring-Config.xml]: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'loadingObject' defined in class path resource [Spring-Config.xml]: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:228)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:213)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:691)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at com.jcg.spring.log4j.Mainclass.main(Mainclass.java:12)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:232)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:296)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:217)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:147)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85)
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:225)
    ... 9 more

我已将 application.properties 文件和 Spring-Config.xml 元数据文件放置到此位置 ...\src\main\resources

application.properties

log4j.configuration=C:\Softwares\ConfigFiles\log4j.properties

Spring-Config.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="propertiesToBeTaken" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:application.properties</value>
                <value>classpath*:*keys.properties</value>
            </list>
        </property>
    </bean>

    <bean id = "loadingObject" class = "com.jcg.spring.log4j.TestController">
        <property name="log4jConfig" value="${log4j.configuration}" />
    </bean> 

</beans>

代码片段

public class TestController  {

    public String log4jConfig;

    public void setlog4j(String log4jConfig){

        this.log4jConfig = log4jConfig;

    }

    public  String getlog4j(){

        return this.log4jConfig;
    }

}

public class Mainclass {

    public static void main(String[] args){


        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
        TestController obj = (TestController) context.getBean("TestController");
        System.out.println(obj.log4jCongif);



    }

}

一切似乎都很好,但不确定为什么会出现此错误。 卡在这一段时间。任何人都可以看看吗?我缺少什么?

谢谢

尝试将 PropertyPlaceHolder Bean 替换为:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="location">
            <value>application.properties</value>
        </property>
    </bean>

似乎 Spring 容器试图在 PropertyPlaceholderConfigurer 之前实例化 TestController bean,所以 属性 没有得到解析,因此出现错误。

您可以尝试将 <property name="ignoreUnresolvablePlaceholders" value="true"/> 放入 Spring-Config.xml 以告诉 spring 忽略未解析的属性。一旦 PropertyPlaceholderConfigurer 将被实例化,可能 属性 将得到解决。

试试这个

<bean id="propertiesToBeTaken" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>classpath:keys.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

还有一些变化:

  • TestController obj = (TestController) context.getBean("loadingObject");
  • setter 方法名:setLog4jConfig
  • getter 方法名称:getLog4jConfig