1.首先这是我几天来对切面的编程的理解,之前有稍微学了下 spring切面的编程,spring中事物处理常常与pointcut相结合。
pointcut的注解类型 表达式 我就不多说了 。具体可以看spring文档的第199页~200页,都比较简单。
大体上是这样的 注解 + (表达标签+表达式格式)
如: @Pointcut (value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
@ 注解( value=“ 表达标签 ( 表达式格式)”)
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class NotVeryUsefulAspect {
@AfterReturning(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
private void logReceiver(){
System.out.println("切入点logReceiver...");
}
@Pointcut(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..)) && args(param)")
private void pointcut(String param){
System.out.println("切入点pointcut()"+param);
}
//方法体将不执行
@Pointcut("within(com.cn.spring.aspectj.*)")
public String inWebLayer() {
System.out.println("切入点inWebLayer()");
return "返回值加载";
}
@Before(value="inWebLayer()")
private void beforeinWebLayer(){
System.out.println("beforeinWebLayer~~");
}
@Before(value="pointcut(param)")
private void beforePointcut(String param){
System.out.println("beforePointcut:"+param);
}
@AfterReturning(pointcut="inWebLayer()",returning="retVal")
public void doAccessCheck(Object retVal) {
System.out.println("doAccessCheck:"+retVal);
}
@Around(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
private Object aroundLayer(ProceedingJoinPoint pjp) throws Throwable{
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
System.out.println("aroundLayer~~");
return retVal;
}
}
以上声明需要注意:spring基本包不提供Aspect包,需要另外引入,可以去eclipse官网下载:http://www.eclipse.org/aspectj/。
@Around 判断是否执行 以上的拦截 ,第一个参数必须ProceedingJoinPoint. 如要拦截:
@Around(value="execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(..))")
private Object aroundLayer(ProceedingJoinPoint pjp) throws Throwable{
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
System.out.println("aroundLayer~~");
return retVal;
}
就代表 需要执行之后的拦截 ,此拦截 在@Before 之前 做逻辑判断。