码迷,mamicode.com
首页 > 编程语言 > 详细

Spring 事务管理

时间:2015-05-13 22:10:09      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" //申明通知
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">//申明schema

	<!-- DispatcherServlet Context: defines this servlet‘s request-processing infrastructure -->
	
	<!--注解驱动 Enables the Spring MVC @Controller programming model -->    
    <!-- 需要访问数据库,所以引入事务管理 -->
    <bean id="userDao" class="me.chanjar.weixin.cp.dao.impl.UserinfoDaoimpl" >
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
<!-- 配置c3p0 sqlserver数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	   <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> 
	   <property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;databaseName=getsalary"></property>
	   <property name="user" value="sa"></property>
	   <property name="password" value="sa123"></property>
	</bean>
	<!-- 配置hibernate的sessionfactory bean 由spring 的localsessionfactorybean提供-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	  <!-- 配置数据源 -->
	  <property name="dataSource" ref="dataSource"></property>  
	  <!-- 配置hibernate的配置文件和名称 -->
	  <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	  <!-- 配置hibernate映射文件 -->
	  <property name="mappingLocations" value="classpath:me/chanjar/weixin/cp/po/*.hbm.xml"></property>
	</bean>
	<!-- 配置spring的事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	   <property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 通知配置事务属性,需要事务属性管理器transactionmanager -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<!-- 事务的传播属性 -->
	  <tx:attributes>
	  <!-- all methods starting with get are read-only -->
	    <tx:method name="get*"   read-only="true"/>
	    <tx:method name="query*" read-only="true"/>
	    <tx:method name="ad*"    propagation="REQUIRED"/>
	    <tx:method name="updat*" propagation="REQUIRED"/>
	    <tx:method name="del*"   propagation="REQUIRED"/>
	  </tx:attributes>
	</tx:advice>
	<!-- 配置事务的aop,并且把事务属性和切点关联起来 -->
	<aop:config>
	<!-- 配置切点 -execution(*返回值,impl文件下面的某个类的所有方法-->
	      <aop:pointcut expression="execution(* me.chanjar.weixin.cp.dao.impl.*.*(..))" id="pointcut" />
	      <aop:advisor  advice-ref="txAdvice" pointcut-ref="pointcut"/>
	</aop:config>
	
</beans><!--application-Context.xml-->

如上是application-Context.xml配置文件。下面看一下web.xml 

<?xml version="1.0" encoding="utf-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
<servlet>
<servlet-name>Test</servlet-name> 
<servlet-class>me.chanjar.weixin.cp.cpservlet.MpServlet</servlet-class>
</servlet> 
<servlet-mapping> 
<servlet-name>Test</servlet-name> 
<url-pattern>/justdoit</url-pattern> 
</servlet-mapping> 

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<filter>
		<filter-name>encodingFilter</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>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

如下是springmvc-servlet.xml的配置文件。知道为啥叫spring-mvc的配置文件吗?是因为我在web.xml配置文件里面说明了,dispatch-servlet 的名称为springmvc的原因吗????

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<!-- DispatcherServlet Context: defines this servlet‘s request-processing infrastructure -->
	
	<!--注解驱动 Enables the Spring MVC @Controller programming model -->

	<mvc:annotation-driven></mvc:annotation-driven>
		<!-- 扫描器 -->
    <context:component-scan base-package="me" />
  
	<!-- 配置视图解析器。Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- Spring分段文件上传所必须的 ,用于检查请求中是否包含multipart  
    see: http://www.html.org.cn/books/springReference/ch13s08.html  
    -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
          <property name="maxUploadSize" value="100000"/>
    </bean> 
    <!--添加spring mvc intercepters-->
     <mvc:interceptors>
      <bean class="me.chanjar.weixin.cp.Interceptor.MyHandlerInterceptor">
      </bean>
    </mvc:interceptors>
    <!-- 添加intercepters结束 -->
	<import resource="classpath:applicationContext.xml"/>
</beans><!--springmvc-servlet.xml-->

一、spring 事务申明步骤 

1.1 首先申明命名空间和schema

           首先需要引入aop 以及tx 的申明和schema

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" //申明通知
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">//申明schema

1.2 申明组件扫描的包路径

<context:componet-scan base-package=""/>

因为我在springmvc-servlet.xml文件里面申明了扫描包路径,并且将applicationContext.xml引入。所以就不需要在applicationContext.xml申明了。


Spring 事务管理

标签:

原文地址:http://my.oschina.net/u/2308739/blog/414516

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!