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

时间:2019-02-24 17:18:36

标签: java spring spring-bean

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

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);



    }

}

一切似乎都还可以,但是不确定为什么会出现此错误。 坚持了一段时间。有人可以看看吗?我想念的是什么?

谢谢

2 个答案:

答案 0 :(得分:1)

似乎Spring容器正在尝试在TestController之前实例化PropertyPlaceholderConfigurer 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");
  • 设置方法名称:setLog4jConfig
  • 获取方法的名称:getLog4jConfig

答案 1 :(得分:0)

尝试将PropertyPlaceHolder Bean替换为:

selectedCars:any[] = [];