关于Spring AOP,可以去看看官方文档

https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop

  • Aspect: A modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented by using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).

  • Join point: A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

  • Advice: Action taken by an aspect at a particular join point. Different types of advice include “around”, “before” and “after” advice. (Advice types are discussed later.) Many AOP frameworks, including Spring, model an advice as an interceptor and maintain a chain of interceptors around the join point.

  • Pointcut: A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

  • Introduction: Declaring additional methods or fields on behalf of a type. Spring AOP lets you introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

  • Target object: An object being advised by one or more aspects. Also referred to as the “advised object”. Since Spring AOP is implemented by using runtime proxies, this object is always a proxied object.

  • AOP proxy: An object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy is a JDK dynamic proxy or a CGLIB proxy.

  • Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

官方文档对aop一些概念的定义:

join point:在spring aop中就是指方法(你要横切的方法)。

Pointcut:join point的集合。

target Object:目标对象。

AOP proxy:代理对象。

advice:增强的逻辑以及时机(before,after等等)。

aspect:pointcut+advice,在@AspectJ风格语法中,就是一个类。下面会示例。

Introduction:spring4后提出来的概念,这里就不说了。

demo示例:

导入依赖:

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>

AppConfig类,加上@EnableAspectJAutoproxy注解

@EnableAspectJAutoProxy
@ComponentScan("com.sunsas.aspectjdemo")
public class AppConfig { }

新建一个接口:

package com.sunsas.aspectjdemo.service;

public interface ProxyInterface {
void functionA();
void functionB();
}

新建一个类ProxyClassA实现接口:

package com.sunsas.aspectjdemo.service.impl;

import com.sunsas.aspectjdemo.service.ProxyInterface;
import org.springframework.stereotype.Component; @Component
public class ProxyClassA implements ProxyInterface {
@Override
public void functionA() {
System.out.println("ProxyClassA functionA");
} @Override
public void functionB() {
System.out.println("ProxyClassA functionB");
}
}

在别的包下新建一个类ProxyClassB实现接口:

package com.sunsas.other;

import com.sunsas.aspectjdemo.service.ProxyInterface;
import org.springframework.stereotype.Component; @Component
public class ProxyClassB implements ProxyInterface {
@Override
public void functionA() {
System.out.println("ProxyClassB functionA");
} @Override
public void functionB() {
System.out.println("ProxyClassB functionB");
}
}

声明一个切面:

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component
@Aspect
public class MyAspect {
@Pointcut("execution(* com.sunsas.aspectjdemo.service.impl.*.*(..))")
public void cutA(){
}
// @Pointcut("execution(* functionA*(..))")
// public void cutFunctionA(){
// } @Before("cutA()")
public void proxyFunctionA(){
System.out.println("++++++++++++++++++++++++begin");
System.out.println("before function");
}
// @After("cutFunctionA()")
// public void proxyFunctionB(){
// System.out.println("after function");
// System.out.println("--------------------------end");
// }
}

具体的语法可以去官方文档查看,这里使用:

表示这个service包下的所有方法都会被横切。

新建测试类Test

package com.sunsas.aspectjdemo;

import com.sunsas.aspectjdemo.service.impl.ProxyClassA;
import com.sunsas.other.ProxyClassB;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext =
new AnnotationConfigApplicationContext(AppConfig.class);
ProxyClassA proxyClassA = (ProxyClassA) annotationConfigApplicationContext.getBean("proxyClassA");
System.out.println(proxyClassA);
ProxyClassB proxyClassB = annotationConfigApplicationContext.getBean(ProxyClassB.class);
System.out.println(proxyClassB);
proxyClassA.functionA();
proxyClassA.functionB();
proxyClassB.functionA();
proxyClassB.functionB();
}
}

项目结构如图:

运行结果为:

com.sunsas.aspectjdemo.service.impl.ProxyClassA@6c4980d3
com.sunsas.other.ProxyClassB@38b27cdc
++++++++++++++++++++++++begin
before function
ProxyClassA functionA
++++++++++++++++++++++++begin
before function
ProxyClassA functionB
ProxyClassB functionA
ProxyClassB functionB

可见ProxyClassB的方法没有被代理,因为它不在com.sunsas.aspectjdemo.service.impl下。

但如果把这个@PointCut的impl去掉:

打印结果:

com.sunsas.aspectjdemo.service.impl.ProxyClassA@1984b1f
com.sunsas.other.ProxyClassB@6d167f58
++++++++++++++++++++++++begin
before function
ProxyClassA functionA
++++++++++++++++++++++++begin
before function
ProxyClassA functionB
++++++++++++++++++++++++begin
before function
ProxyClassB functionA
++++++++++++++++++++++++begin
before function
ProxyClassB functionB

可以看到ProxyClassB的方法也被代理了。

可能是这时候接口也在此包下,所以实现了此接口的类都会被横切。

上面的MyAspect类就是一个切面,里面包括了Point cut(join point)和advice(before,after,around等等)。

