经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringBoot中创建的AOP不生效的原因及解决
来源:jb51  时间:2021/11/24 12:48:11  对本文有异议

SpringBoot 创建AOP不生效的原因

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

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

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

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

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

SpringBoot aop无效的情况

项目结构

在这里插入图片描述

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

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

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

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

  1. package com.example.demo;
  2. import com.example.demo.inter.CustomerService;
  3. public class CustomerServiceProxy implements CustomerService {
  4. private CustomerService customerService;
  5. public void setCustomerService(CustomerService customerService) {
  6. this.customerService = customerService;
  7. }
  8. public void doSomething1() {
  9. doBefore();
  10. customerService.doSomething1();
  11. // 默认,所以不会执行doBefore
  12. customerService.doSomething2();
  13. // 加入 AopContext.currentProxy的效果,完成切面效果
  14. this.doSomething2();
  15. }
  16. public void doSomething2() {
  17. doBefore();
  18. customerService.doSomething2();
  19. }
  20. private void doBefore() {
  21. System.out.println("do some important things before...");
  22. }
  23. }

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

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号