【问题标题】:context:component-scan" is not bound上下文:组件扫描“未绑定
【发布时间】:2013-05-21 15:22:16
【问题描述】:

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

【问题讨论】:

  • 您缺少context 的命名空间声明。
  • 我对这些命名空间有点困惑,你能详细说明一下吗?
  • 下面的答案涵盖了大部分内容。

标签: java spring


【解决方案1】:

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

【讨论】:

  • 命名空间的用途是什么?一旦我想迁移到 spring 4,我是否必须更新它们?
  • “效用”是什么意思? Spring 需要在实际使用之前知道命名空间正在使用中,类似于声明变量。所有版本的 Spring 都需要命名空间声明
【解决方案2】:

是的,您必须添加

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

【讨论】:

【解决方案3】:

你在那里缺少 Context (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">

添加此代码的最后一行。

【讨论】:

    【解决方案4】:

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

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

    1. &lt;context:component-scan base-package="com.spring.study" /&gt;
    2. &lt;context:annotation-config/&gt;
    3. &lt;mvc:annotation-driven /&gt;

    然后在复制和粘贴代码之前,请在标题 &lt;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>
    

    干杯!德曼!

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2012-11-11
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-17
        • 1970-01-01
        相关资源
        最近更新 更多