- 浏览: 148812 次
- 性别:
- 来自: 彩虹之巅
最新评论
-
qq970836452:
[url][img][url][url][img][url][ ...
spring中配置声明式事务的几种方式 -
qq970836452:
[img][/img]
spring中配置声明式事务的几种方式 -
qq970836452:
[*]
spring中配置声明式事务的几种方式 -
qq970836452:
[img]||[/img][img][/img][url][u ...
spring中配置声明式事务的几种方式 -
qq970836452:
[img][/img][img][/img][url][url ...
spring中配置声明式事务的几种方式
部分例子摘自 spring in action
(1)使用ProxyFactoryBean的代理
package chapter4; public interface Performable { public void perform() throws Exception; } package chapter4; import java.util.Random; public class Artist implements Performable { public void perform() throws Exception { int num = new Random().nextInt(100); if(num >= 50) { throw new Exception(String.valueOf(num)); } else { System.out.println(num); } } } package chapter4; public class Audience { public Audience() { } public void takeSeats() { System.out.println("The audience is taking their seats."); } public void turnOffCellPhones() { System.out.println("The audience is turning off " + "their cellphones"); } public void applaud() { System.out.println("CLAP CLAP CLAP CLAP CLAP"); } public void demandRefund() { System.out.println("Boo! We want our money back!"); } } package chapter4; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.ThrowsAdvice; public class AudienceAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice { private Audience audience; public void setAudience(Audience audience) { this.audience = audience; } public void before(Method method, Object[] args, Object target) throws Throwable { audience.takeSeats(); audience.turnOffCellPhones(); } public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { audience.applaud(); } public void afterThrowing(Exception ex) { audience.demandRefund(); } }
<?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 http://www.springframework.org/schema/beans/spring-beans.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"> <bean id="audience" class="chapter4.Audience" /> <bean id="audienceAdvice" class="chapter4.AudienceAdvice" > <property name="audience" ref="audience" /> </bean> <bean id="audienceAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor"> <property name="advice" ref="audienceAdvice" /> <property name="expression" value="execution(* *.perform(..))" /> </bean> <bean id="artistTarget" class="chapter4.Artist" /> <bean id="artist" class="org.springframework.aop.framework.ProxyFactoryBean" > <property name="target" ref="artistTarget" /> <property name="interceptorNames" value="audienceAdvisor" /> <property name="proxyInterfaces" value="chapter4.Performable" /> </bean> </beans>
(2)隐式使用ProxyFactoryBean的aop代理
DefaultAdvisorAutoProxyCreator实现了BeanPostProcessor,它将自动检查advisor的pointcut是否匹配bean的方法,如果匹配会替换bean为一个proxy,并且应用其advice。
<?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 http://www.springframework.org/schema/beans/spring-beans.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"> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> <bean id="audience" class="chapter4.Audience" /> <bean id="audienceAdvice" class="chapter4.AudienceAdvice" > <property name="audience" ref="audience" /> </bean> <bean id="audienceAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor"> <property name="advice" ref="audienceAdvice" /> <property name="expression" value="execution(* *.perform(..))" /> </bean> <bean id="artist" class="chapter4.Artist" /> </beans>
(3)使用注解的aop代理
xml中增加了一个<aop:aspectj-autoproxy />,它创建了AnnotationAwareAspectJAutoProxyCreator在spring中,这个类将自动代理匹配的类的放方法。和上个例子中DefaultAdvisorAutoProxyCreator做同样的工作。
package chapter4; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class Audience { public Audience() { } @Pointcut("execution(* *.perform(..))") public void pointcut(){} @Before("pointcut()") public void takeSeats() { System.out.println("The audience is taking their seats."); } @Before("pointcut()") public void turnOffCellPhones() { System.out.println("The audience is turning off " + "their cellphones"); } @AfterReturning("pointcut()") public void applaud() { System.out.println("CLAP CLAP CLAP CLAP CLAP"); } @AfterThrowing("pointcut()") public void demandRefund() { System.out.println("Boo! We want our money back!"); } }
<?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 http://www.springframework.org/schema/beans/spring-beans.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"> <aop:aspectj-autoproxy /> <bean id="audience" class="chapter4.Audience" /> <bean id="artist" class="chapter4.Artist" /> </beans>
(4)使用aop配置文件的自动代理
采用这种方法,不用加<aop:aspectj-autoproxy />
package chapter4; import org.aspectj.lang.annotation.Aspect; @Aspect public class Audience { public Audience() { } public void pointcut() { } public void takeSeats() { System.out.println("The audience is taking their seats."); } public void turnOffCellPhones() { System.out.println("The audience is turning off " + "their cellphones"); } public void applaud() { System.out.println("CLAP CLAP CLAP CLAP CLAP"); } public void demandRefund() { System.out.println("Boo! We want our money back!"); } }
<?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 http://www.springframework.org/schema/beans/spring-beans.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"> <bean id="audience" class="chapter4.Audience" /> <aop:config> <aop:aspect ref="audience"> <aop:before method="takeSeats" pointcut="execution(* *.perform(..))" /> <aop:before method="turnOffCellPhones" pointcut="execution(* *.perform(..))" /> <aop:after-returning method="applaud" pointcut="execution(* *.perform(..))" /> <aop:after-throwing method="demandRefund" pointcut="execution(* *.perform(..))" /> </aop:aspect> </aop:config> <bean id="artist" class="chapter4.Artist" /> </beans>
发表评论
-
spring中配置声明式事务的几种方式
2009-06-30 18:31 2848(1)通过TransactionProxyFactoryBea ... -
hibernate实战(第2版)读书笔记(一)
2009-05-28 09:38 3034最近时间重读hibernate实战(第2版),哈哈,很长时间不 ... -
SWT中使用Opengl画圆的例子
2009-03-09 22:26 5346import org.eclipse.opengl.GL; i ... -
Windows下VC6配置Opengl开发环境
2009-03-05 08:14 4728最近需要了解下Opengl,于是开始鼓捣Opengl,从网上下 ... -
用SWT写的简单的通讯录
2009-01-19 12:32 1916用SWT写了个简单的通讯录,管理自己的手机号, -
用SWT做的简单的文件浏览器
2009-01-03 17:55 2841一个用swt做的简单的文件浏览器, package hell ... -
hibernate3 one-ton-one总结之关联表关联实现one-to-one
2008-11-08 19:29 1589在hibernate3中,one-to-one关联主要通过3种 ... -
hibernate3 one-to-one总结之主外键关联
2008-11-08 19:21 2567在hibernate3中,one-to-one关联主要通过3种 ... -
hibernate3 one-to-one总结之共享的关联主键
2008-11-08 19:14 2340在hibernate3中,one-to-one关联主要通过3种 ... -
hibernate3中使用hsql的sequence生成主键
2008-10-25 09:58 3024今天,想尝试下使用hsql的sequence来作为hibern ... -
js dtree的简单应用
2008-09-25 19:54 3273前几天在javaeye上看到有这样一个帖子(http://ww ... -
怎么样让ibatis原生支持C3P0
2008-09-03 08:02 2341昨天,在javaeye上看到一个帖子,http://www.i ... -
在IE和Firefox都通用的弹出对话框
2008-08-15 06:44 4402test.html <!DOCTYPE html PUB ... -
JAVA版 俄罗斯方块
2008-07-26 11:00 3722把以前写的程序拿出来晒晒 -
JAVA版 连连看
2008-07-07 20:26 6296以前写的连连看,大家共同玩下。 -
彻底解决超链接提交中文乱码问题
2008-04-26 11:08 26962在网页中的超链接,例如 <a href="/s ... -
使用JAVA在JSP中生成饼图
2008-03-25 16:27 3787通过在服务器端生成图片 然后这样在页面中的图片可以真正的跨浏览 ... -
JSP 分页
2008-02-26 12:59 2858package examples; import java. ... -
设计模式之 Visitor
2007-12-12 14:27 1365package designpattern; pub ... -
设计模式之 Intercepting Filter
2007-12-12 14:13 2356package filter; public int ...
相关推荐
主要对Spring AOP的相关概念和简单的静态代理、动态代理以及常见的几种AOP配置方式做总结学习。主要包括:1. AOP的常见概念 2. 静态代理 3. jdk动态代理 4. Aspectj and Aspectjweaver 5. **aop-config** 6. CGLIB ...
有关于Spring,我们最常用的两个功能就是IOC和AOP,前几篇文章从源码级别介绍了Spring容器如何为我们生成bean及bean之间的依赖关系... 确实,Spring也就是通过这两种方式来实现AOP相关功能,下面就通过源码来简单求证下
spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...
IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性详解(包含单例设计模式),DI详解,Spring的几种注入方式,利用Spring简化Mybatis;第二天内容:AOP(AOP常用概念、Spring的三种aop实现方式、代理...
SpringAOP发展到现在出现的全部3种配置方式。由于Spring强大的向后兼容性,实际代码中往往会出现很多配置混杂的情况,而且居然还能工作,本文希望帮助大家理清楚这些知识。我们先来把它们的概念和关系说说清楚。AOP...
传播特性有几种?7种; 某一个事务嵌套另一个事务的时候怎么办? REQUIRED_NEW和REQUIRED区别 Spring的事务是如何回滚的,实现原理; 抽象类和接口的区别,什么时候用抽象类什么时候用接口; StringBuilder和...
- Spring依赖注入的方式有几种? - 一个bean的定义包含了什么?(BeanDefinition) - bean的作用域有哪些? - Spring 的扩展点主要有哪些? - Spring如何解决循环依赖? - 事务的传播行为是什么?有哪些? - 什么是AOP...
25. 解释 Spring 支持的几种 bean 的作用域 26. Spring 框架中的单例 bean 是线程安全的吗? 27. 解释 Spring 框架中 bean 的生命周期 28. 哪些是重要的 bean 生命周期方法? 你能重载它们吗? 29. 什么是 Spring ...
从代理机制初探 AOP 动态代理 <br>AOP 观念与术语 Spring AOP Advices Advices 包括了Aspect 的真正逻辑,由于缝合至Targets的时机不同,Spring 提供了几种不同的 Advices。 Before ...
4.3.1 为Spring切面创建自动代理 4.3.2 自动代理@AspectJ切面 4.4 定义纯粹的POJO切面 4.5 注入AspectJ切面 4.6 小结 第二部分 企业Spring 第5章 使用数据库 5.1 Spring的数据访问哲学 5.1.1 了解Spring数据...
4.3.1 为Spring切面创建自动代理 4.3.2 自动代理@AspectJ切面 4.4 定义纯粹的POJO切面 4.5 注入AspectJ切面 4.6 小结 第二部分 企业Spring 第5章 使用数据库 5.1 Spring的数据访问哲学 5.1.1 了解Spring数据...
本部分代码仅仅提供一种功能演示,如果实际应用仍需进一步完善(建议使用一些成型的Ioc框架,例如Spring.net或Castle等)。经过改造后 的系统,组件间依赖关系如下图: 可以看出这次实现了真正的“针对接口编程”...
而代理是一种基本的设计模式,它是一种为了提供额外的或不同的操作而插入到真 实对象中的某个对象。而Java的动态代理在代理上更进一步,既能动态的创建代理对象,又能动态的调用代理 方法。Java的反射和动态代理机制...
16.5spring中带有dwr的支持ajax的应用程序 16.5.1直接web远程控制 16.5.2访问spring管理的beandwr 16.6小结 附录a装配spring a.1下载spring a.1.1研究spring发布 a.1.2构建自己的类路径 a.2把spring添加为一...
由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中: 代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...
多线程几种实现方法,同步? 答:多线程有两种实现方法,一种是继承Thread类或者实现Runnable接口。同步就是在方法返回类型后面加上synchronized。 c#中的委托,事件是不是委托? 答:委托就是将方法作为一个参数...
15.java 中 IO 流分为几种? 16.BIO、NIO、AIO 有什么区别? 17.Files的常用方法都有哪些? 二、容器 18.java 容器都有哪些? 19.Collection 和 Collections 有什么区别? 20.List、Set、Map 之间的区别是什么? 21....
15. java 中 IO 流分为几种? 7 16. BIO、NIO、AIO 有什么区别? 7 17. Files的常用方法都有哪些? 8 二、容器 8 18. java 容器都有哪些? 8 19. Collection 和 Collections 有什么区别? 9 20. List、Set、Map 之间...
14. spring的bean配置的几种方式 15. web.xml的配置 16. spring的监听器。 17. zookeeper的实现机制,有缓存,如何存储注册服务的 18. IO会阻塞吗?readLine是不是阻塞的 19. 用过spring的线程池还是java的线程池? ...
关于 null 的几种处理方式大小写敏感 null 是任何引用类型的初始值 null 只是-种特殊的值使用 Null-Safe 方法null 判断 关于思维导图 Java.IO Java.lang Java.math Java.net Java 基础核心总结 V2.0 IO 传统的 ...