AOP实现方法

 

原文地址

 

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>

 

posted on 2016-01-25 16:53  wzyxidian  阅读(207)  评论(0编辑  收藏  举报

导航