欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Framework源码使用 spring-aspects AOP遇到的问题

程序员文章站 2024-01-07 15:12:04
在编译好的Spring Framework使用Aspects AOP继续上一篇,编译Spring Framework5.1源码文章。现在要使用Aspects AOP也可能会遇到一些问题,现在这篇文章带你们解决这个问题。1.导入AOP我们先看看导入spring-aspects 这个包,然后在一个配置类打上@EnableAspectJAutoProxy这个注解会出现什么样的错误。我们在配置一个AOP切面类,在类里面声明一个切入点,声明前置通知、后置通知、环绕通知。然后开始启动spring,会出...

在编译好的Spring Framework使用Aspects AOP

继续上一篇,编译Spring Framework5.1源码文章。现在要使用Aspects AOP也可能会遇到一些问题,现在这篇文章带你们解决这个问题。


1.导入AOP

我们先看看导入spring-aspects 这个包,然后在一个配置类打上@EnableAspectJAutoProxy这个注解会出现什么样的错误。
Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题
我们在配置一个AOP切面类,在类里面声明一个切入点,声明前置通知、后置通知、环绕通知。然后开始启动spring,
会出现以下错误。
1.程序包org.springframework.cache.jcache.config不存在
2.Error:(38, 49) java: 找不到符号 符号: 类 AbstractJCacheConfiguration
3.Error:(42, 16) java: 找不到符号 符号: 类 JCacheCacheAspect
4.位置: 类 org.springframework.cache.aspectj.AspectJJCacheConfiguration Error:(43, 17) java: 找不到符号

Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题

解决办法: 因为Idea默认使用的是Javac编译器,而aspect关键字它不认识,所以我们就需要下载AspectJ编译器Ajc了。

2.下载AspectJ编译器

去AspectJ官网下载AspectJ编译器,下载aspectj1.9.5的。

链接地址: https://www.eclipse.org/aspectj/downloads.php

Spring Framework源码使用 spring-aspects AOP遇到的问题
下载好了之后就是一个jar包,在jar包的位置右键菜单打开cmd控制台。

Spring Framework源码使用 spring-aspects AOP遇到的问题
运行 (java -jar aspectj-1.9.5.jar) 命令安装ajc编译器。

Spring Framework源码使用 spring-aspects AOP遇到的问题
弹出安装窗口,点击下一步。然后选择JDK安装的目录,点击下一步,选择把编译器安装到哪一个文件夹。这里我选择安装到和aspectj-1.9.5.jar同一级目录下面,点击下一步,安装完成。

Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题

3.设置IDEA编译器

打开Idea

Spring Framework源码使用 spring-aspects AOP遇到的问题
先安装Aspectj插件,并启用此插件。

Spring Framework源码使用 spring-aspects AOP遇到的问题
将Idea编译器设置为ajc,然后设置AspectJ安装目录下的lib文件夹中的aspectjtools.jar文件,再勾选Delegate to Javac选项。

Spring Framework源码使用 spring-aspects AOP遇到的问题
spring-aop_mainspring-aspectjs_main 两个模块添加 AspectJ Facets,打开:File–Project Structure–Modules

Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题
添加完后项目结构是这样的,然后点击完成。

Spring Framework源码使用 spring-aspects AOP遇到的问题
把项目重新build project,然后在运行自己项目的main方法,AOP测试在控制台打印出来了。到此为止在源码中可以使用aspectj aop了。

Spring Framework源码使用 spring-aspects AOP遇到的问题
Spring Framework源码使用 spring-aspects AOP遇到的问题
到此为止我们就可以阅读spring aop 的源码了,这里已经把AOP的后置处理器添加进来了。

Spring Framework源码使用 spring-aspects AOP遇到的问题

本文地址:https://blog.csdn.net/weixin_41657488/article/details/110223801

相关标签: java spring

上一篇:

下一篇: