Java 在 applicationContext.xml 中添加 <mvc:annotation-driven> 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20440276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:55:45  来源:igfitidea点击:

Error when adding <mvc:annotation-driven> in applicationContext.xml

javaxmlspringhibernatespring-mvc

提问by outlookrperson

According to Spring MVC documentation the tag does the following:

根据 Spring MVC 文档,该标签执行以下操作:

  1. Configures the Spring 3 Type ConversionService (alternative to PropertyEditors)
  2. Adds support for formatting Number fields with @NumberFormat
  3. Adds support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
  4. Adds support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath
  5. Adds support for support for reading and writing XML, if JAXB is on the classpath (HTTP message conversion with @RequestBody/@ResponseBody)
  6. Adds support for reading and writing JSON, if Hymanson is o n the classpath (along the same lines as #5)
  1. 配置 Spring 3 Type ConversionService(替代 PropertyEditors)
  2. 添加对使用 @NumberFormat 格式化数字字段的支持
  3. 添加对使用@DateTimeFormat 格式化日期、日历和 Joda 时间字段的支持(如果 Joda 时间在类路径上)
  4. 添加对使用 @Valid 验证 @Controller 输入的支持,如果 JSR-303 提供程序在类路径上
  5. 添加对读取和写入 XML 的支持,如果 JAXB 在类路径上(使用 @RequestBody/@ResponseBody 进行 HTTP 消息转换)
  6. 添加对读取和写入 JSON 的支持,如果 Hymanson 在类路径上(与 #5 相同)

But when I add this to my applicationContext.xml file an error is thrown when the server (JBoss EAP 6.1) is started.

但是当我将它添加到我的 applicationContext.xml 文件中时,会在服务器 (JBoss EAP 6.1) 启动时抛出错误。

This is the error:

这是错误:

08:01:23,435 ERROR [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 68) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerMapping()] threw exception; nested exception is java.lang.ClassCastException: org.springframework.web.accept.ContentNegotiationManagerFactoryBean$$EnhancerByCGLIB$$6af53d42 cannot be cast to org.springframework.web.accept.ContentNegotiationManager

08:01:23,435 错误 [org.springframework.web.context.ContextLoader](ServerService 线程池 - 68)上下文初始化失败:org.springframework.beans.factory.BeanCreationException:在类中定义名称为“requestMappingHandlerMapping”的 bean 创建时出错路径资源[org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]:bean实例化失败;嵌套异常是 org.springframework.beans.factory.BeanDefinitionStoreException: 工厂方法 [public org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerMapping()]抛出异常;嵌套异常是 java.lang.ClassCastException: org.springframework.web.accept。

This is the web.xml:

这是 web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0">

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
</web-app>

This is the applicationContext.xml:

这是 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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-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">

    <mvc:annotation-driven />


    <context:component-scan base-package="br.gov.saude.ouvidor.apresentacao" />
    <context:component-scan base-package="br.gov.saude.ouvidor.apresentacao.controller  " />

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

This is the pom.xml:

这是 pom.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>xxx-xxxx</artifactId>
    <packaging>war</packaging>
    <name>xxx-xxxx</name>

    <properties>
        <project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>xxx.xxx.xxxxxxxx</groupId>
        <artifactId>xxxxxxx</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <optional>true</optional>           
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <optional>true</optional>           
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.19</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.19</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                    <outputFileName>${project.artifactId}</outputFileName>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>              
                <configuration>             
                    <warName>${project.artifactId}</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Removing the tag <mvc:annotation-driven>everything goes fine. What could be wrong? Any help will be appreciated.

删除标签<mvc:annotation-driven>一切正常。可能有什么问题?任何帮助将不胜感激。

Thanks!

谢谢!

采纳答案by Kevin Bowersox

In the web.xmlfile you are specifying that Java configuration will be used to setup the MVC components, however the MVC components are attempted to be setup within the applicationContext.xmlfile.

web.xml文件中,您指定将使用 Java 配置来设置 MVC 组件,但是尝试在applicationContext.xml文件中设置 MVC 组件。

The ContextLoaderListenerin web.xmlis setup to launch the /WEB-INF/applicationContext.xmlconfiguration file. The container setup by applicationContext.xmlshould not include the MVC components but should be used for registering beans outside of the MVCscope such as those for data persistence.

ContextLoaderListenerweb.xml是设置启动/WEB-INF/applicationContext.xml配置文件。容器设置applicationContext.xml不应包含 MVC 组件,而应用于注册MVC范围外的 bean,例如用于数据持久性的bean 。

If you truly want to use Java Configuration to setup the Spring MVC components you must move all of the Spring MVC components established in the applicationContext.xmlfile to a Java class annotated with @Configurationand translate the XML beans to their appropriate configurations in Java.

如果您真的想使用 Java 配置来设置 Spring MVC 组件,您必须将applicationContext.xml文件中建立的所有 Spring MVC 组件移动到一个用 Java 注释的 Java 类中,@Configuration并将 XML bean 转换为它们在 Java 中的适当配置。

See: Spring + Web MVC: dispatcher-servlet.xml vs. applicationContext.xml (plus shared security)

请参阅:Spring + Web MVC:dispatcher-servlet.xml 与 applicationContext.xml(加上共享安全性)