本文主要简单讲解框架整合的思路。

1、Spring框架的搭建

这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件,那么spring容器搭建完成。(当然org.springframework的核心jar包需要引入)

当然为了更加易用支持J2EE应用,一般我们还会加上如下:

Spring监听HTTP请求事件:org.springframework.web.context.request.RequestContextListener


 <context-param>
   <param-name>contextConfigLocationparam-name>
   <param-value>classpath*:webconfig/service-all.xmlparam-value>
 context-param>
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
 listener>
 <listener>
   
   
   
   
   
   
   <listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
  listener>
 <listener>
   <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
 listener>
 <filter>
   <filter-name>encodingFilterfilter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
   <init-param>
     <param-name>encodingparam-name>
     <param-value>UTF-8param-value>
   init-param>
   <init-param>
     <param-name>forceEncodingparam-name>
     <param-value>falseparam-value>
   init-param>
 filter>
 <filter-mapping>
   <filter-name>encodingFilterfilter-name>
   <url-pattern>/*url-pattern>
 filter-mapping>
 


2、Spring MVC的搭建

首先我们知道Spring MVC的核心是org.springframework.web.servlet.DispatcherServlet,所以web容器中少不了它的注册。(当然org.springframework的web、mvc包及其依赖jar包需要引入)


 <servlet>
   <servlet-name>Spring-MVCservlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
   <init-param>
     <param-name>contextConfigLocationparam-name>
     <param-value>classpath*:spring/spring-mvc.xmlparam-value>
   init-param>
   <load-on-startup>1load-on-startup>
 servlet>
 <servlet-mapping>
   <servlet-name>Spring-MVCservlet-name>
   <url-pattern>*.dourl-pattern>
 servlet-mapping>
 


同时为了更好使用MVC,spring-mvc.xml需要配置以下:

1)(可选)多部分请求解析器(MultipartResolver)配置,与上传文件有关 需要类库commons-io、commons-fileupload

"multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8">property>
   <property name="maxUploadSize" value="104857600">property>

   <property name="maxInMemorySize" value="40960">property>
 bean>


2)(可选)本地化(LocaleResolver)配置

3)(可选)主题解析器(ThemeResolver)配置 

4)(必选)处理器映射器(HandlerMapping)配置,可以配置多个,一般采用RequestMappingHandlerMapping或者自定义

这里我们自定义了一个处理器映射器,继承重写RequestMappingHandlerMapping,支持@RequestMapping无需任何path参数自动装载类名或方法作为url路径匹配。

"handlerMapping" 
   class="io.flysium.framework.web.servlet.mvc.method.annotation.CustomHandlerMapping">
   <property name="order" value="-1" />
 bean>


CustomHandlerMapping实现:

@Override
 protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) {
   RequestMappingInfo info = createRequestMappingInfoDefault(method);
   if (info != null) {
     RequestMappingInfo typeInfo = createRequestMappingInfoDefault(handlerType);
     if (typeInfo != null)
       info = typeInfo.combine(info);
   }
   return info;
 }

 private RequestMappingInfo createRequestMappingInfoDefault(AnnotatedElement element) {
   RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element,
       RequestMapping.class);
   RequestCondition condition = (element instanceof Class)
       ? getCustomTypeCondition((Class) element)
       : getCustomMethodCondition((Method) element);
   /**
   * 以类名和方法名映射请求,参照@RequestMapping
   * 默认不需要添加任何参数(如:/className/methodName.do)
   */

   String defaultName = (element instanceof Class)
       ? ((Class) element).getSimpleName()
       : ((Method) element).getName();
   return requestMapping == null
       ? null
       : createRequestMappingInfo(requestMapping, condition, defaultName);
 }

 protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation,
     RequestCondition customCondition, String defaultName) {
   String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value());
   if (patterns != null && (patterns.length == 0)) {
     patterns = new String[]{defaultName};
   }
   return new RequestMappingInfo(
       new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(),
           this.useSuffixPatternMatch, this.useTrailingSlashMatch,
           this.fileExtensions),
       new RequestMethodsRequestCondition(annotation.method()),
       new ParamsRequestCondition(annotation.params()),
       new HeadersRequestCondition(annotation.headers()),
       new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
       new ProducesRequestCondition(annotation.produces(), annotation.headers(),
           this.contentNegotiationManager),
       customCondition);
 }


