SpringBoot创建的AOP不生效

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot创建的AOP不生效

sliby_spe   2022-05-24 我要评论

SpringBoot 创建AOP不生效的原因

最近在学习SpringBoot,今天学习了Aop的注册方式,原理很简单,配置也很简单,但是我注册了切面之后切面一直不生效,是为什么呢?查了好久的资料终于发现了原因,可以看下图我的切面注册类并没有问题

然后在网上偶然看到可能是主程序扫描的原因,才发现了原因,可以看到我的显示方式本来是flat的,那样的话就很难找出原因了

修改为hirerchical就可以很清楚的看出问题

可以看出,我的主程序和切面类并不在一个包中,那么主程序扫描不到切面类,自然就不会注册切面了,最简单的解决方式就是在主程序中添加一个注解@ComponentScan

那么我们就能对springboot有更深入的认识,其实他相对于ssm所有的简化步骤关键在于主程序,他起到了一个封装加载的步骤,不主动声明的情况下他会扫描和自己在同一个包下面的所有类,并根据注解自动注册,那么以后写项目时最好的方式就是将主程序放在主包下,然后所有的这些类都放在子包中即可

SpringBoot aop无效的情况

项目结构

在这里插入图片描述

package com.example.demo.inter;
public interface CustomerService {
     void doSomething1();
     void doSomething2();
}
package com.example.demo.inter;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
@Service
public class CustomerServiceImpl implements CustomerService {
    @Override
    public void doSomething1() {
        System.out.println("CustomerServiceImpl.doSomething1()");
        doSomething2();
        ((CustomerService) AopContext.currentProxy()).doSomething2();
    }
    @Override
    public void doSomething2() {
        System.out.println("CustomerServiceImpl.doSomething2()");
    }
}
package com.example.demo;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class CustomerServiceInterceptor {
    @Before("execution(* com.example.demo.inter..*.*(..))")
    public void doBefore() {
        System.out.println("do some important things before...");
    }
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy(proxyTargetClass=true, exposeProxy=true)
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
package com.example.demo;
import com.example.demo.inter.CustomerService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
    @Autowired
    CustomerService customerService;
    @Test
    public void testAOP() {
        customerService.doSomething1();
    }
    @Test
    void contextLoads() {
    }
}

运行下testAOP,为啥doSomething2()没有切面效果,使用AopContext.currentProxy就可以了?

拦截器的实现原理就是动态代理,实现AOP机制。Spring 的代理实现有两种:一是基于 JDK Dynamic Proxy 技术而实现的;二是基于 CGLIB 技术而实现的。如果目标对象实现了接口,在默认情况下Spring会采用JDK的动态代理实现AOP,CustomerServerImpl正是这种情况。

JDK动态代理生成的CustomerServiceImpl的代理类翻译过来如下:

package com.example.demo;
import com.example.demo.inter.CustomerService;
public class CustomerServiceProxy implements CustomerService {
    private CustomerService customerService;
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
    public void doSomething1() {
        doBefore();
        customerService.doSomething1();
        // 默认,所以不会执行doBefore
        customerService.doSomething2();
        // 加入 AopContext.currentProxy的效果,完成切面效果
        this.doSomething2();
    }
    public void doSomething2() {
        doBefore();
        customerService.doSomething2();
    }
    private void doBefore() {
        System.out.println("do some important things before...");
    }
}

这样很直观地明白为啥要使用AopContext.currentProxy了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们