Spring框架_事物管理器的配置

来源:互联网 发布:成都知美术馆怎么去 编辑:程序博客网 时间:2024/06/05 16:03

2.Spring的事务管理器配置总体浏览:配置spring事务管理器的三个角色:创建一个事物管理器(利用Spring的自动装备的知识点)<bean id=”” class=””/>---------利用spring提供的事务管理器即可开启事物管理的方法有哪些以及方法的权限(需要引入tx的dtd文件)<tx:advice><tx:attributes><tx:method></tx:method></tx:attributes></tx:advice>那些类的哪些方法开启那个事务管理器<aop:config>标签2.1.步骤一:创建一个Spring的事务管理器<!-- 配置一个事物管理器bean:只是为了对类的初始化:相当于new了一个类 --><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"/>2.2.步骤二:设置那些方法需要事务管理及权限<!-- 制定哪些方法需要开启事物(是事物管理)name="需要执行事物管理器的方法名"propagetion:必须的rollback-for:定义出现什么异常之后rollback --><tx:advice transaction-manager="transactionManager" id="txAdvice"><tx:attributes><tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/><tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/><tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/><!--其他方法只读  --><tx:method name="*" read-only="true"/></tx:attributes></tx:advice>2.3.步骤三:关联事务管理器和执行的类(添加横切面)<aop:config><!--  定义哪些类调用那个处理器  --><aop:pointcut expression="execution(* com.bjsxt.lc.service.impl.*.*(..))" id="pointcut"/><!--  将事物管理器和横切面关联 --><aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/></aop:config>2.4.附录:dtd的头文件:<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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="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.xsd         http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd"default-autowire="byName"><!-- 开启自动装配 -->2.5.注意:如果使用的是hibernate5需要将数据源的访问权交给spring<!--  配置数据源(当我们使用hibernate5并开启事物管理的时候,就必须将配置数据源的权利付给Spring来管理)如果使用以前的hibernate不需要配置此选项--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName"  value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/ssh"/><property name="username" value="root"/><property name="password" value="root"/></bean>分析:为什么bean的id是dataSource如果将数据源交给spring来管理,在执行时会报错,提示一个dataSource这个类找不到,需要用到Spring的自动装配来new出这个类,以提供给Spring来接管数据源为什么标签的name是:driverClassName,url,username,password进入原码,查看全局变量,你需要用Spring的赋值,而变量名为该2.6.Spring如何将hibernate的其他配置文件交给Spring来管理<!--  开启Spring的 hibernate的支持 --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><!-- 将hibernate的配置文件信息放到Spring中 --><property name="hibernateProperties"><props><!-- 官方语言 --><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- 打印sql语句 --><prop key="hibernate.show_sql">true</prop><!--  sql格式化  --><prop key="hibernate.format_sql">true</prop></props></property><!-- 将hibernate的映射文件配置到Spring文件中 --><property name="mappingResources"><array><value>com/bjsxt/lc/pojo/AAdmins.hbm.xml</value></array></property>hibernateProperties:该属性是为hiernate的全局属性赋值,也是从原码里找到相应的变量(set)方法并且。该属性时一个properties的文件,需要用properties的属性赋值mappingResources该属性是引入pojo的配置文件(获取一样)


1 0
原创粉丝点击