5)(必选)处理器适配器(HandlerAdapter)配置,可以配置多个,主要是配置messageConverters,其主要作用是映射前台传参与handler处理方法参数。一般扩展RequestMappingHandlerAdapter,或者自定义。如果我们需要json请求的处理,这里必须扩展。同时我们需要注意的是日期格式的转换。

另外Spring 4.2新特性,加之注解会自动注入@ControllerAdvice,可以定义RequestBodyAdvice、ResponseBodyAdvice,可以更方便地在参数处理方面着手自定义。

<bean id="handlerAdapter"
 class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

 <property name="order" value="-1" />
 <property name="messageConverters">
   <list>
   
     <ref bean="mappingJacksonHttpMessageConverter" />
   list>
 property>
 <property name="webBindingInitializer">
   <bean
     class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">

     <property name="conversionService">
       
       
   <bean
   class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

   bean>
     property>
   bean>
 property>
bean>

<bean name="jacksonObjectMapper"
 class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">

 <property name="featuresToDisable">
   <array>
   <util:constant
static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />

    array>
 property>
 
 <property name="simpleDateFormat">
   <value>yyyy-MM-dd HH:mm:ssvalue>
 property>
bean>


<bean id="mappingJacksonHttpMessageConverter"
 class="io.flysium.framework.http.converter.json.CustomJackson2HttpMessageConverter">

 <property name="objectMapper" ref="jacksonObjectMapper" />
 <property name="supportedMediaTypes">
   <list>
     <value>text/html;charset=UTF-8value>
     <value>application/json;charset=UTF-8value>
   list>
 property>
bean>


6)(可选)处理器异常解析器(HandlerExceptionResolver)配置,可以配置多个,配置Controller异常抛出后,我们是怎么样处理的,一般需要日志或做反馈的可以自定义。

7)(可选)请求到视图名翻译器(RequestToViewNameTranslator)配置,RequestToViewNameTranslator可以在处理器返回的View为空时使用它根据Request获得viewName。

8)(可选)视图解析器(ViewResolver)配置,可以配置多个,定义跳转的文件的前后缀 ,视图模式配置,主要针对@Controller返回ModelAndView的视图路径解析,动给后面控制器的方法return的字符串 加上前缀和后缀,变成一个 可用的url地址 。

"viewResolver"
   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   "prefix" value="/" />
   "suffix" value=".jsp" />
   "viewClass"
     value="org.springframework.web.servlet.view.JstlView" />
 


最后给Controller加入组件扫描吧,这样减少xml配置,直接在Java代码中加入注解即可。


 
 <context:component-scan base-package="io.flysium" use-default-filters="false">
   <context:include-filter type="annotation" 
     expression="org.springframework.stereotype.Controller" />

   <context:include-filter type="annotation" 
     expression="org.springframework.web.bind.annotation.RestController" />

   <context:include-filter type="annotation" 
     expression="org.springframework.web.bind.annotation.ControllerAdvice" />

 context:component-scan>


3、Mybatis整合

整合mybatis到Spring框架,我们需要mybatis的jar包,及mybatis-spring整合jar包。然后在Spring容器中注册配置org.mybatis.spring.SqlSessionFactoryBean(需要数据源,及指定Mybatis配置文件)org.mybatis.spring.SqlSessionTemplate即可。


更多整合请参照Git项目:https://git.oschina.net/svenaugustus/app-ss4m-less

目前除了ssm,另外整合redis(支持切换单节点配置、主从哨兵配置,集群配置)、spring session方案。

其中包括spring MVC的简单demo,用于学习交流。

我有一个微信公众号,经常会分享一些Java技术相关的干货;如果你喜欢我的分享,可以用微信搜索“Java团长”或者“javatuanzhang”关注。