<bean id="aspetbean" class="com.saly.service.MyInterceptor"/>
<aop:config>
<aop:aspect id="asp" ref="aspetbean">
<aop:pointcut id="mycut" expression="execution(* com.saly.service.impl.PersonServiceBean.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck" />
<aop:after-returning pointcut-ref="mycut" method="doAccessCheckAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
<aop:after pointcut-ref="mycut" method="doAccessCheckFinally"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
package com.saly.service;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 必须交给spring管理
* 注解方式:@Component
* 配置方式:<bean id="myInterceptor" class="com.saly.service.MyInterceptor">
* @author saly
*
*/
@Aspect
public class MyInterceptor {
/*
* @Pointcut("execution (* com.saly.service..*.*.(..))")
* 第一个※代表返回类型(任何类型),
* 第一个..对子包进行拦截,
* 第二个※对哪个类拦截(对所有类拦截),
* 第三个※代表所有方法,括号中的两个..代表参数任意
*
*/
//对所有方法进行拦截,参数任意
private void anyMethod(){}//声明一个切入点
public void doAccessCheck(){//只能拦截到参数为一个切是string类型的方法
System.out.println("前置通知");
}
//只能拦截返回值是string类型的方法
public void doAccessCheckAfter(){
System.out.println("后置通知");
}
public void doAccessCheckFinally(){
System.out.println("最终通知");
}
//列外信息作为参数传入到doAfterThrowing方法中
public void doAfterThrowing(){
System.out.println("例外通知");
}
//环绕通知,struts2做权限控制//可以替换以上其他方法
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
//if(){//判断用户是否在权限
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
//}
return result;
}
}
常见配置
(1)execution(java.lang.String com.saly.service.impl.PersonServiceBean.*(..))
拦截返回参数是string的所有方法
(2)execution(* com.saly.service.impl.PersonServiceBean.*(java.lang.String,..))
拦截第一个参数是string类型的所有方法
(3)execution(!void com.saly.service.impl.PersonServiceBean.*(..))
拦截所有返回类型非 void的所有方法
(4)execution(* com.saly.service..*.*(..))
对包和子包下的所有方法进行拦截
1304

被折叠的 条评论
为什么被折叠?



