Spring MVC的:&之间的LT差分;背景:组件扫描>和<注解驱动/>标签? [英] Spring MVC: difference between <context:component-scan> and <annotation-driven /> tags?

查看:243
本文介绍了Spring MVC的:&之间的LT差分;背景:组件扫描>和<注解驱动/>标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

若干天前,我就开始研究这个春天的Hello World教程:<一href=\"http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/\">http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

Some days ago I began to study this Spring Hello World Tutorial: http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

在本教程中的Spring DispatcherServlet使用的弹簧servlet.xml中配置的的文件,这其中:

In this tutorial Spring DispatcherServlet is configured using the spring-servlet.xml file, this one:

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

在这个文件我使用的背景:组件扫描标签说,春天已经扫描我的文件搜索的注释,因此,例如,当控制器类发现的方法注解通过 @RequestMapping(/你好)标注知道,这种方法对于处理以/你好结束URL的HTTP请求。这是简单的...

In this file I am using the context:component-scan tag to say that Spring have to scan my file searching the annotation, so for example, when the controller class finds that a method is annotated by @RequestMapping("/hello") annotation knows that this method handles the HTTP Request toward the URL ending with "/hello". This is simple...

现在我的疑问是关系到Spring MVC的模板项目,我可以自动建立在STS \\ Eclipse中。

Now my doubt is related to the Spring MVC template project that I could automatically build in STS\Eclipse.

当我在STS创建一个新的Spring MVC项目我有我的 DispatcherServlet的的是由一个名为配置的的servlet-context.xml中包含类似的一些配置previous示例文件。

When I create a new Spring MVC project in STS I have that my DispatcherServlet is configured by a file named servlet-context.xml that contains some configuration similar to the previous example file.

在这个文件中,我仍然有部分扫描标签:

In this file, I still have the component scan tag:

<context:component-scan base-package="com.mycompany.maventestwebapp" />

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

but I have also another tag (that look like have similar task), this one:

<annotation-driven />

什么是这两个变量之间的区别?结果
另外一个奇怪的是,previous例子(不使用注释驱动的标签)非常相似,该项目通过使用Spring MVC的模板项目STS创建,但如果我删除注释驱动标签从它的配置文件的项目不运行,并给我下面的错误: HTTP状态404 -

而在堆栈跟踪我有:

警告:org.springframework.web.servlet.PageNotFound - 未找到HTTP请求与URI的映射[/ maventestwebapp /]在DispatcherServlet的名为'appServlet

但是,为什么?在previous示例工作良好,没有注释驱动的标签,而这个控制器类很相似。事实上,只有一个朝向处理HTTP请求方法/路径

But why? The previous example works well without annotation-driven tag, and this controller class is very similar. In fact, there is only one method that handles HTTP request toward "/" path

这是我的控制器类的code:

This is the code of my controller class:

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

有人可以帮助我理解这件事?​​

Can someone help me to understand this thing?

非常感谢你!

推荐答案

&LT; MVC:注解驱动/&GT; 意味着你可以定义Spring bean的依赖,而不实际用XML指定一堆元素或实现一个接口或扩展的基类。例如 @Repository 来告诉春天一类是道,而无需延长的JpaDaoSupport 或DaoSupport其他一些子类。同样 @Controller 告诉春季指定的类包含将处理HTTP请求,而无需您实现Controller接口或扩展实现控制器子类的方法。

<mvc:annotation-driven /> means that you can define spring beans dependencies without actually having to specify a bunch of elements in xml or implement an interface or extend a base class. For example @Repository to tell spring that a class is a Dao without having to extend JpaDaoSupport or some other subclass of DaoSupport. Similarly @Controller tells spring that the the class specified contains methods that will handle http requests without you having to implement the Controller interface or extend a subclass that implements controller.

在春季启动时,它读取它的XML配置文件,然​​后查找&LT;豆类在它的元素,如果它看到的东西像&LT; bean类=com.example.Foo/&GT; 与富被打上了 @Controller 它知道类是一个控制器,并将它因此。默认情况下春假设所有它应该管理类中的beans.xml文件中明确定义。

When spring starts up it reads its xml configuration file and looks for <bean elements within it if it sees something like <bean class="com.example.Foo" /> and Foo was marked up with @Controller it knows that the class is a controller and treats it as such. By default spring assumes that all the classes it should manage are explicitly defined in the beans.xml file.

与组件扫描&LT;背景:组件扫描基包=com.mycompany.maventestwebapp/&GT; 告诉春天,它应搜索类路径所有下com.mycompany.maventestweapp类,看看每个类,看看它是否有一个 @Controller @Repository @Service @Component ,如果是的话那么Spring将与bean注册类厂为你输入&LT; bean类=.../&GT; 中的XML配置文件。

Component scanning with <context:component-scan base-package="com.mycompany.maventestwebapp" /> is telling spring that it should search the class path for all the classes under com.mycompany.maventestweapp and look at each class to see if it has a @Controller, or @Repository, or @Service, or @Component and if it does then Spring will register the class with the bean factory as if you had typed <bean class="..." /> in the xml configuration files.

在一个典型的Spring MVC应用程序,你会发现有两个spring配置文件,该配置应用程序上下文,通常与Spring上下文监听器启动的文件。

In a typical spring mvc app you will find that there are two spring configuration files, a file that configures the application context, usually started with the spring context listener.

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

和一个Spring MVC的配置文件通常开始与Spring分发程序Servlet。例如。

And a Spring MVC configuration file usually started with the Spring dispatcher servlet. For example.

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

Spring有分层bean工厂的支持,所以在Spring MVC中的情况下,调度servlet上下文是主应用程序上下文的孩子。如果servlet上下文被问了一个名为ABC豆它看起来在servlet上下文首先,如果它没有找到它有它的外观在父上下文,它是应用程序上下文。

Spring has support for hierarchical bean factories, so in the case of the spring mvc, the dispatches servlet context is a child of the main application context. If the servlet context was asked for a bean called "abc" it will look in the servlet context first, if it does not find it there it will look in the parent context, which is the application context.

常见豆类,如数据源,JPA配置,业务服务在应用程序上下文中定义,而MVC具体配置去与该servlet相关的配置文件。

Common beans such as data sources, jpa configuration, business services are defined in the application context while MVC specific configuration goes not the configuration file associated with the servlet.

希望这有助于。

这篇关于Spring MVC的:&之间的LT差分;背景:组件扫描&GT;和&lt;注解驱动/&GT;标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