原文地址

http://michael-softtech.iteye.com/blog/650779

(1)使用ProxyFactoryBean的代理

Java代码

  1. package chapter4;

  2. public interface Performable {

  3. public void perform() throws Exception;

  4. }

  5. package chapter4;

  6. import java.util.Random;

  7. public class Artist implements Performable {

  8. public void perform() throws Exception {

  9. int num = new Random().nextInt(100);

  10. if(num >= 50) {

  11. throw new Exception(String.valueOf(num));

  12. } else {

  13. System.out.println(num);

  14. }

  15. }

  16. }

  17. package chapter4;

  18. public class Audience {

  19. public Audience() {

  20. }

  21. public void takeSeats() {

  22. System.out.println("The audience is taking their seats.");

  23. }

  24. public void turnOffCellPhones() {

  25. System.out.println("The audience is turning off " + "their cellphones");

  26. }

  27. public void applaud() {

  28. System.out.println("CLAP CLAP CLAP CLAP CLAP");

  29. }

  30. public void demandRefund() {

  31. System.out.println("Boo! We want our money back!");

  32. }

  33. }

  34. package chapter4;

  35. import java.lang.reflect.Method;

  36. import org.springframework.aop.AfterReturningAdvice;

  37. import org.springframework.aop.MethodBeforeAdvice;

  38. import org.springframework.aop.ThrowsAdvice;

  39. public class AudienceAdvice

  40. implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice {

  41. private Audience audience;

  42. public void setAudience(Audience audience) {

  43. this.audience = audience;

  44. }

  45. public void before(Method method, Object[] args, Object target)

  46. throws Throwable {

  47. audience.takeSeats();

  48. audience.turnOffCellPhones();

  49. }

  50. public void afterReturning(Object returnValue, Method method, Object[] args,

  51. Object target) throws Throwable {

  52. audience.applaud();

  53. }

  54. public void afterThrowing(Exception ex) {

  55. audience.demandRefund();

  56. }

  57. }

Xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:aop="http://www.springframework.org/schema/aop"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. 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">

  7. <bean id="audience" class="chapter4.Audience" />

  8. <bean id="audienceAdvice" class="chapter4.AudienceAdvice" >

  9. <property name="audience" ref="audience" />

  10. </bean>

  11. <bean id="audienceAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcu

    tAdvisor">

  12. <property name="advice" ref="audienceAdvice" />

  13. <property name="expression" value="execution(* *.perform(..))" />

  14. </bean>

  15. <bean id="artistTarget" class="chapter4.Artist" />

  16. <bean id="artist" class="org.springframework.aop.framework.ProxyFactoryBean" >

  17. <property name="target" ref="artistTarget" />

  18. <property name="interceptorNames" value="audienceAdvisor" />

  19. <property name="proxyInterfaces" value="chapter4.Performable" />

  20. </bean>

  21. </beans>

(2)隐式使用ProxyFactoryBean的aop代理

DefaultAdvisorAutoProxyCreator实现了BeanPostProcessor,它将自动检查advisor的pointcut是否匹配bean的方法,如果匹配会替换bean为一个proxy,并且应用其advice。

Xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:aop="http://www.springframework.org/schema/aop"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. 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">

  7. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyC

    reator" />

  8. <bean id="audience" class="chapter4.Audience" />

  9. <bean id="audienceAdvice" class="chapter4.AudienceAdvice" >

  10. <property name="audience" ref="audience" />

  11. </bean>

  12. <bean id="audienceAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcu

    tAdvisor">

  13. <property name="advice" ref="audienceAdvice" />

  14. <property name="expression" value="execution(* *.perform(..))" />

  15. </bean>

  16. <bean id="artist" class="chapter4.Artist" />

  17. </beans>

(3)使用注解的aop代理

xml中增加了一个<aop:aspectj-autoproxy />,它创建了AnnotationAwareAspectJAutoProxyCreator在spring中,这个类将自动代理匹配的类的放方法。和上个例子中DefaultAdvisorAutoProxyCreator做同样的工作。

Java代码

  1. package chapter4;

  2. import org.aspectj.lang.annotation.AfterReturning;

  3. import org.aspectj.lang.annotation.AfterThrowing;

  4. import org.aspectj.lang.annotation.Aspect;

  5. import org.aspectj.lang.annotation.Before;

  6. import org.aspectj.lang.annotation.Pointcut;

  7. @Aspect

  8. public class Audience {

  9. public Audience() {

  10. }

  11. @Pointcut("execution(* *.perform(..))")

  12. public void pointcut(){}

  13. @Before("pointcut()")

  14. public void takeSeats() {

  15. System.out.println("The audience is taking their seats.");

  16. }

  17. @Before("pointcut()")

  18. public void turnOffCellPhones() {

  19. System.out.println("The audience is turning off " + "their cellphones");

  20. }

  21. @AfterReturning("pointcut()")

  22. public void applaud() {

  23. System.out.println("CLAP CLAP CLAP CLAP CLAP");

  24. }

  25. @AfterThrowing("pointcut()")

  26. public void demandRefund() {

  27. System.out.println("Boo! We want our money back!");

  28. }

  29. }

Xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:aop="http://www.springframework.org/schema/aop"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. 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">

  7. <aop:aspectj-autoproxy />

  8. <bean id="audience" class="chapter4.Audience" />

  9. <bean id="artist" class="chapter4.Artist" />

  10. </beans>

(4)使用aop配置文件的自动代理

采用这种方法,不用加<aop:aspectj-autoproxy />

