`

使用Spring的声明式事务----AOP方式

阅读更多

类似于上一篇日志,这里使用AOP的方式来配置。

工程结构:

大部分代码跟上一个工程类似。

 

服务类StudentService.java代码如下:

package com.mysrc.service;

import java.sql.Date;
import java.util.List;

import com.mysrc.dao.StudentDao;
import com.mysrc.entity.Student;

public class StudentService {
	private StudentDao dao;

	public void setDao(StudentDao dao) {
		this.dao = dao;
	}

	public void doComplexLogic() {

		// select
		List<Student> list = dao.getAllStudent();
		for (Student student : list) {
			System.out.println(student);
		}

		// update
		Student student = list.get(0);
		student.setName("laohu..");
		dao.updateStudent(student);
		System.out.println("did update temporarily...");

		//int a = 9 / 0; // 遇到异常,整个事务回滚,也即上面的update不会成功
		// 如果try catch捕获这个异常,那整个事务会顺利执行,不会回滚
		
		int b = 2;
		if (b > 1) {
			throw new CustomRuntimeException();
			// 事务不会回滚,也就是上面的update操作会提交
		}

		// insert
		student = new Student();
		student.setName("hello");
		student.setBirth(new Date(354778));
		student.setScore(78.9f);
		dao.addStudent(student);
		System.out.println("did insert...");

		// delete
		dao.deleteStudent(3);
		System.out.println("did delete...");
	}

}

 这里doComplexLogic()方法上不再有注解修饰了。

 

Spring的应用程序上下文配置文件applicationContext.xml的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd"
	xmlns:tx="http://www.springframework.org/schema/tx">

	<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url"
			value="jdbc:mysql://127.0.0.1:3306/mytestdb?characterEncoding=utf8" />
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
		<property name="maxActive" value="100" />
		<property name="maxIdle" value="30" />
		<property name="maxWait" value="1000" />
		<property name="validationQuery" value="select 1" />
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg name="dataSource" ref="basicDataSource">
		</constructor-arg>
	</bean>
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager ">
		<property name="dataSource">
			<ref bean="basicDataSource" />
		</property>
	</bean>

	<bean id="studentDao" class="com.mysrc.dao.StudentDao">
		<property name="jdbcTemplate">
			<ref bean="jdbcTemplate" />
		</property>
	</bean>

	<bean id="studentService" class="com.mysrc.service.StudentService">
		<property name="dao">
			<ref bean="studentDao" />
		</property>
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="NOT_SUPPORTED"
				read-only="true" />
			<tx:method name="doComplexLogic" propagation="NESTED"
				isolation="REPEATABLE_READ" timeout="1000" rollback-for="java.lang.Exception"
				no-rollback-for="com.mysrc.service.CustomRuntimeException" />
		</tx:attributes>
	</tx:advice>

	<!-- Spring AOP config -->
	<aop:config>
		<!-- 切入点 -->
		<aop:pointcut id="studentServicesPointcut"
			expression="execution(* com.mysrc.service.StudentService.*(..))" />
		<!-- <aop:pointcut id="newServicesPointcut2" expression="execution(* com.model.*.*(..))" 
			/> -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="studentServicesPointcut" />
		<!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="newServicesPointcut2" 
			/> -->
	</aop:config>

</beans>

 
 整个eclipse工程文件在附件中。。

分享到:
评论

相关推荐

    Spring框架-AOP和声明式事务

    Spring框架-AOP和声明式事务

    spring声明式事务配置

    &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...aop-2.5.xsd"&gt;

    Spring2.5和Hibernate3集成--学习spring aop ioc

    采用声明式事务 1.声明式事务的配置 * 配置sessionFactory * 配置事务管理器 * 配置事务的传播特性 * 配置哪些类哪些方法使用事务 2.编写业务逻辑方法 * 继承HibernateDaoSupport类,使用this....

    Hibernate编程式事务与Spring Aop的声明式事务(spring与hibernate集成)

    NULL 博文链接:https://quicker.iteye.com/blog/674029

    Spring ax/aop声明式事务配置实例

    Spring ax/aop声明式事务配置实例

    深入理解Spring声明式事务:源码分析与应用实践

    Spring框架的声明式事务管理是Java开发中的核心特性,它为高效且可靠的数据操作提供了强大支持。...深入理解Spring声明式事务的工作原理,不仅能帮助开发者更高效地使用Spring框架,也是提升Java企业级开发能力的关键。

    spring2.0声明式事务

    spring声明式事务的配置 3. spring2.0配置事务 a) 将spring 1.2升级到spring2.0 i. 去掉spring1.2相关的包 ii. 添加spring2.0的jar包:spring.jar,aspecjrt.jar,aspectjweaver.jar 和cglib-nodep-2.1.3,jar iii. ...

    spring-framework-3.0.0.M4-with-docs

    org.springframework.transaction-3.0.0.M4.jar: 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理 org.springframework.web.servlet-3.0.0.M4.jar: SpringMVC org.springframework.jms-3.0.0.M4...

    spring声明式事务管理

    1.掌握Myeclipse的使用。 2.掌握spring框架和hibernate框架的使用。 3. 掌握整合spring和hibernate的持久化操作编程 4.掌握基于AOP的声明式事务编程...3.配置WEB-INF/applicationContext.xml提供基于AOP的声明式事务

    Spring3配置声明式事务

    在Spring3中配置声明式事务比早期版本显得更加简便。只需要几行配置文件+注解就可以实现面向切面的AOP事务

    spring AOP(声明式事务管理)小程序

    用spring AOP(包括几种常用的通知类型)做的小程序

    MyBatis Spring声明式事务管理示例代码

    Spring的声明式事务管理是采用AOP(Aspect-Oriented Programming,面向切面编程)实现的。在编程式事务管理中,各事务处理代码实际上是相似的,这就造成了代码重复;而且编程式事务管理会造成事务管理代码和被管理的...

    spring源码分析(1-10)

    Spring源代码解析(六):Spring声明式事务处理 Spring源代码解析(七):Spring AOP中对拦截器调用的实现 Spring源代码解析(八):Spring驱动Hibernate的实现 Spring源代码解析(九):Spring Acegi框架鉴权的实现 ...

    Spring声明式事务配置管理方法

    发布于2013-5-6项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring2.0AOP类库即可。添加方法:点击项目右键-&gt;BuildPath-&gt;Addlibrarys:打开AddLibraries对话框,然后选定...

    Spring添加声明式事务.docx

    Spring提供了声明式事务处理机制,它基于AOP实现,无须编写任何事务管理代码,所有的工作全在配置文件中完成。 二、声明式事务的XML配置方式 为业务方法配置事务切面,需要用到tx和aop两个命名空间下的标签,先在...

    spring 3.0 jar 所有开发包及开发项目实例

    org.springframework.transaction-3.0.0.M4.jar: 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理 org.springframework.web.servlet-3.0.0.M4.jar: SpringMVC org.springframework.jms-3.0.0.M4...

    Spring框架资料,Ioc容器, AOP面向切面编程 ,声明式事务 ,Spring5新特性

    Spring框架资料,Ioc容器, AOP面向切面编程 ,声明式事务 ,Spring5新特性

    spring4示例代码

    spring-1 演示了使用setter方式及构造器方式创建bean,util:list标签创建集合,p标签简化配置 和依赖注入, 以及bean的autowire和继承与依赖,以及bean的作用域...spring-5 演示了声明式事务及使用xml配置文件处理事务

Global site tag (gtag.js) - Google Analytics