java aspectj_java – @AspectJ切入包中的所有方法

我有一个特定的包的这个工作代码,但我想配置它为所有控制器,服务和dao包

例如

> com.abc.xyz.content.controller

> com.abc.xyz.content.service

> com.abc.xyz.content.dao

> com.abc.xyz.category.controller

> com.abc.xyz.category.service

> com.abc.xyz.category.dao

等等. . .

这是我的项目的基础包,有人可以帮助我如何去做,以便它适用于我的网络项目的所有类,包括控制器,谢谢提前.

. .

package com.abc.xyz.utilities;

import java.util.Arrays;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

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.core.annotation.Order;

import org.springframework.stereotype.Component;

@Aspect

@Component

public class LoggingAspect

{

private Log log = LogFactory.getLog(this.getClass());

@Pointcut("execution(* com.abc.xyz.content.service..*(..))")

protected void loggingOperation()

{

}

@Before("loggingOperation()")

@Order(1)

public void logJoinPoint(JoinPoint joinPoint)

{

log.info("Signature declaring type : " + joinPoint.getSignature().getDeclaringTypeName());

log.info("Signature name : " + joinPoint.getSignature().getName());

log.info("Arguments : " + Arrays.toString(joinPoint.getArgs()));

log.info("Target class : " + joinPoint.getTarget().getClass().getName());

}

@AfterReturning(pointcut = "loggingOperation()",returning = "result")

@Order(2)

public void logAfter(JoinPoint joinPoint,Object result)

{

log.info("Exiting from Method :" + joinPoint.getSignature().getName());

log.info("Return value :" + result);

}

@AfterThrowing(pointcut = "execution(* com.abc.xyz.content.service..*(..))",throwing = "e")

@Order(3)

public void logAfterThrowing(JoinPoint joinPoint,Throwable e)

{

log.error("An exception has been thrown in " + joinPoint.getSignature().getName() + "()");

log.error("Cause :" + e.getCause());

}

@Around("execution(* com.abc.xyz.content.service..*(..))")

@Order(4)

public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable

{

log.info("The method " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));

try

{

Object result = joinPoint.proceed();

log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);

return result;

}

catch (IllegalArgumentException e)

{

log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()");

throw e;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值