Spring AOP-基于@AspectJ风格的更多相关文章

  1. Spring AOP 基于AspectJ

    简介 AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持.因为Spring1.0的时候Aspectj还未出现; AspectJ1.5中新增了对 ...

  2. Spring AOP基于配置文件的面向方法的切面

    Spring AOP基于配置文件的面向方法的切面 Spring AOP根据执行的时间点可以分为around.before和after几种方式. around为方法前后均执行 before为方法前执行 ...

  3. Spring AOP With AspectJ

    一.AOP和拦截器 某些情况下,AOP和拦截器包括Filter能够实现同样的功能,一般都是请求即controller层的操作,这三个执行顺序为Filter>Interceptor>AOP, ...

  4. Comparing Spring AOP and AspectJ

    AOP 概念 在我们开始之前 , 让我们做一个快速.高级别审查的核心术语和概念 : 方面 — —标准 / 特征代码被分散在多个场所中的应用 , 通常不同于实际的业务逻辑 (例如 , 交易管理) .各方 ...

  5. 比较 Spring AOP 与 AspectJ

    本文翻译自博客Comparing Spring AOP and AspectJ(转载:https://juejin.im/post/5a695b3cf265da3e47449471) 介绍 如今有多个 ...

  6. Spring AOP 和 AspectJ

    现如今有许多个可用的 AOP 库,使用这些库需要能够回答以下问题: 是否与现有的或新的应用程序兼容? 在哪里可以使用 AOP ? 如何迅速与应用程序集成? 性能开销是多少? 在本文中,我们将回答这些问 ...

  7. 比较分析 Spring AOP 和 AspectJ 之间的差别

    面向方面的编程(AOP) 是一种编程范式,旨在通过允许横切关注点的分离,提高模块化.AOP提供方面来将跨越对象关注点模块化.虽然现在可以获得许多AOP框架,但在这里我们要区分的只有两个流行的框架:Sp ...

  8. Spring aop与AspectJ的区别?

    根据我看spring官方文档的理解(不出意外是最正确的答案): ①选择spring的AOP还是AspectJ? spring确实有自己的AOP.功能已经基本够用了,除非你的要在接口上动态代理或者方法拦 ...

  9. spring aop与aspectj

    AOP:面向切面编程 简介 AOP解决的问题:将核心业务代码与外围业务(日志记录.权限校验.异常处理.事务控制)代码分离出来,提高模块化,降低代码耦合度,使职责更单一. AOP应用场景: 日志记录.权 ...

  10. 曹工说Spring Boot源码(22)-- 你说我Spring Aop依赖AspectJ,我依赖它什么了

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

随机推荐

  1. CentOS 7 上CNVnator安装

    1.到github上下载最新版本 https://github.com/abyzovlab/CNVnator/releases 2.先看INSTALL文件,要求以下依赖,我的机器上已经安装了前两个,所 ...

  2. 二、Shell变量

    类型     注释强变量 变量在使用前,必须事先声明,甚至还需要初始化 弱变量 变量用时声明,甚至不区分类型 变量的作用:用来保存变化的数据 变量名 名称固定,由系统设定或用户定义 变量值 根据用户设 ...

  3. Rust学习笔记一 数据类型

    写在前面 我也不是什么特别厉害的大牛,学历也很低,只是对一些新语言比较感兴趣,接触过的语言不算多也不算少,大部分也都浅尝辄止,所以理解上可能会有一些偏差. 自学了Java.Kotlin.Python. ...

  4. Idea使用插件实现逆向工程搭建SpringBoot项目

    之前写SpringBoot项目,每次都要手动去写实体类.dao层啥的,尤其是数据库表字段特别多的时候,特别麻烦.然后很多小伙伴都会用逆向工程来自动生成这些类,省去许多没必要的代码量,但是Mybatis ...

  5. 牛客练习赛39 B 选点(dfs序+LIS)

    题意: 有一棵n个节点的二叉树,1为根节点,每个节点有一个值wi.现在要选出尽量多的点. 对于任意一棵子树,都要满足: 如果选了根节点的话,在这棵子树内选的其他的点都要比根节点的值大: 如果在左子树选 ...

  6. LwIP的udp学习笔记

    * Bind an UDP PCB. * * @param pcb UDP PCB to be bound with a local address ipaddr and port. * @param ...

  7. 一个故事看懂Linux文件权限管理

    前情回顾: 我通过open这个系统调用虫洞来到了内核空间,又在老爷爷的指点下来到了sys_open的地盘,即将开始打开文件的工作. 详情参见:内核地址空间大冒险:系统调用 open系统调用链 我是一个 ...

  8. Xcode如何集成Pod教程

    一般开发都会用到很多第三方的框架,利用好他们可以加快开发进度,为了更方便将第三方的框架集成到我们的项目中,Pod是个很好的选择,现在说一下该怎么将Pod集成到我们的Xcode中 第一种方法 命令行的方 ...

  9. Vue.js 计算属性computed和methods的区别

    在vue.js中,有methods和computed两种方式来动态当作方法来用的 如下: 两种方式在这种情况下的结果是一样的 写法上的区别是computed计算属性的方式在用属性时不用加(),而met ...

  10. 08.JS单词整理

    以下为按照文章顺序简单整理的JS单词, 注意:是JS单词注释,部分与英文不符 01.JS语法规范.变量与常量 console——控制台 log——日志 var——变量 variable变量,变化 co ...