本框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3

开发工具:    myEclipse.

整个工程含完整jar包,可在这里下载: http://www.demodashi.com/demo/10217.html

整体目录结构如下图:

Spring+SpringMVC+Hibernate

最近在鼓捣SpringMVC框架,现将成果都记录下来,免得前学后忘。之前用的框架一直是S2SH,一直苦于要配置一堆的配置文件,自从接触SpringMVC,发现这才是我一直想要的框架,基于全注解,开发过程中零配置,实在快哉。此教程非常适合零基础的人学习回归正题,基于全注解驱动的SpringMVC+Spring4.2+hibernate4.3框架搭建(整合)过程如下,:

开发工具为myEclipse

第一步:新建一个web项目


第二步:加入所需的jar包

Spring+SpringMVC+Hibernate

第三步:接下来我们开始配置SpringMVC容器

为了分工明确,我们将SpringMVC的配置单独写在spring-servlet.xml里,Spring的配置写在spring-common.xml(事务、数据源、sessionFactory等等)里。

spring-common.xml和spring-servlet.xml先加入如下schemal

[html] view plain copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"  
  6.     xmlns:util="http://www.springframework.org/schema/util"  
  7.     xmlns:websocket="http://www.springframework.org/schema/websocket"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  9.                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  10.                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  11.                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  12.                     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  13.                     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
  14.                     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  15.                     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">  
然后spring容器的配置先放下,先来配置springMVC(spring-servlet.xml)的配置

在schemal的结尾处加入这一句:default-autowire="byName" ,依赖注入将根据name自动装配。

接下来启动注解驱动的SpringMVC功能:

[html] view plain copy
  1. <mvc:annotation-driven />  

扫描注解包(在SpringMVC的容器里,只扫描Controller注解就行了)

[html] view plain copy
  1. <context:component-scan base-package="com.mvc.rest"  
  2.         use-default-filters="false">  
  3.         <context:include-filter type="annotation"  
  4.             expression="org.springframework.stereotype.Controller" />  
  5.   </context:component-scan>  

use-default-filters默认为true,默认会扫描@Component、@Controller、@Repository、@Service的注解,在这里只扫描@Controller注解是因为,SpringMVC的容器没有事务的能力,所以扫描@Repository、@Service的注解只能放在Spring的容器。也正因为如此,事务的配置要写在Spring的容器。

然后是对模型视图名称的解析,在请求时模型视图名称添加前后缀(前缀是从控制器里返回的视图的父目录,此处配置的是让容器在WEB-INF/view/下找寻对应的视图;后缀是给视图名称追加后缀名,此处配置的是jsp后缀)

[html] view plain copy
  1. <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver"  p:prefix="/WEB-INF/view/" p:suffix=".jsp" />  

配置CommonsMultpartResolver,上传文件的时候要用到CommonsMultpartResolver,maxUploadSize设置上传文件的大小限制,上传文件必须先配置此解析器。

[html] view plain copy
  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  2.      <property name="maxUploadSize" value="10485760" />  
  3. </bean>  

配置login视图解析,在登录拦截器里,校验未登录的话,要跳转到登录页面,然后由于login页面放在WEB-INF目录下,所以设置跳转到login.jsp会跳转不过去,在此处设置如下,在返回此view-name的地方,容器便不会当作Controller的路径,当作视图的路径跳转,在拦截器里便可以跳转到login页面(此配置告诉容器,这不是一个controller的方法的路径,而是一个视图的名称,请当作视图处理)。

[html] view plain copy
  1. <mvc:view-controller path="/" view-name="login" />  

拦截器的配置也是放在SpringMVC的容器里,拦截器以后的文章里再详细解说。

到此spring-servlet.xml的配置就告一段落了,spring-servlet.xml的全文如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--suppress ALL -->  
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"  
  8.     xmlns:util="http://www.springframework.org/schema/util"  
  9.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  11.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  13.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  14.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
  15.         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  16.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"  
  17.     default-autowire="byName">  
  18.     <mvc:annotation-driven />  
  19.     <!-- controller包(自动注入) -->  
  20.     <context:component-scan base-package="com.mvc.rest" use-default-filters="false">  
  21.       <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
  22.     </context:component-scan>  
  23.     <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->  
  24.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  25.         p:prefix="/WEB-INF/view/" p:suffix=".jsp" />  
  26.     <mvc:view-controller path="/" view-name="login" />  
  27.     <bean id="multipartResolver"  
  28.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  29.         <property name="maxUploadSize" value="10485760" />  
  30.     </bean>  
  31.     <!-- 配置拦截器, 多个拦截器,顺序执行  
  32.         <mvc:interceptors>  
  33.            <mvc:interceptor>  
  34.             <mvc:mapping path="/*" />  
  35.            <bean class="com.mvc.rest.interceptor.CommonInterceptor"></bean>   
  36.      </mvc:interceptor> </mvc:interceptors> -->  
  37. </beans>  

