使用Aspectj进行AOP开发

来源:互联网 发布:js分割数字 编辑:程序博客网 时间:2024/05/21 14:55

添加类库: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类:

package www.csdn.spring.advice.aspetjs;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class AdviceImpl {@Before(value = "execution(* UserDaoImpl.*(..))")public void doTransAction(){System.out.println("----开启事务-----");}@After(value = "execution(* www.csdn..UserDaoImpl.*(..))")public void doAfterTransAction(){System.out.println("-------提交事务-------");}@Around(value = "execution(* www.csdn..UserDaoImpl.save(..))")public void doSec(ProceedingJoinPoint jp){System.out.println("----------安全处理之前-------------");try {Object obj = jp.proceed();} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("----------安全处理之后-------------");}}


配置文件:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"   xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       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           "> <!-- 通知 --> <bean id="adviceImpl" class="www.csdn.spring.advice.aspetjs.AdviceImpl"/>  <!-- 配置bean --> <bean id="userDaoImpl" class="www.csdn.spring.advice.aspetjs.UserDaoImpl"/>  <aop:aspectj-autoproxy/></beans>


测试类:

package www.csdn.spring.advice.aspetjs;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AspetjsTest {@Testpublic void test(){ApplicationContext context=new ClassPathXmlApplicationContext("spring-asp*.xml");UserDao uDao=(UserDao) context.getBean("userDaoImpl");uDao.save(null);/*uDao.delete(null);try{uDao.update(null);}catch(Exception e){}uDao.getObjects();*/}}


例子说明:在配置文件中出现的UserDaoImpl可以自己在相应的包中自己建,最好是新建UserServiceImpl为好。

原创粉丝点击