未在xml中读取属性文件值

问题描述 投票:0回答:1

我正在学习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>

Property文件如下:

pointA.pointX=0
pointA.pointY=0

显示以下异常:

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

提前致谢

java spring
1个回答
0
投票

经过不断的挖掘,我终于得到了答案。感谢来自Youtube的Manish Shen。

我的问题是,

  1. 我把类名命名为错误。此类名称在以前的spring版本中使用。新类名是“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”。
  2. spring.xml属性中的值应写为“$ {pointA.pointX}”而不是“#{pointA.pointX}”。

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

© www.soinside.com 2019 - 2024. All rights reserved.