首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring MVC:<context:component-scan>和<注解驱动的/>标记之间的区别?

Spring MVC:<context:component-scan>和<注解驱动的/>标记之间的区别?
EN

Stack Overflow用户
提问于 2012-12-02 01:46:27
回答 3查看 149.2K关注 0票数 66

几天前,我开始学习这个Spring Hello World教程:http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

在本教程中,SpringServlet.xml使用DispatcherServlet -Servlet.xml文件进行配置:

代码语言:javascript
复制
<?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.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.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>

在这个文件中,我使用context:component-scan标签来说明Spring必须扫描搜索注释的文件,因此,例如,当控制器类发现一个方法被@RequestMapping("/hello")注释注释时,就知道这个方法处理指向以"/hello“结尾的URL的context:component-scan请求。这很简单。

现在,我的疑问与我可以在STS\Eclipse中自动构建的Spring MVC模板项目有关。

当我在STS中创建一个新的Spring MVC项目时,我发现我的DispatcherServlet是由一个名为servlet-context.xml的文件配置的,该文件包含一些类似于前面示例文件的配置。

在这个文件中,我仍然有组件扫描标记:

代码语言:javascript
复制
<context:component-scan base-package="com.mycompany.maventestwebapp" />

但是我还有另一个标签(看起来有类似的任务),这个:

代码语言:javascript
复制
<annotation-driven />

这两个标签有什么区别?

另一个“奇怪”的事情是,前面的示例(没有使用注释驱动的标记)与STS使用Spring MVC模板项目创建的项目非常相似,但是如果我从项目的配置文件中删除注释驱动的标记,项目就不会运行,并给出以下错误:HTTP status404 -

在堆栈跟踪中,我有:

WARN : org.springframework.web.servlet.PageNotFound -在名为'appServlet'的DispatcherServlet中找不到URI /maventestwebapp/的HTTP请求的映射

但是为什么呢?上面的示例在没有注释驱动的标记的情况下工作得很好,并且这个控制器类非常相似。实际上,只有一种方法可以处理指向"/“路径HTTP请求

这是我的控制器类的代码:

代码语言:javascript
复制
package com.mycompany.maventestwebapp;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
*/
@Controller
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );

    return "home";
}

有人能帮我理解这个东西吗?

非常感谢!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13661985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档