未在xml中读取属性文件值

时间:2018-03-04 05:33:52

标签: java spring

我正在学习Java Spring,在使用属性文件时遇到了问题。未在xml文件中读取属性文件值并抛出异常。

XML文件如下:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>


	<bean id="triangle1" class="com.springtest.shapes.Triangle">
		<property name="pointA" ref="zeroPoint"/>
		<property name="pointB" ref="point2" />
		<property name="pointC" ref="point3" />
	</bean>



	<bean id="zeroPoint" class="com.springtest.shapes.Point">
		<property name="x" value="#{pointA.pointX}" />
		<property name="y" value="#{pointA.pointY}" />
	</bean>

	<bean id="point2" class="com.springtest.shapes.Point">
		<property name="x" value="5" />
		<property name="y" value="2" />
	</bean>

	<bean id="point3" class="com.springtest.shapes.Point">
		<property name="x" value="-20" />
		<property name="y" value="10" />
	</bean>
	
	<bean class="com.springtest.main.DisplayNameBeanPostProcessor"/>
	
	<bean class="org.SpringFramework.beans.factory.config.PropertyPlaceHolderConfigurer">
		<property name="locations">
		<value>classpath:pointconfig.properties</value>
		</property>
	</bean>

</beans>
&#13;
&#13;
&#13;

属性文件如下:

&#13;
&#13;
pointA.pointX=0
pointA.pointY=0
&#13;
&#13;
&#13;

显示以下异常:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'pointA' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:100)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:87)
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:52)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:111)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:270)
at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:163)
... 28 more

还提供了项目结构 project structure

提前致谢

1 个答案:

答案 0 :(得分:0)

我在连续挖掘后终于得到了答案。感谢来自Youtube的Manish Shen。

我的问题是,

  1. 我将类名命名为错误。此类名称在以前的spring版本中使用。新的类名是&#34; org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&#34;。

  2. spring.xml属性中的值应写为&#34; $ {pointA.pointX}&#34;而不是&#34;#{pointA.pointX}&#34;。

  3. 我正在分享我得到答案的屏幕截图。 enter image description here