spring-5-注解AOP-1

来源:互联网 发布:软件项目招标评分标准 编辑:程序博客网 时间:2024/06/06 13:58
import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class DaoAspectJ {//execution(<修饰符模式>?<返回值模式><方法名模式>(<参数模式>)<异常模式>?)//@Before在执行某个方法直线执行下面方法中的代码:System.out.println("我要提交事务了");//* save*(..)) 分别代表 返回值类型(*:表示任意) 方法名(save*:这是一个通配符写法,以save开头的方法都拦截) .. 表示方法参数个数任意//所以* save之间需要有空格 类似 void saveget()//@Before相当于前置  @AfterReturning相当于后置//1:方法切点函数,通过描述目标类方法信息定义连接点。execution(),@annotation()////2:方法入参切点函数,通过描述目标类方法入参的信息定义连接点。args() @args()////3:目标代理类切点函数:通过描述目标类型信息定义连接点。 within() @within() target()////4:代理类切点函数通过描述目标类的代理类的信息定义连接点。@target() this()//大致了解//@AspectJ支持三种通配符://*  :匹配任意字符,但它只能匹配上下文中的一个元素。//..  :匹配任意字符,可以匹配上下文中的多个元素,参数的代表//+  :  匹配指定类的所有类,必须跟在类后面,如:com.tz.aspectj.IUserService+//代表继承和扩展指定类的所有类,同时包括指定类本身。//@AspectJ函数按其是否支持通配符及支持的程度可分为以下3类://1:支持所有通配符的有:execution(),within(),如:within(com.tz.user.*)//2:仅支持+通配符的有:args(),this(),target(),如args(com.tz.User+)//3:不支持通配符的有:@args,@within(),@target(),@annotation()//逻辑运算符就用and or not@Before("execution(* save*(..))")public void bj(){System.out.println("我要提交事务了");}@AfterReturning("execution(* com.aop.spring.handerAnnotation.UserDao.save(..)) or execution(* com.aop.spring.handerAnnotation.CourseDao.save(..))")public void aj(){System.out.println("++事务提交完毕++");}}

原创粉丝点击