spring aop 拦截目标方法

package com.skss.portal.service;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
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;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.skss.portal.entity.TbRole;

/**
* spring aop中 @Around和@AfterThrowing 不兼容,这是spring自身的bug
*/
@Component("serviceAspect")
// 申明这是一个组件
@Aspect
// 申明这是一个切面
public class ServiceAspect implements ThrowsAdvice {
//配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
@Pointcut("execution(* com.skss.portal.service..*(..))")
public void aspect() {
};
/*
* 配置前置通知,使用在方法aspect()上注册的切入点
* 同时接受JoinPoint切入点对象,可以没有该参数
*/
@Before("aspect()")
public void before(JoinPoint joinPoint) {
System.out.println("aspect before");

}
//配置后置通知,使用在方法aspect()上注册的切入点
@After("aspect()")
public void after(JoinPoint joinPoint) {
System.out.println("aspect after");

}

// 配置后置返回通知,使用在方法aspect()上注册的切入点
@AfterReturning("aspect()")
public void afterReturn(JoinPoint joinPoint) {
System.out.println("aspect afterReturn");

}

// 配置抛出异常后通知,使用在方法aspect()上注册的切入点
@AfterThrowing(pointcut = "aspect()", throwing = "ex")
public void afterThrow(JoinPoint joinPoint, Exception ex) {
System.out.println("aspect afterThrow....:" + ex);
}

/*
* public void afterThrowing(Method method, Object[] arts, Object target,
* RuntimeException throwable) { System.out.println("have exception"); }
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(
"com/skss/portal/config/applicationContext.xml");
RoleService roleAction = (RoleService) app
.getBean("service.roleService");
roleAction.tt();

}
}

你可能感兴趣的:(spring aop,拦截)