【问题标题】:Aspect Logging: no match for this type name, what am I doing wrong?方面记录:与此类型名称不匹配,我做错了什么?
【发布时间】:2021-09-03 20:01:00
【问题描述】:

这是我的日志类

@Aspect
@Component
public class LoggingAspect {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Pointcut("within(@com.topjava.graduation.restaurant *)" +
            " || within(@com.topjava.graduation.restaurant.service *)" +
            " || within(@com.topjava.graduation.restaurant.controller *)")
    public void springBeanPointcut() {
        // Method is empty as this is just a Pointcut, the implementations are in the advices.
    }

//      Advice that logs methods throwing exceptions.
    @AfterThrowing(pointcut = "springBeanPointcut()", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
        log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL");
    }

//      Advice that logs when a method is entered and exited.

    @Around("springBeanPointcut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        if (log.isDebugEnabled()) {
            log.debug("Enter: {}.{}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(),
                    joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
        }
        try {
            Object result = joinPoint.proceed();
            if (log.isDebugEnabled()) {
                log.debug("Exit: {}.{}() with result = {}", joinPoint.getSignature().getDeclaringTypeName(),
                        joinPoint.getSignature().getName(), result);
            }
            return result;
        } catch (IllegalArgumentException e) {
            log.error("Illegal argument: {} in {}.{}()", Arrays.toString(joinPoint.getArgs()),
                    joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
            throw e;
        }
    }
}

这是我的例外

 Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'
Caused by: java.lang.IllegalArgumentException: warning no match for this type name: com.topjava.graduation.restaurant [Xlint:invalidAbsoluteTypeName]

这是我的结构。我在这个目录下有我的控制器类,为什么不匹配?

非常感谢您的帮助!

【问题讨论】:

    标签: java spring spring-boot aop


    【解决方案1】:

    你有一些基本的语言类型错误

    within(@com.topjava.graduation.restaurant *)
    

    检查这个和你拥有的其他within(@...)

    当您想要表示带有以下注释的类时,您应该使用@。例如,您将使用 within(@org.springframework.stereotype.Repository *) 并且您的意思是具有来自 Spring 的注释 @Repository 的类。

    在您的情况下,您打算指的是包裹。那么正确的做法是within(com.topjava.graduation.restaurant.*)

    所以最后你必须把它改正为

      @Pointcut("within(com.topjava.graduation.restaurant.*)" +
                    " || within(com.topjava.graduation.restaurant.service.*)" +
                    " || within(com.topjava.graduation.restaurant.controller.*)")
    

    【讨论】:

    • 基于包结构within(com.topjava.graduation.restaurant.controller..*) 将是准确的。 controller下有子包
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多