您的位置:首页 > 其它

08 vim编辑

2015-07-23 10:17 281 查看
面向切面编程,代码如下:

@Aspect
public class ControllorAspectj {

@Pointcut("within(com.spring.test.action.*)")
public void doManagerActionClass() {
}

@Before("execution(public * * (..))")
public void getUser(JoinPoint jp) throws IOException {

System.out.println("任意公共方法的执行");
}

@Pointcut("execution(* do*(..))")
public void doMethod(){
}

@Before(value="com.spring.test.common.ControllorAspectj.doMethod() && target(bean)",argNames="bean")
public void beforeDo(JoinPoint jp,Object bean) throws Exception {
String sourceLocation=jp.getKind();

Signature signature=jp.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
method.invoke(jp.getTarget(),jp.getArgs());
System.out.println("方法名:"+method.getName());
System.out.println("任意以do开头的方法"+sourceLocation);
}

@Pointcut("within(com.spring.test..*)")
public void before3(){
System.out.println("test包以及他的子包");
}

@AfterThrowing(pointcut="com.spring.test.common.ControllorAspectj.before3()",throwing="ex")
public void afterThrows(JoinPoint jp,Throwable ex) {
ex.printStackTrace();
System.out.println("test包以及他的子包抛出的异常"+ex.getMessage());
}

@AfterReturning("this(com.spring.test.IBaseAction)")
public void afterReturn(){
System.out.println("after return,实现了IBaseAction");
}

}


注:任何通知方法可以将第一个参数定义为org.aspectj.lang.JoinPoint类型 (环绕通知需要定义第一个参数为ProceedingJoinPoint类型, 它是 JoinPoint 的一个子类)。

以上注意几点:
1、该类声明为@Aspect;
2、在spring配置文件中声明:
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="controllerAspect"
class="com.spring.test.common.ControllorAspectj"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: