杂谈spring、springMVC

一、背景

目前项目组都在用SSM(spring+springMVC+mybatis)开发项目

大家基本都停留在框架的基本使用阶段,对框架的职责并不清晰,导致配置文件出现了不少问题

在这简单讲解一下spring、springMVC在项目中各自负责的工作

二、spring、springMVC的关系

spring粗略可分为五个模块

1、bean容器,负责bean的创建、管理,其中包括了依赖注入功能

2、aop模块,切面编程,降低应用耦合的好方式

3、数据访问与集成,提供语义丰富的异常层(可迅速理解数据库操作抛出的错误),提供事务管理,JMS封装

4、web和远程调用,集成了其它MVC框架并提供了自己的MVC框架(springMVC),集成RMI等远程服务调用服务,并自带了远程调用框架(HTTP invoker)

5、测试

由spring的模块划分可看到

在代码层次上:springMVC属于spring代码的一部分

在实际使用中:springMVC被独立为一个jar包,如不引用,将没有这个功能

故,可以将其看作两个类库

三、项目中主要用到的spring、springMVC功能

项目中主要用到的spring功能:

1、依赖注入(这里包括了bean的创建)

2、aop编程

3、事务管理

项目中主要用到的springMVC的功能:

1、创建控制器类

2、请求映射,根据URI(可不负责任的理解为相对路径)调用相应的action

3、数据绑定,如表单数据绑定为action参数

4、解析视图,向视图传递数据

项目中主要用到的spring功能和springMVC功能有重合部分

spring创建了所有的bean,包括控制器类,而springMVC也要创建控制器类

这里处理不好将会出现创建两次bean的情况,严重则会出现aop、事务失效

四、根据功能进行正确的spring、springMVC配置

spring负责bean的创建(@controller除外),bean依赖的管理、aop、事务、ORM的集成

(这里只开启注解事务,不配置事务管理器,因为根据ORM不同,事务管理器的选择也不同,所以事务管理器实际上放在ORM的配置文件中)

故配置文件applicationContext.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    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
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <!-- 加载系统配置文件 -->
     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:property/init.properties</value>
            </list>
        </property>
    </bean>
    
    <!-- 自动检测bean.不包括@Controller(交由mvc检测) -->
    <context:component-scan base-package="com.xjyh" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    
    <!-- 开启AOP -->
    <aop:aspectj-autoproxy expose-proxy="true"/>
    
    <!-- mybtis配置 -->
    <import resource="spring-mybatis.xml"/>
    
    <!-- 配置注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    
</beans>

而springMVC,负责了创建控制器类、请求映射、数据绑定、解析视图、向视图传递数据

故配置文件springmvc-servlet.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <!-- 启动扫描所有的controller -->
    <context:component-scan base-package="com.xjyh.controller"/>
    
    <!-- 
        自动注册DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter等多个bean,主要功能如下:
        1、根据@RequestMapping注解,处理请求映射 
        2、表单等数据绑定,支持自定义类型的数据绑定
        3、返回对象自动转换为json
    -->
    <mvc:annotation-driven />
    
    <!-- 静态资源访问 -->
    <mvc:resources mapping="/javascripts/**" location="/javascripts/"/>
    <mvc:resources mapping="/styles/**" location="/styles/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/files/**" location="/files/"/>
    <mvc:resources mapping="/templates/**" location="/templates/"/>
    
    <!-- 视图控制器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 使用JstlView -->
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- multipart数据解析器,使springMVC支持文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

</beans>

附:

mybaits负责配置数据源,扫描dao、mapper文件,加载spring模板

spring-mybatis.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!-- 配置dbcp数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="${jdbc.dbcp.dataSource.driverClassName}" />
        <property name="url" value="${jdbc.dbcp.dataSource.url}" />
        <property name="username" value="${jdbc.dbcp.dataSource.username}" />
        <property name="password" value="${jdbc.dbcp.dataSource.password}" />
        <!-- Connection Pooling DBCP -->
        <property name="initialSize" value="${jdbc.dbcp.dataSource.initialSize}" />
        <property name="maxActive" value="${jdbc.dbcp.dataSource.maxActive}" />
        <property name="maxIdle" value="${jdbc.dbcp.dataSource.maxIdle}" />
        <property name="maxWait" value="${jdbc.dbcp.dataSource.maxWait}" />
        <property name="poolPreparedStatements" value="${jdbc.dbcp.dataSource.poolPreparedStatements}" />
        <property name="defaultAutoCommit" value="${jdbc.dbcp.dataSource.defaultAutoCommit}" />
        <!-- 是否自动回收超时连接 -->
        <property name="removeAbandoned" value="${jdbc.dbcp.dataSource.removeAbandoned}" />
        <!--removeAbandonedTimeout: 超时时间(以秒数为单位)--> 
        <property name="removeAbandonedTimeout" value="180"/>  
    </bean>
    
    <!-- 对数据源进行事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- mybatis文件配置,扫描所有mapper文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:mybatis-conf.xml" />
            <property name="mapperLocations" value="classpath:mybatis-mapper/*.xml" />
            <property name="typeAliasesPackage" value="com.xjyh.entity" />  
    </bean>
    
    <!-- spring与mybatis整合配置,扫描所有dao -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.xjyh.dao"></property>
        <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"></property>
    </bean>
    
    <!--通过模板定制mybatis的行为 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"  >
        <constructor-arg index="0"  ref="sqlSessionFactory"/>
         <constructor-arg index="1" value="BATCH" />
    </bean>
    
</beans>

web.xml负责spring应用上下文加载、编码过滤、springMVC的DispatcherServlet注册

<?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">
    <display-name>Archetype Created Web Application</display-name>
    
    <!-- spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!-- 该xml文件亦可写到上方spring配置文件中 -->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</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>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>
原文地址:https://www.cnblogs.com/xjyh/p/4580217.html