According to wikipedia

 aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. 

Let's imagine we have serveral modules in a project, each of the them is working well until one day the team decide to dig up more information. The desire to add more logs in the project increases while the effort to change the existing codes is huge, it stops the team from moving on bravely.

Somehow the spread of aop cheers the team. According the story, we just need to add a few classes to change the whole project. For example, one class can add entry log to every mothod. It slightly impact the performance and changes almost nothing of the existing codes. A small modification, an encouraging benefit.

AOP(面向切面编程) 通过隔离切面逻辑来提高模块性

假设我们的项目现在已经有不少模块,它们都很正常的运行着。突然有一天,团队想要从现有项目挖掘出更多的信息。比如,通过全面的方法调用日志来作服务监控。一个一个方法的去添加日志固然可以,然而写起来很枯燥也很花时间,这让队伍有所顾虑。

这时,一条好消息传来。据说有个叫AOP的东西可以通过增加很少量的代码就对整个项目进行修改。举个例子,添加一个的切面类就可以给所有方法加上调用日志。可以说在无需触碰现有代码,也不太影响项目性能的情况下,获得了不小的收益。

There are several ways to enhance the current codes using aop:

1) compile time enhancement

2) post compile enhancement

3) load time enhancement

4) run time enhancement

切面编程有许多方法

1) 编译期代码增强

2) 已有二进制代码增强(比如增强第三方库)

3) jvm类加载期代码增加

4) 运行时代码增强

This chapter will show an example of compile time weaving.

这一节会简单介绍下编译器代码织如的方法

Write a simple service

写一个简单的订阅服务

public class BookingService {
    public void book() {
        System.out.println("Booked a room");
    }
}

A test class

一个切面测试类

public class AspectTest {
    public static void main(String[] args) throws Exception {
        BookingService bookingService = new BookingService();
        bookingService.book();
    }
}

