首页 > 解决方案 > 上下文: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

提前致谢

标签: javaxmlspring

解决方案


正确的 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>

推荐阅读