Coder Social home page Coder Social logo

sivan757 / aspectj-examples Goto Github PK

View Code? Open in Web Editor NEW
11.0 1.0 0.0 1.92 MB

Java 集成 aspectj 的示例项目, 包含 '.aj'文件, Java 注解, Lombok, LTW, SpringBoot, SpringBoot + Lombok, SpringBoot + Lombok + LTW的集成示例.

Java 97.04% AspectJ 2.96%
aspectj ltw lombok springboot-lombok ajc

aspectj-examples's Introduction

aspectj-examples

在网上看了很多文章,大多是介绍 AspectJ 语法(使用.aj、Annotation编写切面)、Spring Aop 与 AspectJ 的关系,但却很少有在SpringBoot中使用的例子,因此做一次使用起来的分享~

AspectJ 是什么

AspectJ 是一个 AOP 的具体实现框架。AOP(Aspect Oriented Programming)即面向切面编程,可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。

织入时间

将定义横切关注点(切面)插入到编写的源代码中的过程,称为编织。 在 AspectJ 中,编织有3个不同的时间段:

  1. Compile-time(CTW): 编译时编织,AJC编译器从源代码编译并生成编织好的文件输出。
  2. Post-compile time: 编译后编织,用于编织已编译的类。(为了方便,在下文中将使用简写,PCTW
  3. Load-time(LTW): 加载时编织,在类加载器将类加载到JVM时编织,需要提供AspectJ Java Agent支持.

AspectJ Jar

我们经常会使用到AspecJ提供的三个Jar包:

  1. aspectjrt.jar: 提供运行时的注解、静态方法,基本上用到AspectJ都需要引入依赖(但我还未找到不需要的场景...)。
  2. aspectjweaver.jar: 包含AspectJ运行时、编织器、编织类装入器和编织代理。它还包含用于解析XML编织配置文件的DTD。
  3. aspectjtools.jar: 提供AJC编译器,可以在编译期将 .java.class.aj定义的切面编织到源代码中。

模块介绍

aspectj-examples
├── aspectj                         # LTW 相关Jar
├── aspectj-aj                      # AspectJ.aj CTW示例
├── aspectj-java                    # AspectJ.java CTW示例
├── aspectj-lombok                  # AspectJ + Lombok PCTW示例
├── aspectj-lombok-ltw              # AspectJ + Lombok LTW示例
├── springboot-aspectj              # SpringBoot + AspectJ 示例
├── springboot-aspectj-autowired    # SpringBoot + AspectJ + @Autowired 示例
├── springboot-aspectj-lombok       # SpringBoot + AspectJ + Lombok PCTW示例
└── springboot-aspectj-lombok-ltw   # SpringBoot + AspectJ + Lombok LTW示例

各模块相互独立,父pom.xml仅定义依赖版本;各模块拥有说明文档提供简短说明、依赖、运行、输出等信息;按模块顺序,模块中相同的说明信息不会重复出现。

NOTE:

  1. 除LTW(Load Time Weaving)外,其余模块都需要使用AJC编译器 (aspectj-maven-plugin 默认提供)。

  2. lombok 模块,需要在'IDE'中设置'Post-compile weave'

    • IDEA: 'Project Structure' -> 'Facets' -> 'your-lombok-module' -> 'Compiler' -> 'Post-compile weave mode'
    • Eclipse: EJDT
  3. 尝试在LTW中使用'.aj'文件,但无法解析,暂无解决方案。

更新注记

  • '2020-11-26': 发现 SpringBoot 使用 AspectJ 后,无法对切面(@Aspect)对象进行自动注入,添加一个关于注入切面类的模块。

参考

  1. The AspectJtm Development Environment Guide

  2. AspectJ and Lombok are not mutually exclusive

  3. Mojo's AspectJ Maven Plugin - aspectj:compile

  4. 原生AspectJ用法分析以及SpringAOP原理分析

aspectj-examples's People

Contributors

sivan757 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.