java - 上下文:xml 中的属性占位符
问题描述
我是弹簧框架的新手。当我尝试在我的 xml 文件中加载属性文件时,它显示以下错误。
log4j:WARN 找不到记录器的附加程序(org.springframework.context.support.ClassPathXmlApplicationContext)。log4j:WARN 请正确初始化 log4j 系统。线程“main”中的异常线程“main”中的异常org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自类路径资源[byConstructor.xml]的XML文档中的第10行无效;嵌套异常是 org.xml.sax.SAXParseException;行号:10;列号:70;元素“context:property-placeholder”的前缀“context”未绑定。
这是我的.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<context:property-placeholder location="classpath:from.properties"/>
<bean id="u" class="dependenceInjection.ByConstructor">
<constructor-arg value="101" type="int"></constructor-arg>
<constructor-arg value="java"></constructor-arg>
</bean>
<bean id="impleCoach" class="dependenceInjection.ImpleCaoch">
<property name="name" value="${value}" />
</bean>
来自.properties
value=value from properties
提前致谢
解决方案
正确的 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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:property-placeholder location="classpath:from.properties"/>
<bean id="u" class="dependenceInjection.ByConstructor">
<constructor-arg value="101" type="int"></constructor-arg>
<constructor-arg value="java"></constructor-arg>
</bean>
<bean id="impleCoach" class="dependenceInjection.ImpleCaoch">
<property name="name" value="${value}" />
</bean>
</beans>
注意:此 xml 将起作用。但我建议在您编写的 xml 中检查您的 bean。
请遵循此约定来创建 bean:
示例:Class : Team , package: com.demo , id = firstletter of the class is small , 即team
<bean id ="team" class="com.demo.Team">
</bean>
推荐阅读
- android - kotlin 中的 recycler 不能为空
- c# - C# 自定义异常不可调用或类型无效
- css - 将 UIFont 转换为 css 字体样式字符串
- azure - 主机路径不允许作为卷源,您需要引用“卷”部分中定义的 Azure 文件共享
- python - 下划线的使用:q.choice_set.all()
- firebase - 如何递归删除firestore中的集合?
- python-3.x - 使用 cytpes 加载 dll 返回 WinError 127
- javascript - 垂直对齐矩形中心的段落
- c - mmap(MAP_SHARED) 调用在 GPFS 中不起作用
- python - 为什么我无法从 __init__ 访问 Kivy lang 属性?