AspectJ

Aspect-oriented programming先参考这篇文章:

http://blog.youkuaiyun.com/onlyqi/article/details/7168050


参考wiki:

http://en.wikipedia.org/wiki/AspectJ

AspectJ Programming Guide:

http://www.eclipse.org/aspectj/doc/released/progguide/index.html


All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects.

AspectJ是Aspect-oriented programming在java中的扩展,所以任何标准的java代码都是AspectJ代码,但是AspectJ代码比标准java代码扩展出了以下部分:


Aspects can contain several entities unavailable to standard classes. These are:

    inter-type declarations—allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:

aspect VisitAspect {
  void Point.acceptVisitor(Visitor v) {
    v.visit(this);
  }
}

    pointcuts — allow a programmer to specify join points (well-defined moments in the execution of a program, like method call, object instantiation, or variable access). All pointcuts are expressions (quantifications) that determine whether a given join point matches. For example, this point-cut matches the execution of any instance method in an object of type Point whose name begins with set:

pointcut set() : execution(* set*(..) ) && this(Point);

    advice — allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. Here, the advice refreshes the display every time something on Point is set, using the pointcut declared above:

after () : set() {
  Display.update();
}

AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance).


其中最关键的就是join points,也就是pointcut,即定义在什么地方做cross-cutting。而AspectJ对此的语法是:


AspectJ's join-point model

    The join points in AspectJ include method or constructor call or execution, the initialization of a class or object, field read and write access, exception handlers, etc. They do not include loops, super calls, throws clauses, multiple statements, etc.

    Pointcuts are specified by combinations of primitive pointcut designators (PCDs).

1,  "Kinded" PCDs match a particular kind of join point (e.g., method execution) and tend to take as input a Java-like signature. One such pointcut looks like this:

 execution(* set*(*))

    This pointcut matches a method-execution join point, if the method name starts with "set" and there is exactly one argument of any type.

2, "Dynamic" PCDs check runtime types and bind variables. For example

  this(Point)

    This pointcut matches when the currently-executing object is an instance of class Point. Note that the unqualified name of a class can be used via Java's normal type lookup.

3, "Scope" PCDs limit the lexical scope of the join point. For example:

 within(com.company.*)

    This pointcut matches any join point in any type in the com.company package. The * is one form of the wildcards that can be used to match many things with one signature.

4, Pointcuts can be composed and named for reuse. For example

   pointcut set() : execution(* set*(*) ) && this(Point) && within(com.company.*);

    This pointcut matches a method-execution join point, if the method name starts with "set" and this is an instance of type Point in the com.company package. It can be referred to using the name "set()".

 

Advice specifies to run at (before, after, or around) a join point (specified with a pointcut) certain code (specified like code in a method). The AOP runtime invokes Advice automatically when the pointcut matches the join point.

For example:

after() : set() {
   Display.update();
}

    This effectively specifies: "if the set() pointcut matches the join point, run the code Display.update() after the join point completes." 


要想在java中使用AspectJ,我们需要准备两样东西:

1,由于AspectJ使用的不同于java的语法,我们需要用AspectJ complier。eclipse的AspectJ development tool已经集成了这个编译器。

2,AspectJ的jar包。

官网上都提供:
http://eclipse.org/aspectj/







04-01
<think>好的,用户想了解AspectJ的使用教程或官方文档。首先,我需要整理现有的引用信息,看看有哪些可用的资源。根据引用内容,用户可能对在Android或Spring中使用AspectJ感兴趣,或者如何引入依赖。 首先,引用[1]提到在Android中直接引入AspectJ的方式,引用[2]和[3]讨论了Spring AOP与AspectJ的结合,引用[4]给出了添加依赖的示例。用户可能需要基础教程和官方文档的指引。 我应该分步骤回答,先介绍AspectJ的基本概念,再讲解使用方式,包括依赖配置、切面编写和编译配置。然后提供官方文档链接和相关教程资源。同时,根据引用内容,需要区分在Spring和Android中的不同使用方法,避免混淆。 需要确保回答结构清晰,符合用户要求的系统级指令,比如正确使用LaTeX格式,不过这里可能用不上数学公式。但要注意中文回答,生成相关问题,并正确添加引用标识。最后,生成的相关问题应该围绕AspectJ的应用场景、依赖注入、与Spring AOP的区别等,帮助用户深入理解。</think>### AspectJ 使用教程及官方文档指引 #### 一、AspectJ 简介 AspectJ 是一个面向切面编程(AOP)的扩展工具,通过增强 Java 语法(如注解、切入点表达式等)实现代码的横向关注点分离。它支持编译时编织(CTW)和加载时编织(LTW),适用于日志、事务、权限控制等场景[^2][^3]。 --- #### 二、AspectJ 使用步骤 ##### 1. 添加依赖 在 Maven 项目中引入 AspectJ 核心依赖: ```xml <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.6</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency> ``` 引用自依赖配置示例[^4]。 ##### 2. 编写切面类 使用 `@Aspect` 注解定义切面,结合 `@Pointcut` 和 `@Around` 等注解实现逻辑: ```java @Aspect public class LoggingAspect { @Pointcut("execution(* com.example.service.*.*(..))") public void serviceMethods() {} @Around("serviceMethods()") public Object logMethod(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("方法执行前: " + joinPoint.getSignature()); Object result = joinPoint.proceed(); System.out.println("方法执行后"); return result; } } ``` ##### 3. 配置编织方式 - **编译时编织**:通过 AspectJ 编译器(`ajc`)在编译阶段织入切面。 - **加载时编织**:在 JVM 启动时添加参数 `-javaagent:aspectjweaver.jar` 实现动态织入。 --- #### 三、AspectJ 在 Spring 与 Android 中的使用 1. **Spring 集成** Spring AOP 默认使用动态代理,而 AspectJ 提供更强大的功能(如非 Spring 管理对象的注入)。需在配置中启用 `@EnableAspectJAutoProxy`。 2. **Android 应用** 通过 Gradle 插件(如 `aspectj-tools`)配置编译时编织,直接引入 AspectJ 依赖并定义切面逻辑[^1]。 --- #### 四、官方文档与资源 1. **AspectJ 官方文档** 访问 [AspectJ 官网](https://www.eclipse.org/aspectj/) 获取完整语法和示例。 2. **教程推荐** - [AspectJ 编程指南](https://www.eclipse.org/aspectj/doc/released/progguide/index.html) - [Spring 与 AspectJ 整合文档](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-using-aspectj) ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值