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. <?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" 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.     <bean id="helloWorld" class="HelloWorldImpl"/>  
  11.       
  12.     <bean id="logAdvice" class="LogAdvice"/>  
  13.       
  14.     <aop:config>  
  15.         <aop:aspect ref="logAdvice">  
  16.             <aop:pointcut id="logPointcut" expression="execution(* *.*(..))" />  
  17.             <aop:before method="methodBefore" pointcut-ref="logPointcut" />  
  18.             <aop:after method="methodAfter" pointcut-ref="logPointcut" />  
  19.             <aop:after-throwing method="methodException" pointcut-ref="logPointcut" />  
  20.               
  21.             <!-- aroun最好不要和他们同时使用 -->  
  22.             <aop:around method="methodRound" pointcut-ref="logPointcut" />  
  23.         </aop:aspect>  
  24.     </aop:config>  
  25. </beans>  
 


打印结果,之所以会重复,使用为使用了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  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值