gpt4 book ai didi

java - 无效的 bean 定义 : Could not resolve placeholder

转载 作者:行者123 更新时间:2023-11-30 05:45:48 26 4
gpt4 key购买 nike

我第一次使用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

应用程序属性

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



}

}

一切似乎都很好,但不知道为什么会出现这个错误。一时间陷入这个困境。有人可以看一下吗?我缺少什么?

谢谢

最佳答案

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

您可以尝试输入 <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

关于java - 无效的 bean 定义 : Could not resolve placeholder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54854464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com