27

我是春天的新手,我知道这个问题已经被问过很多次了,但我不得不再问一次。

我想,我已经做了适当的命名空间声明,但仍然面临错误 这里"The prefix "context" for element "context:component-scan" is not bound." 有一个类似的问题,但我没有得到答案

这是我的 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">



    <bean id="point1" class="org.sri.proj.Point">
        <property name="x" value="0" />
        <property name="y" value="0" />
    </bean>

    <bean id="point2" class="org.sri.proj.Point">
        <property name="x" value="10" />
        <property name="y" value="10" />
    </bean>

    <context:component-scan base-package="org.sri.proj"/>

</beans>
4

5 回答 5

95

context将命名空间声明添加到beans应用程序上下文文件中的标记定义中

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
于 2013-05-21T15:25:08.370 回答
10

是的,您必须添加

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

所以它看起来像:

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
于 2013-05-21T15:26:23.313 回答
3

您在http://www.springframework.org/schema/context那里缺少 Context() 命名空间:

<beans:beans xsi:schemaLocation="http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd 


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

添加此代码的最后一行。

于 2013-05-21T15:26:11.140 回答
3

为了所有的时间,不只是简单地复制和过去。但。

首先查看您在 xml 文件中使用的注释。假设您的 xml 包含以下内容...

  1. <context:component-scan base-package="com.spring.study" />
  2. <context:annotation-config/>
  3. <mvc:annotation-driven />

然后在你复制和过去的代码之前,看看你在标题中真正需要什么<beans:beans section ...

所以,这样做满足上述配置设置和

确保每次更改文件后都进行清理和构建!

     <?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.spring.study" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

干杯! 德曼!

于 2014-02-06T12:59:57.107 回答
2

您应该添加xmlns:context="http://www.springframework.org/schema/context" 到您的 bean xml 中。

于 2017-02-20T13:45:38.877 回答