Springboot

Springboot aop不起作用的问题

2021-03-23  本文已影响0人  发哥_郑书发

最近在研究Springboot切面编程,碰到一个莫名其妙的问题,aop怎么都不起作用。自定义的注解使用到了aop,却是有效的。

网上查找了几个小时, 各种方式都试了(使用的是InteliJ IDEA编辑器)。

首先确认了pom.xml文件里是载入了aop的

其次,确认了切面类是没有问题的,

package com.jeealfa.aspect;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.*;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect

@Component

public class AopLog {

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

    //线程局部变量,用于解决多线程中相同变量的访问冲突问题

    ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Pointcut("execution(* com.jeealfa.controller..*.*(..))")

    public void aopWebLog() {

    }

    @Before("aopWebLog()")

    public void doBefore(JoinPoint joinPoint) throws Throwable {

        startTime.set(System.currentTimeMillis());

        //接手到请求,记录请求内容

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = attributes.getRequest();

        log.info("URL:" + request.getRequestURI().toString());

        log.info("HTTP Method:" + request.getMethod());

        log.info("IP:" + request.getRemoteAddr());

        log.info("类的方法:" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

        log.info("参数:" + request.getQueryString());

        System.out.println(request.getRequestURI().toString());

    }

}

再次,根据网上查的资料在启动类加了注解,且aspect文件夹与JeealfaBootApplication处于平级

package com.jeealfa;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication

@EnableAspectJAutoProxy(proxyTargetClass=true)

@ComponentScan({"com.jeealfa.*"})

public class JeealfaBootApplication {

public static void main(String[] args) {

SpringApplication.run(JeealfaBootApplication.class, args);

}

}

实际@SpringBootApplication包含了@CompentScan

另外,HelloController类hello方法如下

package com.jeealfa.controller;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController

@Slf4j

public class HelloController {

    @GetMapping("/hello")

    public String hello(){ return "hello world"; }

}

网上各种方法都试了, aop就是始终不起作用。 运行 /hello时,AopLog里的doBefore方法始终没有运行。并且发现一个奇怪现象,其他文件修改,springboot的热启动都会自动更新启动,修改这个切面类时,却无反应。

不过最终经过多次折腾终于发现了问题所在。

在创建这个切面类时,直接选择了 Aspect, 见下图

创建了之后, 然后在把aspect修改为class,

修改为class后,左边最前面的A标会自动变成C标,这样就跟从Java Class创建的切面类看起来一模一样,但就是不起作用。

正确的做法: 通过Java Class创建aspect类,然后加上 @Aspect和@Component注解。 我试了从Java Class创建aspect类,把之前无效的代码完整拷贝到这个新建的文件里,就生效了。至于上面那种创建aspect类为何会导致无效的内在原因,还不清楚,有知情的朋友请留言回复。 虽然两种创建方式不同,但最终代码呈现完全是一样的,就是一个有用一个无用。

上一篇下一篇

猜你喜欢

热点阅读