Java代码

  1. package chapter4;

  2. import org.aspectj.lang.annotation.Aspect;

  3. @Aspect

  4. public class Audience {

  5. public Audience() {

  6. }

  7. public void pointcut() {

  8. }

  9. public void takeSeats() {

  10. System.out.println("The audience is taking their seats.");

  11. }

  12. public void turnOffCellPhones() {

  13. System.out.println("The audience is turning off " + "their cellphones");

  14. }

  15. public void applaud() {

  16. System.out.println("CLAP CLAP CLAP CLAP CLAP");

  17. }

  18. public void demandRefund() {

  19. System.out.println("Boo! We want our money back!");

  20. }

  21. }

Xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:aop="http://www.springframework.org/schema/aop"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. 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">

  7. <bean id="audience" class="chapter4.Audience" />

  8. <aop:config>

  9. <aop:aspect ref="audience">

  10. <aop:before method="takeSeats" pointcut="execution(* *.perform(..))" />

  11. <aop:before method="turnOffCellPhones" pointcut="execution(* *.perform(..))" />

  12. <aop:after-returning method="applaud" pointcut="execution(* *.perform(..))" />

  13. <aop:after-throwing method="demandRefund" pointcut="execution(* *.perform(..))" />

  14. </aop:aspect>

  15. </aop:config>

  16. <bean id="artist" class="chapter4.Artist" />

  17. </beans>

AOP实现方法的更多相关文章

  1. JS实现AOP拦截方法调用

    //JS实现AOP拦截方法调用function jsAOP(obj,handlers) {    if(typeof obj == 'function'){        obj = obj.prot ...

  2. AOP记录方法的执行时间

    作用AOP监控方法的运行时间如下: @Component @Aspect public class LogAop { private Logger log = LoggerFactory.getLog ...

  3. 动态代理AOP实现方法过滤

    上一节实现了动态代理,接下来 有时候,我不需要在每一个方法都要记录日志,做权限验证 等等. 所有就有了这样的需求.AOP实现特定方法过滤,有选择性的来对方法实现AOP 拦截.就是本节标题所示. 举个例 ...

  4. AOP获取方法注解实现动态切换数据源

    AOP获取方法注解实现动态切换数据源(以下方式尚未经过测试,仅提供思路) ------ 自定义一个用于切换数据源的注解: package com.xxx.annotation; import org. ...

  5. 使用Spring Aop验证方法参数是否合法

    先定义两个注解类ValidateGroup 和 ValidateFiled ValidateGroup .java package com.zf.ann; import java.lang.annot ...

  6. 使用AOP实现方法执行时间和自定义注解

    环境:IDEA2018+JDK1.8+SpringBoot 第一步:在pom文件中引入依赖(度娘有很多(*^▽^*)): <!--引入AOP的依赖--><dependency> ...

  7. (一)七种AOP实现方法

    在这里列表了我想到的在你的应用程序中加入AOP支持的所有方法.这里最主要的焦点是拦截,因为一旦有了拦截其它的事情都是细节. Approach 方法 Advantages 优点 Disadvantage ...

  8. JAVA动态代理和方法拦截(使用CGLib实现AOP、方法拦截、委托)

    AOP用CGLib更简便.更可控. 动态代理的实现非常优雅. 实体类: public class SampleClass { public String MyFunction1(String inpu ...

  9. AOP 增强方法

    Spring AOP 提供了 5 种类型的通知,它们分别是 Before Advice(前置通知).After Returning Advice(后置通知).Interception Around A ...

随机推荐

  1. [转] JavaScript 原型理解与创建对象应用

    这段时间把之前的 JavaScript 的笔记复习了一遍,又学习了一些新的内容,所以把自己的学习笔记加上个人理解在这里总结一下,并提供一个简单的应用示例,希望能帮助一些刚入门的朋友.主 要参考< ...

  2. iOS中__block 关键字的底层实现原理

    在 <iOS面试题集锦(附答案)> 中有这样一道题目: 在block内如何修改block外部变量?(38题)答案如下: 默认情况下,在block中访问的外部变量是复制过去的,即:写操作不对 ...

  3. java中关于public class

    在编写类的时候可以使用两种方式定义类:     public class定义类:     class定义类: 1,如果一个类声明的时候使用了public class进行了声明,则类名称必须与文件名称完 ...

  4. sqlite使用blob类型存储/访问 结构体

    /* open fire host and slora report data database */ int open_report_db(void) { ; char sql[SQL_COMMAN ...

  5. javascript操作json方法

    /*新增json的对象属性*/ var json = {}; json["subjectName"] = "ddd"; json.teacherlist = & ...

  6. junit 测试注解

    * @Test: 将一个 普通的方法修饰成为一个测试方法* @BeforeClass: 他会在所有的方法运行前被执行,static修饰* @AfterClass 他会在所有方法运行结束后被执行,sta ...

  7. php生成不重复随机字符串

    使用时间戳作为原始字符串,再随机生成五个字符随机插入任意位置,生成新的字符串,保证不重复 function rand($len) { $chars='ABCDEFGHIJKLMNOPQRSTUVWXY ...

  8. webUploader上传组件 实际运用小结

    WebUploader组件实际介绍: 官网:http://fex.baidu.com/webuploader/doc/index.html 组件优势及优化总结:http://itindex.net/d ...

  9. 关于一点coding.net与git配合在AndroidStudio/Idea上的使用笔记个的

    编写程序的我们经常需要对我们写的代码做版本控制,或者分支管理,具备类似功能的软件很多,诸如SVN,Git,CVS等等!但配置版本控制服务器(SVN server etc.)是繁琐的并且需要一定的成本! ...

  10. SlidingMenu侧换菜单的导入

    对于Adt-22.3有一种使用SlidingMenu(侧滑菜单的方式),直接加你放到lib文件夹下