<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/xxx?serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="xxxxxx"></property> </bean> <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate" scope="prototype"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="accountDao" class="com.czy.dao.impl.AccountDaoImpl"> <property name="template" ref="template"></property> </bean> <bean id="accountService" class="com.czy.service.impl.AccountServiceImpl"> <property name="dao" ref="accountDao"></property> </bean> <!-- spring中基于xml的声明式事务控制配置步骤 1.配置事务管理器 2.配置事务的通知 此时我们需要导入事务的约束:tx的名称空间和约束,同时也需要aop的 使用tx:advice标签配置事务通知 属性: id:给事务通知起唯一标志 transaction-manager:给事务通知提供一个事务管理器引用 3.配置aop中的通用切入点表达式 4.建立事务通知和切入点表达式的对应关系 5.配置事务的属性 是在事务的通知tx:advice标签的内部 --> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 配置事务的属性 isolation:用于指定事务的隔离级别。默认是DEFAULT,表示使用数据库的默认隔离级别 propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。 read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写 timeout:用于指定事务的超时时间。默认值是-1,表示永不超时。如果指定了数值,以秒为单位。 rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值,表示任何异常都回滚 no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,表示任何异常都回滚 --> <tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="false"/> <!--查询方法没有事务且为只读模式--> <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method> </tx:attributes> </tx:advice> <!--配置aop--> <aop:config> <!--配置切入点表达式--> <aop:pointcut id="pt1" expression="execution(* com.czy.service.impl.*.*(..))"/> <!--建立切入点表达式和事务通知的对应关系--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor> </aop:config>
<!--为注解的类提供动态代理--> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> </beans>
标签:xml,控制,事务,Spring,基于,声明 来源: https://www.cnblogs.com/czy-algorithm/p/16183004.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。