spring aop应用场景

1.事务处理:

	
        
            
        
    
    
    
        
                
          

2.监控

  1. package com.albert.spring.proxy;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5.   
  6. public class Interceptor  
  7. {  
  8.     public void before()  
  9.     {  
  10.         System.out.println("前置通知");  
  11.     }  
  12.       
  13.     public void afterReturning()  
  14.     {  
  15.         System.out.println("後置通知");  
  16.     }  
  17.       
  18.     public void beforeAdd(JoinPoint jp)  
  19.     {  
  20.         System.out.println("前置通知,獲取類名:" + jp.getTarget().getClass().getName());  
  21.         System.out.println("前置通知,獲取方法名:" + jp.getSignature().getName());  
  22.         System.out.print("前置通知,獲取參數:");  
  23.         for (Object obj : jp.getArgs())  
  24.         {  
  25.             System.out.print(obj + "\t");  
  26.         }  
  27.     }  
  28.       
  29.     public void afterReturningRes(String result)  
  30.     {  
  31.         System.out.println("後置通知,返回結果:" + result);  
  32.     }  
  33.       
  34.     public void after()  
  35.     {  
  36.         System.out.println("最終通知");  
  37.     }  
  38.       
  39.     public void AfterThrowing()  
  40.     {  
  41.         System.out.println("例外通知");  
  42.     }  
  43.       
  44.     public void catchException(Exception e)  
  45.     {  
  46.         System.out.println("獲取拋出的異常:" + e);  
  47.     }  
  48.       
  49.     public Object around(ProceedingJoinPoint pjp)  
  50.         throws Throwable  
  51.     {  
  52.         System.out.println("進入環繞");  
  53.         // if(){ // 進行一些判斷,再執行環繞Object result = pjp.proceed();  
  54.         // }  
  55.         System.out.println("退出環繞");  
  56.         return result;  
  57.     }  
  58. }  

 

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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.            http://www.springframework.org/schema/aop  
  10.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  11.     <aop:aspectj-autoproxy />  
  12.     <bean id="userDaoImp" class="com.albert.spring.proxy.aop.UserDaoImp" />  
  13.     <bean id="aspectBean" class="com.albert.spring.proxy.aop.Interceptor" />  
  14.     <aop:config>  
  15.       
  16.         <aop:aspect id="asp" ref="aspectBean">  
  17.           
  18.             <aop:pointcut id="thecut"  
  19.                 expression="execution(* com.albert.spring.proxy.aop.UserDaoImp.*(..))" />  
  20.             <aop:after-returning pointcut-ref="thecut"  
  21.                 method="afterReturningRes" returning="result" />  
  22.             <aop:around pointcut-ref="thecut" method="around" />  
  23.             <aop:after-throwing pointcut-ref="thecut" method="catchException"  
  24.                 throwing="e" />  
  25.             <aop:after pointcut-ref="thecut" method="after" />  
  26.             <aop:before pointcut-ref="thecut" method="before" />  
  27.             <aop:before pointcut-ref="thecut" method="beforeAdd" />  
  28.         aop:aspect>  
  29.     aop:config>  
  30.   
  31. beans>  


你可能感兴趣的:(java)