Spring MVC为什么此Hello World在没有注释驱动标签的情况下运行良好(不同于其他任何Spring项目)


问题内容

我已开始研究Spring MVC,阅读此教程:http : //viralpatel.net/blogs/spring-3-mvc-
create-hello-world-application-
spring-3-mvc/

好的,这对我来说很清楚。

在此示例中,我具有 web.xml 文件来配置我的Web应用程序:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee    /web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>Spring3MVC</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

spring-servlet.xml 文件用于配置mu DispatcherServlet:

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

<context:component-scan
    base-package="net.viralpatel.spring3.controller" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

而且,正如您在上一个链接中看到的那样,我只有一个控制器类来处理朝向“ / hello” URL的HTTP请求…好吧…这对我来说很清楚…

在此示例旁边,我通过STS \ Eclipse中的相​​关模板项目创建了一个新的Spring MVC项目。

这个示例与上一个示例非常相似:我始终拥有web.xml文件来保护我的Web应用程序,一个用于配置DispatcherServlet的文件以及一个用于处理HTTP请求的控制器类。

但是有一些我无法理解的差异。

这是我的 web.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->

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


<!-- Creates the Spring Container shared by all Servlets and Filters -->

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

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

这是 servlet-context.xml 文件(用于配置我的DispatcherServlet的配置文件):

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<context:component-scan base-package="com.mycompany.maventestwebapp" />
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

好的,您可以看到两个示例的配置文件有所不同:

在STS \ Eclipse创建的项目中,在 servlet-context.xml 文件中,我具有这些配置,这些配置在我发布的第一个示例中不存在:

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

好的…我已经阅读了很多有关此配置标签的文档:意味着您可以定义spring
bean依赖关系,而无需实际在xml中指定一堆元素或实现接口或扩展基类。当spring启动时,它读取其xml配置文件并查找,并且Foo被@Controller标记,它知道该类是一个控制器并将其视为此类。默认情况下,spring假定它应管理的所有类均在beans.xml文件中明确定义。然后是
组件扫描
告诉Spring的标记,它应该在com.mycompany.maventestweapp下的所有类中搜索类路径,并查看每个类以查看其是否具有@
Controller,@ Repository,@ Service,@
Component以及是否然后,Spring会将类注册到Bean工厂,就像您在xml配置文件中键入的一样。BLABLABLA ETCETCETC
BLABLA

好吧,这对我来说是绝对清楚的!我想我没有问题要理解为什么我在第二个示例的DispatcherServler配置文件中有 注释驱动的 标记,这很清楚。

问题是要了解 为什么我的第一个示例的DispatcherServlet配置文件中没有此标记以及为什么它能正常工作 如果我尝试从servlet-
context.xml文件中删除此标记(在第二个示例中),则不会运行并进入错误提示我: HTTP状态404- 在堆栈跟踪中,我有:
警告:org.springframework.web.servlet.PageNotFound-在DispatcherServlet中找不到名称为’
appServlet’

我认为这是合理的行为,因为Spring尚未启用注释支持,因此没有注册我的控制器类(如果我在bean.xml中明确定义了该类,如何使用@Controller并使用@Controller注释我的控制器类)文件),因此无法使用此bean的方法来处理HTTP请求…好吧…听起来不错…

但是,为什么在第一个示例中没有注释驱动的标签就可以工作呢? 我没有注释驱动的标签(因此,我无法启用注释来声明什么是Spring
bean,例如控制器),也没有在bean.xml文件中明确声明该类……因此不应当我删除注释驱动的标签时,第二个示例无法正常工作…但是它有效:-/

我快要明白为什么…

拜托,你能帮我吗?

非常感谢

安德里亚


问题答案:

<mvc:annotation-driven> 是提供其他服务的帮助程序标记:

  • 注册默认处理程序映射
  • 消息转换(例如,将控制器中返回的对象转换为JSON)
  • JSR 303验证(@Valid批注)
  • 转换服务(如果指定了转换器,则控制器将能够将实体作为参数,即,来自请求的文本ID将通过MVC层转换为实际的业务对象。其他对象(如日期,枚举等)也是如此)
  • 也许在Spring的新版本中会添加更多功能。

它是通过org.springframework.web.servlet.config包中的AnnotationDrivenBeanDefinitionParser实现的

并不
需要为您简单的控制器工作,如果你有合适的映射。但是,如果您想做一些或多或少的高级工作,例如JSR
303验证,使用JSON实现REST服务,为MVC注册转换服务,那么该标签将很有用,因为它将这些服务的配置保留在一个位置。

这就是为什么将它包含在Spring模板项目中的原因(模板项目是开发Spring应用程序的起点(因此,您可以直接放入@Responsebody控制器方法返回的值或向MVC添加转换器))面向spring学习者)。

还请注意,处理程序映射配置在Spring
3.1中已更改

在Spring 3.1之前,类型和方法级别的请求映射在两个单独的阶段中进行了检查-
首先由DefaultAnnotationHandlerMapping选择一个控制器,然后由AnnotationMethodHandlerAdapter缩小要调用的实际方法。