LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.
Survive during the day and develop at night。
目录
概 述
原理概述
SpringMVC与Servlet 3.0结合
web容器是管理servlet(通过servlet容器),以及监听器(Listener)和过滤器(Filter)的。这些都是在web容器的掌控范围里。
Tomcat 7以上的版本支持Servlet3.0。在Servlet 3.0环境中,当容器启动时,容器会在类路径中查找ServletContainerInitializer接口的实现类,如果发现这样的类就会用这个类来配置Servlet容器(Tomcat,Jetty)。
javax.servlet.ServletContainerInitializer
在spring中有存在一个SpringServletContainerInitializer类实现了ServletContainerInitializer接口,SpringServletContainerInitializer类在onStartup方法中又去查找
WebApplicationInitializer接口的实现类,在spring中有一个WebApplicationInitializer的实现类AbstractAnnotationConfigDispatcherServletInitializer。
因此只要继承AbstractAnnotationConfigDispatcherServletInitializer类就可以配置DispatcherServlet与Spring的ApplicationContext(Spring的核心接口,是BeanFactory接口的子接口)。
org.springframework.web.SpringServletContainerInitializer
org.springframework.web.WebApplicationInitializer
org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer
在springMVC应用程序中一般有两个上下文应用。
一个是Spring的应用上下文,是由ContextLoaderListener监听器进行创建,这个监听器是一个ServletContextListener的子类,也就是ServletContext监听器。这个应用上下文加载应用中的业务层与数据访问的的一些bean.
另一个是SpringMVC的应用上下文,是由DispatcherServlet启动时创建的。这个应用上下文加载一个Web组件的bean,例如控制器,视图解析器,以及处理器映射器。
以前连个应用上下文在创建时都需要都xml配置文件,两个上下文的配置文件的位置在web.xml中进行配置
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 在ServletContextListener中初始化spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 在DispatcherServlet中初始化springMVC容器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 设置过滤器,设置字符编码 -->
<!-- 字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResposeEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置cxf的核心控制器 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- 所有来自/webservice/*的请求交给cxf处理 -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
和Servlet 3.0结合之后,就可以采用两个java配置类代替xml配置文件:
package spitter.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = {"spitter"},excludeFilters =@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class))
public class RootConfig {
}
如下:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration//声明为配置类
@EnableWebMvc//启动SpringMVC组件。如果在xml配置,也可以采用<mvc:annotation-driven>标签启动
@ComponentScan("spitter.web")//启动扫描组件
public class WebConfig extends WebMvcConfigurationSupport {
//配置视图解析器
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver=new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
//配置静态资源处理
@Override
protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();//通过调用enable()方法,我们要求DispatcherServlet对静态资源的请求转发到
//Servlet容器中默认的Servlet上,而不是使用DispatcherServlet本身来处理静态资源请求。
}
}
相关工具如下:
分析:
小结:
主要讲述了Spring MVC是如何支持 Servlet 3.0 的,请大家指正~
参考资料和推荐阅读
1.链接: 参考资料.