Spring第三天

来源:互联网 发布:interface php 编辑:程序博客网 时间:2024/06/01 08:24

Spring第二天回顾:

  1. 代理模式
  2. Aop编程
  3. Spring对Jdbc的支持
    JdbcTemplate

思考:

 - 程序的“事务控制",可以用aop实现!即只需要写一次,运行时候动态织入业务方法上。 - Spring提供了对事务的管理,开发者只需要按照Spring的方法做就行。

目标:

  1. Spring声明事务管理(xml方式,注解方式)
  2. Spring与HIbernate整合
  3. SSH整合

1.程序中事务控制

环境准备1.1

用户访问->Action->Service->Dao    一个业务的成功:调用Service是执行成功的,意味着Service中调用的所有dao是执行成功的。事务应该再service层统一控制.没有应用事务的代码模拟:    在Service中调用2次dao,希望其中一个dao执行失败,整个操作要回滚。开发步骤:    1.后台环境准备        数据库,表/entity/dao/service    2.dao的实现用jdbcTemplate    3.对象创建都由Spring容器完成

1.2事务控制概述

  • 编程事务控制

    自己手动手动控制事务,就叫做编程式事务控制。jdbc代码:    Conn.setAutoCommite(false);//设置手动控制事务Hibernate代码:    Session.beginTransaction(); //开启一个事务【细粒度的事务控制:可以对指定的方法,指定的方法的某几行添加事务控制】(比较灵活,但是开发起来比较繁琐:每次都要开启,提交,回滚。)  
  • 声明式事务控制

    Spring提供了对事务的管理,这个就叫事务管理。Spring提供了对事务控制的实现。用户如果想用Spring声明式管理,只需要在配置文件中配置即可,不想使用时直接移除配置。这个实现了对事务控制的最大程度的解耦.Spring声明式事务管理,核心实现就是基于Aop.【组力度的事务控制:只能给方法应用事务,不可以对方法的某几行应用事务】(因为aop拦截的是方法)

XML实现Spring声明式事务管理:

<?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:tx="http://www.springframework.org/schema/tx"       xmlns:aop="http://www.springframework.org/schema/aop"       xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans.xsd            http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop.xsd">    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="user" value="root"></property>        <property name="password" value="root"></property>        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>        <property name="initialPoolSize" value="3"></property>        <property name="maxPoolSize" value="5"></property>        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>        <property name="maxStatements" value="100"></property>        <property name="acquireIncrement" value="2"></property>    </bean>    <!--创建JdcbTemplate工具类实例-->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"></property>    </bean>    <!--创建dao实例-->    <bean id="deptDao" class="cn.itcast_02.a_tx.DeptDao">        <property name="jdbcTemplate" ref="jdbcTemplate"></property>    </bean>    <bean id="deptService" class="cn.itcast_02.a_tx.DeptService">        <property name="deptDao" ref="deptDao"></property>    </bean>    <!--##########Spring声明式事务管理配置############-->    <!--5.1配置事务管理器类-->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"></property>    </bean>    <!--5.2配置事务增强-->    <tx:advice transaction-manager="transactionManager" id="txAdvice">        <tx:attributes>            <tx:method name="*" read-only="false"/>            <tx:method name="get*" read-only="true"/>            <tx:method name="find*" read-only="true"/>        </tx:attributes>    </tx:advice>    <!--5.3Aop配置:拦截那些方法(切入点表达式)+应用上面的事务增强配置-->    <aop:config>        <aop:pointcut id="pt" expression="execution(* cn.itcast_02.a_tx.*(..))"/>        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>    </aop:config></beans>

注解方式实现Spring声明式事务管理:

步骤:    1)必须引入AOP相关的jar问价    2)bean.xml中制定注解方式实现声明式事务管理以及应用的事务管理器类    3)在需要添加事务控制的地方,写上:@Transacantional@Transactional注解:    1)应用事务的注解    2)定义到方法上:当前方法应用spring的声明式事务    3)定义到类上:当前类的所有方法都应用Spring声明式事务管理    4)定义到父类上:当执行父类的方法时候应用事务。

<?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:tx="http://www.springframework.org/schema/tx"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans.xsd            http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context.xsd default-autowire="byName ">    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="user" value="root"></property>        <property name="password" value="root"></property>        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>        <property name="initialPoolSize" value="3"></property>        <property name="maxPoolSize" value="5"></property>        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>        <property name="maxStatements" value="100"></property>        <property name="acquireIncrement" value="2"></property> </bean>    <!--创建JdcbTemplate工具类实例--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"></property>    </bean>    <!--事务管理器类-->    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"></bean>    <!--开启注解扫描-->    <context:component-scan base-package="cn.itcast_02.b_anno"></context:component-scan>    <!--注解方式实现事务:指定注解方法实现事务-->    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans>

package cn.itcast_02.b_anno;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Isolation;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;/** * Service * Created by 朱博文 on 2017/7/7. */@Servicepublic class DeptService {    //容器注入dao对象    @Resource    private DeptDao deptDao;    @Transactional(            readOnly = false,  //读写事务            timeout = -1,       //事务的超时时间不限制  (这个不一定起作用,最终由数据库底层决定)            noRollbackFor = ArithmeticException.class,   //遇到数学异常不回滚            isolation = Isolation.DEFAULT,         // 事务的隔离级别,数据库的默认            propagation = Propagation.REQUIRED //事务的传播行为        )    public void save(Dept dept) {        deptDao.sava(dept);    }}

事务转播行为:    progargation.REQUIRED        指定当前的方法必须在事务的环境下执行;        (如果当期那运行的方法,已经存在事务,就会加入当前的事务)    Propagation.REQUIRED_new        指定挡在的方法必须在事务的环境下运行;        (如果当前运行的方法,已经存在事务;事务会挂机;会始终开启一个新的事务,执行完后;刚才挂起的事务继续运行)

    举例:    Class Lod{    progargation.REQUIRED_NEW    insertLog();    }    progargation.REQUIRED        void saveDept(){        insertLog(); //始终开启事务,不会因为saveDept中的异常而停止        saveDept();

测试步骤:

1)日志表Log2)LogService.java     insertLog();

package cn.itcast_02.b_anno;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by 朱博文 on 2017/7/7. */public class App {    //了解容器的相关方法    @Test    public void testApp() throws Exception {        //1.根据bean.xml配置路径,创建容器对象//        ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast_02/b_anno/bean.xml");        //2.根据多个路径配置文件的路径,创建容器的对象//        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{});        //3.容器对象相关方法        ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast_02/b_anno/bean.xml");        //3.1从名字获取实例//        DeptService deptService = (DeptService) ac.getBean("deptService");        //3.2从类型获取实例//        DeptService deptervice = (DeptService) ac.getBean(DeptService.class);        //3.3泛型,不需要强转        DeptService deptervice =  ac.getBean("deptService",DeptService.class);        //3.4获取容器中bean对象的数量        int count = ac.getBeanDefinitionCount();        System.out.println();        String[] beanDefinitionNames = ac.getBeanDefinitionNames();        }}
原创粉丝点击