>> 命名切点
下面是一个命名切点定义类
package com.springzoo.aspectj.advanced;
import org.aspectj.lang.annotation.Pointcut;
public class TestNamePointcut {
@Pointcut("within(com.springzoo.*)")
private void inPackage(){}
@Pointcut("execution(* greetTo(..)))")
protected void greetTo(){}
@Pointcut("inPackage() and greetTo()")
public void inPkgGreetTo(){}
}
然后使用这个:
@Aspect
public class TestAspect {
//-------------复合运算----------
// @Before("!target(com.springzoo.NaiveWaiter) "+
// "&& execution(* serveTo(..)))")
// public void notServeInNaiveWaiter() {
// System.out.println("--notServeInNaiveWaiter() executed!--");
// }
// @After("within(com.springzoo.*) "
// + " && execution(* greetTo(..)))")
// public void greeToFun() {
// System.out.println("--greeToFun() executed!--");
// }
//
// @AfterReturning("target(com.springzoo.Waiter) || "+
// " target(com.springzoo.Seller)")
// public void waiterOrSeller(){
// System.out.println("--waiterOrSeller() executed!--");
// }
// //------------引用命名切点----------//
// @Before("TestNamePointcut.inPkgGreetTo()")
// public void pkgGreetTo(){
// System.out.println("--pkgGreetTo() executed!--");
// }
//
// @Before("!target(com.springzoo.NaiveWaiter) && "
// +"TestNamePointcut.inPkgGreetTo()")
// public void pkgGreetToNotNaiveWaiter(){
// System.out.println("--pkgGreetToNotNaiveWaiter() executed!--");
// }
//
//------------访问连接点对象----------//
@Around("execution(* greetTo(..)) && target(com.springzoo.NaiveWaiter)")
public void joinPointAccess(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("------joinPointAccess-------");
System.out.println("args[0]:"+pjp.getArgs()[0]);
System.out.println("signature:"+pjp.getTarget().getClass());
pjp.proceed();
System.out.println("-------joinPointAccess-------");
}
//
// //------------绑定连接点参数----------//
// @Before("target(com.springzoo.NaiveWaiter) && args(name,num,..)")
// public void bindJoinPointParams(int num,String name){
// System.out.println("----bindJoinPointParams()----");
// System.out.println("name:"+name);
// System.out.println("num:"+num);
// System.out.println("----bindJoinPointParams()----");
// }
//------------绑定代理对象----------//
// @Before("execution(* greetTo(..)) && this(waiter)")
// @Before("this(waiter)")
// public void bindProxyObj(Waiter waiter){
// System.out.println("----bindProxyObj()----");
// System.out.println(waiter.getClass().getName());
// System.out.println("----bindProxyObj()----");
// }
//------------绑定类标注对象----------//
// @Before("@within(m)")
// public void bindTypeAnnoObject(Monitorable m){
// System.out.println("----bindTypeAnnoObject()----");
// System.out.println(m.getClass().getName());
// System.out.println("----bindTypeAnnoObject()----");
// }
//------------绑定抛出的异常----------//
// @AfterReturning(value="target(com.springzoo.SmartSeller)",returning="retVal")
// public void bingReturnValue(int retVal){
// System.out.println("----bingReturnValue()----");
// System.out.println("returnValue:"+retVal);
// System.out.println("----bingReturnValue()----");
// }
// //------------绑定抛出的异常----------//
// @AfterThrowing(value="target(com.springzoo.SmartSeller)",throwing="iae")
// public void bindException(IllegalArgumentException iae){
// System.out.println("----bindException()----");
// System.out.println("exception:"+iae.getMessage());
// System.out.println("----bindException()----");
// }
}
>> 访问连接点信息
AspectJ使用org.aspcetj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象。
有几个主要方法:
* java.lang.Object[] getArgs():获取连接点方法运行时的入参列表
* Signature getSignature():获取连接点的方法签名对象
* java.lang.Object getTarget():获取连接点所在的目标对象
* java.lang.Object getThis():获取代理对象本身
而ProceedingJoinPoint继承自JoinPoint,它新增两个用于执行连接点方法的方法:
* java.lang.Object proceed() throws Throwable:通过反射执行目标对象的连接点处的方法
* java.lang.Object proceed(java.lang.Object[] args) throws Throwable:通过反射执行连接点处方法,不过传入了新的参数。
本人博客已搬家,新地址为:http://yidao620c.github.io/
相关推荐
一个基于@AspectJ的spring2.0 AOP应用实例,很小很简单,没有任何额外信息,最适合AOP入门学习。使用log4j打印信息。把项目直接import进myeclipse就可以使用啦......
NULL 博文链接:https://snowolf.iteye.com/blog/1481442
Spring4 In Action-4.2-@AspectJ-切面,Spring4 In Action-4.2-@AspectJ-切面。Spring4 In Action-4.2-@AspectJ-切面
@AspectJ配置Spring AOP,文档,Aspect jar包, 可运行的demo,
NULL 博文链接:https://samter.iteye.com/blog/410618
上文我们已讲完五种通知Advice注解,所以从本文开始介绍@Pointcut切点表达式,虽然Spring AOP的切点表达式尚未 全部实现 @AspectJ 的切点指示符,但是也已经足够多和灵活,本文主要介绍@Pointcut切点表达式中的@...
NULL 博文链接:https://rain1109.iteye.com/blog/1838100
NULL 博文链接:https://moshow.iteye.com/blog/1613947
NULL 博文链接:https://quicker.iteye.com/blog/670885
pring源代码各个模块作用 核心模块: 1 spring-core:核心模块 依赖注入IOC和DI的最基本实现 spring-beans:Bean工厂与装配 spring-context:上下文,即IOC...spring-aspects:集成AspectJ,Aop应用框架 spring-instrume
aspectj
Spring spectJ AOP 前置通知 后置通知 返回通知 异常通知 环绕通知
博文链接:https://shaqiang32.iteye.com/blog/201914
Spring2.5开发 如果我们用到了AOP的话 一定需要aspectjrt.jar + aspectjweaver.jar 两个jar文件 这里面提供了 导入这两个就可以进行AOP开发了
这是进行spring-framework的AOP开发时需要的jar包,解压缩其中lib文件夹,lib文件夹中有4个重要的jar包:aspectjrt.jar,aspectjtools.jar,aspectjweaver.jar,org.aspectj.matcher.jar,解压缩后把这4个jar包导入...
spring通过aspectj来实现事务控制