【问题标题】:Error after moving <mvc:annotation-driven/>: no declaration can be found for element 'mvc:annotation-driven'移动 <mvc:annotation-driven/> 后出错:找不到元素“mvc:annotation-driven”的声明
【发布时间】:2012-02-19 10:59:55
【问题描述】:

我有一个 dispatcher-servlet.xml 和一个 applicationContext.xml。

我一直在做一些重构,并移动了

<mvc:annotation-driven/>
<context:component-scan base-package="com.xxx"/>

从 dispatcher-servlet.xml 到 applicationContext.xml。

我现在收到此错误:

2012-01-26 10:34:36.434:WARN::Nested in org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.

我的 applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd"
       xmlns:context="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
        >

    <mvc:annotation-driven/>
    <context:component-scan base-package="com.xxx"/>

    <bean id="templateErrorListener"
          class="com.stringtemplate.log.Slf4jStringTemplateErrorListener"/>

    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
          p:config-location="/WEB-INF/ehcache.xml"/>

    <ehcache:annotation-driven cache-manager="ehCacheManager"/>

</beans>

我的 dispatcher-servlet.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">

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1"/>
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
            </list>
        </property>
        <property name="ignoreAcceptHeader" value="true"/>
    </bean>

    <bean class="com.stringtemplate.StringTemplateViewResolver">
        <property name="templateErrorListener" ref="templateErrorListener"/>
        <property name="templateRoot" value="/WEB-INF/templates/"/>
        <property name="order" value="2"/>
    </bean>
    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="3"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="formHttpMessageConverter"/>
                <ref bean="stringHttpMessageConverter"/>
            </list>
        </property>
    </bean>

    <bean id="formHttpMessageConverter "
          class="org.springframework.http.converter.FormHttpMessageConverter "/>
    <bean id="stringHttpMessageConverter "
          class="org.springframework.http.converter.StringHttpMessageConverter "/>

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

    <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:${systemTargetEnv}/app.properties</value>
                <value>classpath:/build.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName">
            <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
        </property>
    </bean>
</beans>

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    我一直在进行一些重构,并将&lt;mvc:annotation-driven/&gt;dispatcher-servlet.xml 移至applicationContext.xml

    你不想那样做。 &lt;mvc:annotation-driven/&gt; 用于启用注解式 MVC 控制器,并且这些控制器必须驻留在 dispatcher-servlet.xml 中。将&lt;mvc:annotation-driven/&gt; 放入applicationContext.xml 是没有意义的,不会有任何意义。

    不过,我可能应该回答这个问题,答案是,虽然您已经声明了 mvc 命名空间,但您没有告诉 Spring 在哪里可以找到它的架构。添加

    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    

    applicationContext.xml中的schemaLocation属性

    【讨论】:

    • 感谢您的回答 - 我已经移动了它,但现在我得到了上下文错误。我想我已经更新了架构。我已经修改了问题以反映这一点。我认为组件扫描应该在应用程序上下文中进行?!
    • 谢谢,我更新了我的上下文模式位置,现在我们又开始工作了。谢谢!!
    猜你喜欢
    • 2013-03-02
    • 2011-04-11
    • 2018-02-15
    • 1970-01-01
    • 2018-04-12
    • 2013-12-24
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多