spring入门(10)---使用Aspectj进行AOP开发

添加类库:aspectjrt.jar和aspectjweaver.jar
添加aop schema.
定义xml元素:<aop:aspectj-autoproxy>
编写java类,并用@Aspect注解成通知
     AspectJ 支持 5 种类型的通知注解:
  @Before: 前置通知, 在方法执行之前执行
  @After: 后置通知, 在方法执行之后执行
  @AfterReturning: 返回通知, 在方法返回结果之后执行
  @AfterThrowing: 异常通知, 在方法抛出异常之后
  @Around: 环绕通知, 围绕着方法执行
配置成普通bean元素即可.

 

二、

后置通知:@After
    @After("execution(* *..WelcomeService.*(..))")
    public void applaud(){..}
后置通知在目标方法执行完成之后执行.一个切面aspect包含很多通知.
@After
    后置通知表明目标方法执行完之后,不论是否抛异常,都会织入该通知.
@AfterReturning
    方法返回后通知只在目标方法返回以后执行,若抛异常不执行.

@AfterReturning(pointcut="",returning="res")
public void xxx(Joinput jp,Object res)
在AfterReturning
通知中可接收到返回值.res即是用来接收返回值的对象.

三、

环绕通知:@Around
    @Around("execution(* *..WelcomeService.*(..))")
    public void around(ProceedingPointCut jp){..}
注意:可以控制目标方法是否调用,以及返回完全不同的对象,要慎用.

指定优先级:
@Aspect
@Order(0)
public class xxx{...}
加上@Order注解可以指定加入切面的优先级(先后顺序,值越小,优先级越高)

例子说明:


AdviceImpl类:

[java] view plain copy print ?
  1. package www.csdn.spring.advice.aspetjs;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.After;  
  5. import org.aspectj.lang.annotation.Around;  
  6. import org.aspectj.lang.annotation.Aspect;  
  7. import org.aspectj.lang.annotation.Before;  
  8.   
  9. @Aspect  
  10. public class AdviceImpl {  
  11.   
  12.     @Before(value = "execution(* UserDaoImpl.*(..))")  
  13.     public void doTransAction(){  
  14.         System.out.println("----开启事务-----");  
  15.     }  
  16.       
  17.     @After(value = "execution(* www.csdn..UserDaoImpl.*(..))")  
  18.     public void doAfterTransAction(){  
  19.         System.out.println("-------提交事务-------");  
  20.     }  
  21.       
  22.     @Around(value = "execution(* www.csdn..UserDaoImpl.save(..))")  
  23.     public void doSec(ProceedingJoinPoint jp){  
  24.         System.out.println("----------安全处理之前-------------");  
  25.         try {  
  26.             Object obj = jp.proceed();  
  27.         } catch (Throwable e) {  
  28.             // TODO Auto-generated catch block   
  29.             e.printStackTrace();  
  30.         }  
  31.         System.out.println("----------安全处理之后-------------");  
  32.     }  
  33. }  


配置文件:

[java] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.            http://www.springframework.org/schema/beans/spring-beans.xsd   
  7.            http://www.springframework.org/schema/aop   
  8.            http://www.springframework.org/schema/aop/spring-aop.xsd   
  9.            ">  
  10.   
  11.  <!-- 通知 -->  
  12.  <bean id="adviceImpl" class="www.csdn.spring.advice.aspetjs.AdviceImpl"/>  
  13.    
  14.  <!-- 配置bean -->  
  15.  <bean id="userDaoImpl" class="www.csdn.spring.advice.aspetjs.UserDaoImpl"/>  
  16.    
  17.  <aop:aspectj-autoproxy/>  
  18. </beans>  


测试类:

[java] view plain copy print ?
  1. package www.csdn.spring.advice.aspetjs;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class AspetjsTest {  
  8.   
  9.     @Test  
  10.     public void test(){  
  11.         ApplicationContext context=new ClassPathXmlApplicationContext("spring-asp*.xml");  
  12.         UserDao uDao=(UserDao) context.getBean("userDaoImpl");  
  13.         uDao.save(null);  
  14.         /*uDao.delete(null); 
  15.         try{ 
  16.             uDao.update(null); 
  17.         }catch(Exception e){ 
  18.              
  19.         } 
  20.         uDao.getObjects();*/  
  21.     }  
  22. }

你可能感兴趣的:(spring入门(10)---使用Aspectj进行AOP开发)