SpringBoot中创建的AOP不生效的原因及解决

 更新时间:2021年11月24日 11:13:41   作者:sliby_spe  
这篇文章主要介绍了SpringBoot中创建的AOP不生效的原因及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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了。

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

相关文章

  • 详解MyBatis中Executor执行SQL语句的过程

    详解MyBatis中Executor执行SQL语句的过程

    MyBatis中获取SqlSession时会创建执行器Executor并存放在SqlSession中,本篇文章将以MapperMethod的execute() 方法作为起点,对MyBatis中的一次实际执行请求进行说明,并结合源码对执行器Executor的原理进行阐释
    2023-07-07
  • SpringBoot简单实现定时器过程

    SpringBoot简单实现定时器过程

    这篇文章主要介绍了SpringBoot简单实现定时器过程,对于Java后端来说肯定实现定时功能肯定是使用到Spring封装好的定时调度Scheduled
    2023-04-04
  • 解决gateway配合nacos路由报错:Unable to find instance for XXX问题

    解决gateway配合nacos路由报错:Unable to find instance&

    这篇文章主要介绍了解决gateway配合nacos路由报错:Unable to find instance for XXX问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-06-06
  • 浅谈Java线程池的7大核心参数

    浅谈Java线程池的7大核心参数

    本篇文章基于正在看这篇文章的你已经具备了基本的Java并发的相关知识.如果对于Java并发编程一无所知的话,请先看看Java并发编程的一些前导基础知识,文中有非常详细的图文示例及代码,,需要的朋友可以参考下
    2021-05-05
  • SpringBoot 整合 Shiro 密码登录的实现代码

    SpringBoot 整合 Shiro 密码登录的实现代码

    这篇文章主要介绍了SpringBoot 整合 Shiro 密码登录的实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • MyBatis多表操作查询功能

    MyBatis多表操作查询功能

    这篇文章主要介绍了MyBatis多表操作,包括一对一查询,一对多查询的模型,多对多查询的需求,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-11-11
  • Maven统一版本管理的实现

    Maven统一版本管理的实现

    在使用Maven多模块结构工程时,配置版本是一个比较头疼的事,本文主要介绍了Maven统一版本管理的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • 一篇文章了解Jackson注解@JsonFormat及失效解决办法

    一篇文章了解Jackson注解@JsonFormat及失效解决办法

    这篇文章主要给大家介绍了关于如何通过一篇文章了解Jackson注解@JsonFormat及失效解决办法的相关资料,@JsonFormat注解是一个时间格式化注解,用于格式化时间,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • Java线程通信详解

    Java线程通信详解

    本篇文章主要介绍了Java线程通信问题,线程通信用来保证线程协调运行,有需要的朋友可以了解一下。
    2016-10-10
  • @RequestBody不能映射到对象的解决

    @RequestBody不能映射到对象的解决

    这篇文章主要介绍了@RequestBody不能映射到对象的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10

最新评论