这个问题在这里已有答案:
前几天我开始研究这个Spring Hello World教程:http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/
在本教程中,使用spring-servlet.xml文件配置Spring 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.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") 注释时,知道此方法处理HTTP请求到URL以"/hello"结尾 . 这很简单......
现在我的疑问与我可以在STS \ Eclipse中自动构建的Spring MVC模板项目有关 .
当我在STS中创建一个新的Spring MVC项目时,我的DispatcherServlet由一个名为 servlet-context.xml 的文件配置,该文件包含一些与前一个示例文件类似的配置 .
在这个文件中,我仍然有组件扫描标记:
<context:component-scan base-package="com.mycompany.maventestwebapp" />
但我还有另一个标签(看起来有类似的任务),这一个:
<annotation-driven />
这两个标签有什么区别?
另一个"strange"事是上一个例子(不要运行并给我以下错误: HTTP Status 404 -
在堆栈跟踪中,我有:
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/maventestwebapp/] in DispatcherServlet with name 'appServlet'
但为什么?前面的示例在没有注释驱动标记的情况下运行良好,并且此控制器类非常相似 . 实际上,只有一种方法可以处理对“/”路径的HTTP请求
这是我的控制器类的代码:
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";
}
有人能帮我理解这件事吗?
非常感谢你!
3 回答
<mvc:annotation-driven />
意味着您可以定义spring bean依赖项,而无需在XML中指定一堆元素或实现接口或扩展基类 . 例如@Repository
告诉spring一个类是Dao而不必扩展JpaDaoSupport
或DaoSupport的其他一些子类 . 类似地,@Controller
告诉spring,指定的类包含将处理Http请求的方法,而无需实现Controller接口或扩展实现控制器的子类 .当spring启动时,它会读取其XML配置文件并在其中查找
<bean
元素,如果它看到类似<bean class="com.example.Foo" />
并且Foo被标记为@Controller
它知道该类是控制器并将其视为这样 . 默认情况下,Spring假定它应该管理的所有类都在beans.XML文件中显式定义 .使用
<context:component-scan base-package="com.mycompany.maventestwebapp" />
进行组件扫描告诉它应该在类路径中搜索com.mycompany.maventestweapp下的所有类,并查看每个类以查看它是否具有@Controller
,或@Repository
,或@Service
,或@Component
,如果是,那么Spring将在bean工厂中注册该类,就像在XML配置文件中键入了<bean class="..." />
一样 .在典型的spring MVC应用程序中,您会发现有两个spring配置文件,一个配置应用程序上下文的文件,通常以Spring上下文侦听器启动 .
Spring MVC配置文件通常以Spring调度程序servlet开头 . 例如 .
Spring支持分层bean工厂,因此对于Spring MVC,调度程序servlet上下文是主应用程序上下文的子代 . 如果servlet上下文被要求提供一个名为“abc”的bean,它将首先查看servlet上下文,如果它在那里找不到它,它将查看父上下文,即应用程序上下文 .
诸如数据源,JPA配置,业务服务之类的公共bean在应用程序上下文中定义,而MVC特定配置不是与servlet相关联的配置文件 .
希望这可以帮助 .
告诉Spring扫描这些包的Annotations .
注册RequestMappingHanderMapping,RequestMappingHandlerAdapter和ExceptionHandlerExceptionResolver,以支持MVC附带的带注释的控制器方法,如@ RequestMapping,@ ExceptionHandler等 .
这也使ConversionService能够支持注释驱动的输出格式化以及输入的注释驱动验证 . 它还支持@ResponseBody,您可以使用它来返回JSON数据 .
您可以在@Configuration类中使用@ComponentScan(basePackages = {“...”,“...”}和@EnableWebMvc)使用基于Java的配置完成相同的操作 .
查看3.1文档以了解更多信息 .
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config
注释驱动向Spring表明它应该扫描带注释的bean,而不仅仅是依赖于XML bean配置 . 组件扫描指示在哪里查找这些bean .
这是一些文档:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable