简体   繁体   English

context:xml中的property-placeholder

[英]context:property-placeholder in xml

I am new to spring framework. 我是Spring框架的新手。 When I am trying to load properties files in my xml file it is showing the following error. 当我尝试在XML文件中加载属性文件时,它显示以下错误。

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN找不到记录器的附加程序(org.springframework.context.support.ClassPathXmlApplicationContext)。 log4j:WARN Please initialize the log4j system properly. log4j:WARN请正确初始化log4j系统。 Exception in thread "main" Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [byConstructor.xml] is invalid; 线程“主”中的异常线程“主”中的异常org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:类路径资源[byConstructor.xml]中XML文档中的第10行无效; nested exception is org.xml.sax.SAXParseException; 嵌套的异常是org.xml.sax.SAXParseException; lineNumber: 10; lineNumber:10; columnNumber: 70; columnNumber:70; The prefix "context" for element "context:property-placeholder" is not bound. 元素“ context:property-placeholder”的前缀“ context”未绑定。

Here is my my .xml file: 这是我的.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>

from.properties from.properties

value=value from properties

thanks in advance 提前致谢

The proper xml file : 正确的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>

Note : This xml will work. 注意:此xml将起作用。 But I recommend checking your beans in the xml that you have written. 但是我建议您在编写的xml中检查bean。

Please follow this convention for creating beans : 请遵循以下约定来创建bean:

Example : Class : Team , package: com.demo , id = firstletter of the class is small , ie, team 示例:类: Team ,包: com.demo ,id = 该类的首字母小 ,即团队

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

byConstructor.xml我认为我的xml代码是正确的,您可以在下一张图片中检出,但是eclipse将<context:property-placeholder location="classpath:from.properties"/> as an error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM