context:component-scan“没有绑定

时间:2013-05-21 15:22:17

标签: java spring

我是春天新手,我知道这个问题已被多次询问,但我不得不再问一次。

我想,我已经完成了相应的命名空间声明,但仍面临错误"The prefix "context" for element "context:component-scan" is not bound."有一个类似的问题here,但我得到答案

这是我的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>

5 个答案:

答案 0 :(得分:81)

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">  

答案 1 :(得分:8)

是的,你必须添加

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">

答案 2 :(得分:3)

您缺少上下文(http://www.springframework.org/schema/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">

添加此代码的最后一行。

答案 3 :(得分:3)

永远不仅仅是复制和过去。但

首先看看你在xml文件中使用了哪个注释。让我们说你的xml包含以下内容......

  1. <context:component-scan base-package="com.spring.study" />
  2. <context:annotation-config/>
  3. <mvc:annotation-driven />
  4. 然后,在复制和过去代码之前,请在标题<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>
    

    干杯! DMAN!

答案 4 :(得分:2)

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