1. 配置示例
当在容器(如tomcat、jetty等)中启动web项目时,首先会读取项目web.xml配置文件里的配置,web.xml最核心的配置抽取如下:
1.1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>hello</display-name>
<!--监听tomcat容器启动后,通过指定的配置文件初始化spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/root-context.xml</param-value>
</context-param>
<!--监听器,用来监听tomcat容器启动-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置SpringMVC前端控制器-->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定SpringMVC配置文件 -->
<!--若指定contextConfigLocation,则该文件为 "<param-value></param-value>"指定的值-->
<!--若没有指定contextConfigLocation,该文件为"/WEB-INF/appServlet-context.xml"-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/servlet-context.xml</param-value>
</init-param>
</servlet>
<!--servlet与servlet-mapping成对出现-->
<!--servlet-mapping用来配置哪些请求会经过前端控制器处理-->
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!--匹配所有请求(不包含.jsp)-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
1.2 servlet-context.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/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-autowire="byName" default-lazy-init="false">
<mvc:annotation-driven/>
<!--扫描包配置,会自动装配base-package包下的bean-->
<context:component-scan base-package="spittr.web.controller"/>
<mvc:default-servlet-handler/>
<!--jsp视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="0"/>
</bean>
</beans>
1.3 root-context.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: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">
<!--扫描包配置,会自动装配base-package包下的bean-->
<context:component-scan base-package="spittr.data.impl"/>
</beans>
2. 详细配置说明
2.1 <mvc:annotation-driven/>
<mvc:annotation-driven/>标签对应的解析类是`org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser·。这个类主要是隐式地向 Spring容器注册
- RequestMappingHandlerMapping、BeanNameUrlHandlerMapping
这两个是HandlerMapping接口的实现类,用来处理请求映射的。其中第一个是处理@RequestMapping注解的。第二个会将controller类的名字映射为请求url。 - RequestMappingHandlerAdapter、HttpRequestHandlerAdapter、SimpleControllerHandlerAdapter
这三个是用来处理请求的。确定调用哪个controller的哪个方法来处理当前请求。第一个处理@Controller注解的处理器,支持自定义方法参数和返回值。第二个是处理继承HttpRequestHandler的处理器。第三个处理继承自Controller接口的处理器。 - ExceptionHandlerExceptionResolver、ResponseStatusExceptionResolver、DefaultHandlerExceptionResolver
这三个是用来处理异常的解析器。
2.2 <context:annotation-config/>
当我们需要使用注解模式时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
简单的说,用什么注解,就需要声明对应的BeanPostProcessor。这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。
2.3 <context:component-scan/>
<context:component-scan/>标签是告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件。
该配置项其实也包含了自动注入2.2中所述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去,但必须要配置全!以防万一,还是同时声明的好。
2.4 <mvc:annotation-scan/>
告知Spring启用注解驱动
2.5 <mvc:default-servlet-handler/>
在Spring MVC上下文中定义一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。
一般Web应用服务器默认的Servlet名称是"default",因此DefaultServletHttpRequestHandler可以找到它。如果你所有的Web应用服务器的默认Servlet名称不是"default",则需要通过default-servlet-name属性显示指定:
<mvc:default-servlet-handler default-servlet-name="所使用的Web服务器默认使用的Servlet名称" />
网友评论