spring aop 两种方式

纯注解方式

@Aspect
@Component
public class LogAop {
	  @Pointcut("within(@org.springframework.stereotype.Controller *)")
	  public void cutController() {
	    System.out.println("cut----");
	  }
	  @Around("cutController()")
	  public Object recordSysLog(ProceedingJoinPoint point) throws Throwable  {
		  
		 String strMethodName = point.getSignature().getName();
	        String strClassName = point.getTarget().getClass().getName();
	        Object[] params = point.getArgs();
	        StringBuffer bfParams = new StringBuffer();
	        Enumeration<String> paraNames = null;
	        HttpServletRequest request = null;
	        if (params != null && params.length > 0) {
	            request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	            paraNames = request.getParameterNames();
	            String key;
	            String value;
	            while (paraNames.hasMoreElements()) {
	                key = paraNames.nextElement();
	                value = request.getParameter(key);
	                bfParams.append(key).append("=").append(value).append("&");
	            }
	            if (StringUtils.isBlank(bfParams)) {
	                bfParams.append(request.getQueryString());
	            }
	        }

	        String strMessage = String
	                .format("[类名]:%s,[方法]:%s,[参数]:%s", strClassName, strMethodName, bfParams.toString());
	   
	        return point.proceed();  
	  }
	  //使用matchCondition这个切入点进行增强
	    @Before("cutController()")
	    public void before() {
	        System.out.println("before 前置通知......");
	    }
	    @After("cutController()")
	    public void after() {
	        System.out.println("after 后置通知......");
	    }
}

xml配置(转载)

1 xml配置

<bean id="sysAspect" class="com.example.aop.SysAspect"/>
<!-- 配置AOP -->
<aop:config>
    <!-- 配置切点表达式  -->
    <aop:pointcut id="pointcut" expression="execution(public * com.example.controller.*Controller.*(..))"/>
    <!-- 配置切面及配置 -->
    <aop:aspect order="3" ref="sysAspect">
        <!-- 前置通知 -->
        <aop:before method="beforMethod"  pointcut-ref="pointcut" />
        <!-- 后置通知 -->
        <aop:after method="afterMethod"  pointcut-ref="pointcut"/>
        <!-- 返回通知 -->
        <aop:after-returning method="afterReturnMethod" pointcut-ref="pointcut" returning="result"/>
        <!-- 异常通知 -->
        <aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
        <aop:around method="aroundMethod" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

2 切面类
@Component //该标签把LoggerAspect类放到IOC容器中
public class SysAspect {

/**
 * 前置通知
 * @param joinPoint
 */
public void beforMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    List<Object> args = Arrays.asList(joinPoint.getArgs());
    System.out.println("this method "+methodName+" begin. param<"+ args+">");
}
/**
 * 后置通知(无论方法是否发生异常都会执行,所以访问不到方法的返回值)
 * @param joinPoint
 */
public void afterMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("this method "+methodName+" end.");
}
/**
 * 返回通知(在方法正常结束执行的代码)
 * 返回通知可以访问到方法的返回值!
 * @param joinPoint
 */
public void afterReturnMethod(JoinPoint joinPoint,Object result){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("this method "+methodName+" end.result<"+result+">");
}
/**
 * 异常通知(方法发生异常执行的代码)
 * 可以访问到异常对象;且可以指定在出现特定异常时执行的代码
 * @param joinPoint
 * @param ex
 */
public void afterThrowingMethod(JoinPoint joinPoint,Exception ex){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("this method "+methodName+" end.ex message<"+ex+">");
}
/**
 * 环绕通知(需要携带类型为ProceedingJoinPoint类型的参数)
 * 环绕通知包含前置、后置、返回、异常通知;ProceedingJoinPoin 类型的参数可以决定是否执行目标方法
 * 且环绕通知必须有返回值,返回值即目标方法的返回值
 * @param point
 */
public Object aroundMethod(ProceedingJoinPoint point){

    Object result = null;
    String methodName = point.getSignature().getName();
    try {
        //前置通知
        System.out.println("The method "+ methodName+" start. param<"+ Arrays.asList(point.getArgs())+">");
        //执行目标方法
        result = point.proceed();
        //返回通知
        System.out.println("The method "+ methodName+" end. result<"+ result+">");
    } catch (Throwable e) {
        //异常通知
        System.out.println("this method "+methodName+" end.ex message<"+e+">");
        throw new RuntimeException(e);
    }
    //后置通知
    System.out.println("The method "+ methodName+" end.");
    return result;
}

}

发布了10 篇原创文章 · 获赞 0 · 访问量 159

猜你喜欢

转载自blog.csdn.net/ghx123456ghx/article/details/103859734