【问题标题】:context:property-placeholder in xml上下文:xml 中的属性占位符
【发布时间】:2019-03-07 17:41:23
【问题描述】:

我是 Spring 框架的新手。当我尝试在我的 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文件吗?

标签: java xml spring


【解决方案1】:

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

Example : Class : Team, package:com.demo, id = firstletter of the class is small 即团队 p>

<bean id ="team" class="com.demo.Team">
</bean>

【讨论】:

  • 类名是正确的我添加了一张图片,请查看并建议我
  • 这是一个经过测试的 xml。它会起作用的。请正确设置您的项目
  • 在xsi:schemaLocation中,你还没有添加这个springframework.org/schema/mvcspringframework.org/schema/mvc/spring-mvc.xsd"。添加这个
  • xml 文件在没有&lt;context:property-placeholder location="classpath:from.properties/&gt; 的情况下正常工作,每当我在&lt;beans&gt; 标记中添加此标记时,Eclipse 将&lt;context:property-placeholder location="classpath:from.properties"/&gt; 标记为错误。我还在上面添加了屏幕截图,你可以在那里查看
  • xmlns:mvc="http://www.springframework.org/schema/mvc" 中有一个拼写错误,谢谢@Anish B
【解决方案2】:

byConstructor.xml 我认为我的 xml 代码是正确的,您可以在下图中查看,但 eclipse 标记为 &lt;context:property-placeholder location="classpath:from.properties"/&gt; as an error

【讨论】:

    猜你喜欢
    • 2013-12-06
    • 2012-08-03
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2017-02-20
    相关资源
    最近更新 更多