第四步:我们配置web.xml

先配置CharacterEncodingFilter编码过滤器,此过滤器必须放在配置文件的最上面,有多个过滤器的时候,也应该放在第一位。encoding目标编码,forceEncoding设为true,会忽略请求来源的编码,强制使用encoding设置的编码。

[html] view plain copy
  1. <filter>  
  2.        <filter-name>CharacterEncodingFilter</filter-name>  
  3.        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  4.        <init-param>  
  5.            <param-name>encoding</param-name>  
  6.            <param-value>utf-8</param-value>  
  7.        </init-param>  
  8.        <init-param>  
  9.            <param-name>forceEncoding</param-name>  
  10.            <param-value>true</param-value>  
  11.        </init-param>  
  12.    </filter>  
  13.    <filter-mapping>  
  14.        <filter-name>CharacterEncodingFilter</filter-name>  
  15.        <url-pattern>/*</url-pattern>  
  16.    </filter-mapping>  

然后配置ContextLoaderListener,此监听用来加载我们写的配置文件

[html] view plain copy
  1. <listener>  
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3. </listener>  
然后加载Spring配置文件

[html] view plain copy
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>  
  4.         classpath*:/spring/spring-common.xml  
  5.     </param-value>  
  6. </context-param>  
接下来就是配置SpringMVC的核心Servlet,所有请求都要先经过DispatcherServlet,然后进行分发到对应的控制器。该Servlet须第一个被加载,且在初始化的时候去加载SpringMVC的配置文件——spring-servlet.xml

[html] view plain copy
  1. <servlet>  
  2.     <servlet-name>spring-mvc</servlet-name>  
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.     <init-param>  
  5.         <description>spring mvc</description>  
  6.         <param-name>contextConfigLocation</param-name>  
  7.         <param-value>classpath*:/spring/spring-servlet.xml</param-value>  
  8.     </init-param>  
  9.     <load-on-startup>1</load-on-startup>  
  10. </servlet>  
然后设置DispatcherServlet拦截的请求,此处的servlet-name,即是上面配置的DispatcherServlet的name,url-pattern设置为斜杠,则会拦截所有请求,也即静态资源html、css、js也直接请求。

[html] view plain copy
  1.   <servlet-mapping>  
  2. lt;servlet-name>spring-mvc</servlet-name>  
  3. <url-pattern>/</url-pattern>  
  4.    </servlet-mapping>  
为此,我们需要设置,哪些资源不进行拦截

[html] view plain copy
  1. <servlet-mapping>  
  2.       <servlet-name>default</servlet-name>  
  3.       <url-pattern>/html/*</url-pattern>  
  4.   </servlet-mapping>  
  5.  <servlet-mapping>  
  6.       <servlet-name>default</servlet-name>  
  7.       <url-pattern>/js/*</url-pattern>  
  8.   </servlet-mapping>  
  9.     <servlet-mapping>  
  10.  <servlet-name>default</servlet-name>  
  11.       <url-pattern>/css/*</url-pattern>  
  12.   </servlet-mapping>  
  13.      <servlet-mapping>  
  14.     <servlet-name>default</servlet-name>  
  15.       <url-pattern>/images/*</url-pattern>  
  16.   </servlet-mapping>  
到此,SpringMVC就可以正常使用了。

欢迎页面的设置,原本此处只能设置视图名,*.jsp或者*.html,因为在spring-servlet.xml里设置了视图解析:<mvc:view-controller path="/" view-name="login" />,所以,此处设置为welcome-file设置为login,容器便会将其解析为视图login.jsp,绕过WEB-INFO下的资源无法直接访问的限制。

[html] view plain copy
  1. <welcome-file-list>  
  2.      <welcome-file>login</welcome-file>  
  3.  </welcome-file-list>  
我们还可以设置error-page的页面

[html] view plain copy
  1. <error-page>  
  2.    <error-code>404</error-code>  
  3.    <location>/html/error/404.html</location>  
  4. </error-page>  
  5.  <error-page>  
  6.    <error-code>500</error-code>  
  7.    <location>/html/error/500.html</location>  
  8. </error-page>  
为了集成hibernate,我们还要配置OpenSessionInViewFilter,此过滤器会将Hibernate的Session和一次完整的请求过程绑定起来,事务控制,必须配置此过滤器。

[html] view plain copy
  1.  <filter>  
  2.     <filter-name>openSession</filter-name>  
  3.     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
  4.  </filter>  
  5.  <filter-mapping>  
  6. <filter-name>openSession</filter-name>  
  7. <url-pattern>/*</url-pattern>  
  8.  </filter-mapping>  

完整的web.xml的配置如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.     <filter>  
  7.         <filter-name>CharacterEncodingFilter</filter-name>  
  8.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  9.         <init-param>  
  10.             <param-name>encoding</param-name>  
  11.             <param-value>utf-8</param-value>  
  12.         </init-param>  
  13.         <init-param>  
  14.             <param-name>forceEncoding</param-name>  
  15.             <param-value>true</param-value>  
  16.         </init-param>  
  17.     </filter>  
  18.     <filter-mapping>  
  19.         <filter-name>CharacterEncodingFilter</filter-name>  
  20.         <url-pattern>/*</url-pattern>  
  21.     </filter-mapping>  
  22.     <listener>  
  23.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  24.     </listener>  
  25.     <context-param>  
  26.         <param-name>contextConfigLocation</param-name>  
  27.         <param-value>  
  28.             classpath*:/spring/spring-common.xml  
  29.         </param-value>  
  30.     </context-param>  
  31.     <servlet>  
  32.         <servlet-name>spring-mvc</servlet-name>  
  33.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  34.         <init-param>  
  35.             <description>spring mvc</description>  
  36.             <param-name>contextConfigLocation</param-name>  
  37.             <param-value>classpath*:/spring/spring-servlet.xml</param-value>  
  38.         </init-param>  
  39.         <load-on-startup>1</load-on-startup>  
  40.     </servlet>  
  41.     <filter>  
  42.         <filter-name>openSession</filter-name>  
  43.         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
  44.     </filter>  
  45.     <filter-mapping>  
  46.         <filter-name>openSession</filter-name>  
  47.         <url-pattern>/*</url-pattern>  
  48.     </filter-mapping>  
  49.     <servlet-mapping>  
  50.         <servlet-name>spring-mvc</servlet-name>  
  51.         <url-pattern>/</url-pattern>  
  52.     </servlet-mapping>  
  53.     <welcome-file-list>  
  54.         <welcome-file>login</welcome-file>  
  55.     </welcome-file-list>  
  56.     <servlet-mapping>  
  57.         <servlet-name>default</servlet-name>  
  58.         <url-pattern>/html/*</url-pattern>  
  59.     </servlet-mapping>  
  60.     <servlet-mapping>  
  61.         <servlet-name>default</servlet-name>  
  62.         <url-pattern>/js/*</url-pattern>  
  63.     </servlet-mapping>  
  64.     <servlet-mapping>  
  65.         <servlet-name>default</servlet-name>  
  66.         <url-pattern>/css/*</url-pattern>  
  67.     </servlet-mapping>  
  68.     <servlet-mapping>  
  69.         <servlet-name>default</servlet-name>  
  70.         <url-pattern>/images/*</url-pattern>  
  71.     </servlet-mapping>  
  72.     <error-page>  
  73.         <error-code>404</error-code>  
  74.         <location>/html/error/404.html</location>  
  75.     </error-page>  
  76.     <error-page>  
  77.         <error-code>500</error-code>  
  78.         <location>/html/error/500.html</location>  
  79.     </error-page>  
  80. </web-app>  

第五步:配置spring-common.xml(数据源、事务、sessionFactory)

 配置数据源jdbc.properties:

[html] view plain copy
  1. jdbc.driverClassName=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc\:mysql\://121.40.90.125\:3306/test  
  3. jdbc.username=root  
  4. jdbc.password=exceptoin882465\[email protected]\#  

解析properties:

[html] view plain copy
  1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2. <span style="font-family:Microsoft YaHei;">   </span><property name="locations">  
  3.     <value>classpath:jdbc.properties</value>  
  4. <span style="font-family:Microsoft YaHei;">   </span></property>  
  5. </bean>  
数据库连接池的配置取properties中的值:

[html] view plain copy
  1. <bean id="dataSource" destroy-method="close"<span style="font-family:Microsoft YaHei;">  </span>class="org.apache.commons.dbcp.BasicDataSource">  
  2.     <property name="driverClassName" value="${jdbc.driverClassName}" />  
  3.     <property name="url" value="${jdbc.url}" />  
  4.     <property name="username" value="${jdbc.username}" />  
  5.     <property name="password" value="${jdbc.password}" />  
  6. </bean>  


若不设置url的编码,在mysql数据库里,保存进去的中文会变成问号。

配置sessionFactory

[html] view plain copy
  1.  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  2. <property name="dataSource" ref="dataSource" />  
  3. <property name="hibernateProperties">  
  4.     <props>  
  5.             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  6.             <prop key="hibernate.hbm2ddl.auto">update</prop>  
  7.             <prop key="hibernate.show_sql">true</prop>  
  8.             <prop key="hibernate.format_sql">true</prop>  
  9.     </props>  
  10. </property>  
  11. <!-- 注解方式配置 -->  
  12. <property name="packagesToScan">  
  13.   <list>  
  14.     <value>com.mvc.rest.entity</value>  
  15.  </list>  
  16. </property>  
  17.  </bean>  
packagesToScan扫描我们的hibernate实体文件。

最后配置事务

[html] view plain copy
  1. <bean id="txManager"  
  2.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  3.         <property name="sessionFactory" ref="sessionFactory" />  
  4.     </bean>  
  5.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  6.         <tx:attributes>  
  7.             <tx:method name="save*" propagation="REQUIRED" />  
  8.             <tx:method name="add*" propagation="REQUIRED" />  
  9.             <tx:method name="edit*" propagation="REQUIRED" />  
  10.             <tx:method name="update*" propagation="REQUIRED" />  
  11.             <tx:method name="delete*" propagation="REQUIRED" />  
  12.             <tx:method name="register*" propagation="REQUIRED" />  
  13.             <tx:method name="all" propagation="REQUIRED" />  
  14.             <tx:method name="changePassword*" propagation="REQUIRED" />  
  15.             <tx:method name="restPassword*" propagation="REQUIRED" />  
  16.             <tx:method name="authorize*" propagation="REQUIRED" />  
  17.             <tx:method name="send*" propagation="REQUIRED" />  
  18.             <tx:method name="init*" propagation="REQUIRED" />  
  19.             <!-- <tx:method name="*" read-only="true"/> -->  
  20.         </tx:attributes>  
  21.     </tx:advice>  
  22.     <aop:config>  
  23.         <aop:pointcut id="serviceOperation"  
  24.             expression="execution(* com.mvc.rest.service.impl.*.*(..))" />  
  25.         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
  26. </aop:config>  
完整的spring-common.xml的配置如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--suppress ALL -->  
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"  
  8.     xmlns:util="http://www.springframework.org/schema/util"  
  9.     xmlns:websocket="http://www.springframework.org/schema/websocket"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  11.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  12.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  13.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  14.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  15.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
  16.         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  17.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">  
  18.     <context:component-scan base-package="com.mvc.rest" />  
  19.     <!-- properties文件解析器 -->  
  20.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  21.         <property name="locations">  
  22.             <value>classpath:jdbc.properties</value>  
  23.         </property>  
  24.     </bean>  
  25.     <!-- 配置数据源 -->  
  26.     <bean id="dataSource" destroy-method="close"  
  27.         class="org.apache.commons.dbcp.BasicDataSource">  
  28.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  29.         <property name="url" value="${jdbc.url}" />  
  30.         <property name="username" value="${jdbc.username}" />  
  31.         <property name="password" value="${jdbc.password}" />  
  32.     </bean>  
  33.     <bean id="sessionFactory"  
  34.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  35.         <property name="dataSource" ref="dataSource" />  
  36.         <property name="hibernateProperties">  
  37.             <props>  
  38.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  39.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  40.                 <prop key="hibernate.show_sql">true</prop>  
  41.                 <prop key="hibernate.format_sql">true</prop>  
  42.             </props>  
  43.         </property>  
  44.         <!-- 注解方式配置 -->  
  45.         <property name="packagesToScan">  
  46.             <list>  
  47.                 <value>com.mvc.rest.entity</value>  
  48.             </list>  
  49.         </property>  
  50.     </bean>  
  51.     <bean id="txManager"  
  52.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  53.         <property name="sessionFactory" ref="sessionFactory" />  
  54.     </bean>  
  55.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  56.         <tx:attributes>  
  57.             <tx:method name="save*" propagation="REQUIRED" />  
  58.             <tx:method name="add*" propagation="REQUIRED" />  
  59.             <tx:method name="edit*" propagation="REQUIRED" />  
  60.             <tx:method name="update*" propagation="REQUIRED" />  
  61.             <tx:method name="delete*" propagation="REQUIRED" />  
  62.             <tx:method name="register*" propagation="REQUIRED" />  
  63.             <tx:method name="all" propagation="REQUIRED" />  
  64.             <tx:method name="changePassword*" propagation="REQUIRED" />  
  65.             <tx:method name="restPassword*" propagation="REQUIRED" />  
  66.             <tx:method name="authorize*" propagation="REQUIRED" />  
  67.             <tx:method name="send*" propagation="REQUIRED" />  
  68.             <tx:method name="init*" propagation="REQUIRED" />  
  69.             <!-- <tx:method name="*" read-only="true"/> -->  
  70.         </tx:attributes>  
  71.     </tx:advice>  
  72.     <aop:config>  
  73.         <aop:pointcut id="serviceOperation"  
  74.             expression="execution(* com.mvc.rest.service.impl.*.*(..))" />  
  75.         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
  76.     </aop:config>  
  77. </beans>  

到此,基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建大功告成。

相关文章:

  • 2021-07-24
  • 2021-12-08
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-15
  • 2021-11-13
猜你喜欢
  • 2021-08-11
  • 2022-01-01
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2021-07-30
相关资源
相似解决方案