An aspect class (it's a special class and may not be recoginzed correctly by IDE)

切面类 (IDE可能无法很好识别这个文件)

package aop.compile;

import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;

public aspect TrialAspect{        before():execution(* aop.compile.BookingService.*(..)){        System.out.println("----------log before method excecution----------");        }}

Now, execute weaving command in terminal and run enhanced main class

现在,用ajc编译代码并运行测试类

ajc -d . *.java

java aop.compile.AspectTest

The terminal displays the result as expected. Every time the booking service is called, an entry log will be recorded.

标记部分便是切面逻辑。每次订阅服务有被调用,就会增加一条调用日志

----------log before method excecution----------
Booked a room

Before weaving, the directory looks this way

代码织入之前,目录结构是这样的

After weaving, we found the enhanced class files

运行完ajc之后,可以看到编译出的class文件

Take a look at the BookingService.class, the marked line on which the enhancement relies is used for log purpose.

点开订阅服务的代码一看,原来切面逻辑(打日志)已经写入了book方法

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package aop.compile;

import aop.compile.TrialAspect;

public class BookingService {
    public BookingService() {
    }

    public void book() {
        $a8643223();
        System.out.println("Booked a room");
    }
}

Till this step, the demo is finished.

至此,演示结束

How to use AspectJ

如何使用AspectJ

1. Download aspectj jar from https://eclipse.org/aspectj/

先从官网下载aspectj jar包

2. Install aspectj

安装

java -jar aspectj-xx.jar

an installation window pops up

安装时会弹出窗口

by default, aspectj will be installed in C: driver

3. Configure path and classpath environment variables.

配置环境变量和类路径

Add C:\aspectj1.8\lib\aspectjrt.jar;C:\aspectj1.8\lib\aspectjtools.jar --> classpath

Add C:\aspectj1.8\bin --> path

4. Check installation

检查安装

Java AOP (1) compile time weaving 【Java 切面编程 (1) 编译期织入】的更多相关文章

  1. Java AOP (2) runtime weaving 【Java 切面编程 (2) 运行时织入】

    接上一篇 Java AOP (1) compile time weaving [Java 切面编程 (1) 编译期织入] Dynamic proxy   动态代理 Befor talking abou ...

  2. Spring AOP 之编译期织入、装载期织入、运行时织入(转)

    https://blog.csdn.net/wenbingoon/article/details/22888619 一   前言 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP ...

  3. Java实战之03Spring-03Spring的核心之AOP(Aspect Oriented Programming 面向切面编程)

    三.Spring的核心之AOP(Aspect Oriented Programming 面向切面编程) 1.AOP概念及原理 1.1.什么是AOP OOP:Object Oriented Progra ...

  4. 面向切面编程AOP

    本文的主要内容(AOP): 1.AOP面向切面编程的相关概念(思想.原理.相关术语) 2.AOP编程底层实现机制(动态代理机制:JDK代理.Cglib代理) 3.Spring的传统AOP编程的案例(计 ...

  5. 依赖注入(DI)有助于应用对象之间的解耦,而面向切面编程(AOP)有助于横切关注点与所影响的对象之间的解耦(转good)

    依赖注入(DI)有助于应用对象之间的解耦,而面向切面编程(AOP)有助于横切关注点与所影响的对象之间的解耦.所谓横切关注点,即影响应用多处的功能,这些功能各个应用模块都需要,但又不是其主要关注点,常见 ...

  6. Spring 框架基础(04):AOP切面编程概念,几种实现方式演示

    本文源码:GitHub·点这里 || GitEE·点这里 一.AOP基础简介 1.切面编程简介 AOP全称:Aspect Oriented Programming,面向切面编程.通过预编译方式和运行期 ...

  7. AOP——面向切面编程

    目录 什么是AOP AOP的作用和优势 作用: 优势: AOP相关术语 AOP的实现方式 使用动态代理的方式 使用XML的方式 使用注解的方式 什么是AOP AOP:全称是Aspect Oriente ...

  8. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

  9. Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理

    1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...

随机推荐

  1. mac下安装 resin 奇葩问题总结

    mac 下安装 resin(需要确认电脑配置好了 java 环境变量): 第一步:去 resin 的官网下载最新的压缩包,官网连接:http://caucho.com/products/resin/d ...

  2. 二、Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...

  3. 打印zigzag矩阵

    比较愚蠢但是很好理解的一种方法 public static void printZigzag (int n){ int[][] arr = new int[n][]; //动态创建数组 并初始化 fo ...

  4. (1) 类构造块,this(),static,单例模式串讲

    类构造块 在类只用一对大括号包含的内容,构造所有的对象时都会执行的内容,如果某个类有好几个够赞函数,公共部分抽取出来,放到构造块中. clas Boy { ... { syso("哭...& ...

  5. [译]Selenium Python文档:目录

    作者:Baiju Muthukadan 协议:本文档采用知识共享署名 - 共享4.0国际许可. 原英文网址:http://selenium-python.readthedocs.io/index.ht ...

  6. 日期控件My97DatePicker的使用

    一. 简介 1. 简介 目前的版本是:4.8 2. 注意事项 My97DatePicker目录是一个整体,不可破坏里面的目录结构,也不可对里面的文件改名,可以改目录名 My97DatePicker.h ...

  7. pacejs进度条监控服务端数据加载是否完毕

    记得刚刚入职新公司的时候,公司在做app里面的h5页面.跟之前公司的流程不太一样.之前都是写完静态页面后通过ajax加载接口数据,这公司省事了,写完静态页面直接扔给服务端,他们来填数据,确实给前端省事 ...

  8. 老李分享:HTTP协议之请求和响应

    老李分享:HTTP协议之请求和响应   HTTP请求头详解: GET http://www.foo.com/ HTTP/1.1 GET是请求方式,请求方式有GET/POST http://www.fo ...

  9. https证书申请流程和简介

    HTTPS证书是什么 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安 ...

  10. Android Weekly Notes Issue #251

    Android Weekly Issue #251 April 2nd, 2017 Android Weekly Issue #251. 本期内容: Android O新增的API: View的too ...