Spring AOP 之 HelloWorld(简单例子)

java代码 Java代码   收藏代码

  1. import org.aspectj.lang.JoinPoint;  
  2. import org.aspectj.lang.ProceedingJoinPoint;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class AopTest {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         ApplicationContext ctx = new ClassPathXmlApplicationContext("benx.xml");  
  13.         HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");  
  14.         helloWorld.sayHelloWorld();  
  15.     }  
  16.   
  17. }  
  18.   
  19. interface HelloWorld {  
  20.     void sayHelloWorld();  
  21. }  
  22.   
  23. interface HelloChina {  
  24.     void sayHelloChina();  
  25. }  
  26.   
  27. class HelloWorldImpl implements HelloWorld {  
  28.   
  29.     public void sayHelloWorld() {  
  30.         System.out.println("Hello World!");  
  31.         //制造异常  
  32.         String str = null;  
  33.         str.substring(1);  
  34.   
  35.     }  
  36. }  
  37.   
  38. /** 
  39.  * 日志拦截器 
  40.  *  
  41.  * @author jin.xiong 
  42.  *  
  43.  */  
  44. class LogAdvice {  
  45.   
  46.     /** 
  47.      * 执行方法前拦截器 
  48.      *  
  49.      * @param joinPoint 
  50.      */  
  51.     public void methodBefore(JoinPoint joinPoint) {  
  52.         System.out.println(joinPoint.getTarget().getClass().getName() + "."  
  53.                 + joinPoint.getSignature().getName() + " Start");  
  54.     }  
  55.   
  56.     /** 
  57.      * 方法执行后拦截器 
  58.      *  
  59.      * @param joinPoint 
  60.      */  
  61.     public void methodAfter(JoinPoint joinPoint) {  
  62.         System.out.println(joinPoint.getTarget().getClass().getName() + "."  
  63.                 + joinPoint.getSignature().getName() + " end");  
  64.     }  
  65.   
  66.     /** 
  67.      * 方法出现异常拦截器 
  68.      *  
  69.      * @param joinPoint 
  70.      */  
  71.     public void methodException(JoinPoint joinPoint) {  
  72.         System.out.println(joinPoint.getTarget().getClass().getName() + "."  
  73.                 + joinPoint.getSignature().getName() + " mett Error");  
  74.     }  
  75.   
  76.     /** 
  77.      * 方法环绕拦截器,如果使用了这个,可以忽视上面的方法 
  78.      * 注意该方法参数为ProceedingJoinPoint ,这是可以执行的,只有round可以使用 
  79.      * @param joinPoint 
  80.      * @return 
  81.      */  
  82.     public Object methodRound(ProceedingJoinPoint joinPoint) {  
  83.   
  84.         methodBefore(joinPoint);  
  85.         Object ob = null;  
  86.         try {  
  87.             ob = joinPoint.proceed();  
  88.         } catch (Throwable error) {  
  89.             methodException(joinPoint);  
  90.         }  
  91.         methodAfter(joinPoint);  
  92.         return ob;  
  93.     }  
  94.   
  95. }  
benx.xml Java代码   收藏代码
  1. "1.0" encoding="UTF-8"?>  
  2. "http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  6.           http://www.springframework.org/schema/aop  
  7.             http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  8.               
  9.               
  10.     "helloWorld" class="HelloWorldImpl"/>  
  11.       
  12.     "logAdvice" class="LogAdvice"/>  
  13.       
  14.       
  15.         "logAdvice">  
  16.             "logPointcut" expression="execution(* *.*(..))" />  
  17.             "methodBefore" pointcut-ref="logPointcut" />  
  18.             "methodAfter" pointcut-ref="logPointcut" />  
  19.             "methodException" pointcut-ref="logPointcut" />  
  20.               
  21.               
  22.             "methodRound" pointcut-ref="logPointcut" />  
  23.           
  24.       
  25.   
 

打印结果,之所以会重复,使用为使用了round,可以把benx.xml中的methodRound去掉 Java代码   收藏代码
  1. HelloWorldImpl.sayHelloWorld Start  
  2. HelloWorldImpl.sayHelloWorld Start  
  3. Hello World!  
  4. HelloWorldImpl.sayHelloWorld end  
  5. HelloWorldImpl.sayHelloWorld mett Error  
  6. HelloWorldImpl.sayHelloWorld mett Error  
  7. HelloWorldImpl.sayHelloWorld end  
 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部