aspectj

AspectJ 切点指示器-05-this

自闭症网瘾萝莉.ら 提交于 2020-02-26 00:21:57
this() 代理类按照类型匹配于指定类,则被代理的目标类所有连接点匹配切点 一般情况下,使用this()和target()来匹配定义切点,二者是等效的,二者的区别体现在通过引介切面产生代理对象时的具体表现。 1、Factory package com.test.aspectj.expression; /** * 工厂接口 */ public interface Factory { // 制作产品 void make(); // 运输 void delivery(String address); } 2、PhoneFactory package com.test.aspectj.expression; import com.test.aspectj.expression.args.FreshFoodFactory; import com.test.aspectj.expression.args.FrozenFoodFactory; import org.springframework.stereotype.Component; /** * 食品工厂 */ @Component public class FoodFactory implements Factory { // 制作产品的方法 @Override public void make() { System.out

SpringAOP知识点梳理

这一生的挚爱 提交于 2020-02-25 22:08:15
在前段时间面试时,曾多次被问到“对AOP你了解多少”这样的问题。以前工作中虽然有用过 Spring AOP,但由于没有系统得自学过,所以关于Spring AOP 这块,可以说是基础甚差。即使临时抱佛脚,凭借网上面试宝典中前人的总结,在面对面试官时,也仅仅说出“AOP是面向切面编程”、“运用了代理模式”云云。多次碰壁后,我意识到自己过于自信,没有系统概念,就无法在面试官前多说出一句有意义的回答。 最近我把《Spring实战第四版》关于AOP那章看了一遍,除了最后的“注入AspectJ切面”外,其他知识点,都简单地做了梳理。AspectJ切面,由于涉及到AspectJ 自有AOP语法,我暂时不准备补习,所以不做梳理。 我认为,关于 Spring AOP,可以从这几个问题开始: 什么是 AOP? AOP 要解决的问题是什么? 关于 AOP 的术语? Spring AOP 的实现方式? 一、什么是AOP? AOP,全称:Aspect Oriented Programming,意为面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。(此概念来自百度百科) AOP框架不只是定义在Spring意义下,还有很多其他的AOP框架。不同的 AOP框架可能在连接点模型上有强弱之分,例如:有些通知可以应用在字段修饰符级别,另一些则只支持与方法调用相关的连接点;除此之外

AspectJ 切点指示器-04-target和@target

房东的猫 提交于 2020-02-25 21:04:58
target() 匹配目标类以及子类,目标类以及其子类的所有方法执行都会匹配到 @target() 匹配目标对象被特定注解标注的类 最终类结构图 1、Factory package com.test.aspectj.expression; /** * 工厂接口 */ public interface Factory { // 制作产品 void make(); // 运输 void delivery(String address); } 2、PhoneFactory,会被TargetAspect里定义的@Before("target(com.test.aspectj.expression.PhoneFactory)")匹配到 package com.test.aspectj.expression; import com.test.aspectj.expression.annotation.Log; import org.springframework.stereotype.Component; /** * 手机工厂 */ @Component public class PhoneFactory implements Factory { // 制作产品的方法 @Override @Log public void make() { System.out.println(

0216 aop和打印数据库执行日志

扶醉桌前 提交于 2020-02-25 20:34:29
需求 maven依赖 <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.8.7</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.2-jre</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <

AspectJ - pointcut to match a method that has generic parameters

丶灬走出姿态 提交于 2020-02-24 06:22:20
问题 I have a generic method that accepts any type as its parameter. For example, I would like a pointcut that matches the calls made to the method only with 'String' type as its parameter. Ultimately the requirement is to limit the scope which the advices get executed for to 'String' parameters. Here is my generic class and method: public class Param<T> { public T execute(T s){ return s; } } Main class: My app makes calls to the method with both Boolean and String as parameters. public static

aspectJ最简单的HelloWorld例子

ⅰ亾dé卋堺 提交于 2020-02-19 20:35:34
aspectJ最简单的HelloWorld例子 这是aspectJ的入门。aspectJ和aop有什么区别? aspectJ也能够实现和aop功能,aop一般指spring的aop,就是指使用@Aspect/@Pointcut/@Before/@Around等注解的那一套,实现原理是动态代理。而 aspectJ是更加高效的在编译器的层面上植入通知的 可以猜测,aspectJ 和 lombok 类似,都是编译器帮你 “写入” 了通知的代码(拦截后打印日志的逻辑,叫 “通知”)。虽然aspectJ高效,但是需要特别的编译器,所以没有aop来得方便,而aop相比aspectJ没那么高效但是也能接受,所以工作多年我 从未见到在工作中使用aspectJ 简介和spring aop的对比 这里摘抄自 https://www.jianshu.com/p/872d3dbdc2ca Spring AOP AspectJ 在纯 Java 中实现 使用 Java 编程语言的扩展实现 不需要单独的编译过程 除非设置 LTW,否则需要 AspectJ 编译器 (ajc) 只能使用运行时织入 运行时织入不可用。支持编译时、编译后和加载时织入 功能不强-仅支持方法级编织 更强大 - 可以编织字段、方法、构造函数、静态初始值设定项、最终类/方法等…。 只能在由 Spring 容器管理的 bean 上实现

spring面向切面编程(AOP)

谁说我不能喝 提交于 2020-02-19 04:04:29
AOP(Aspect Oriented Programing)面向切面编程 AOP是一种编程范式,隶属于软工范畴,指导开发 者如何组织程序结构 AOP弥补了OOP的不足,基于OOP基础之上进行 横向开发 AOP联盟 一、OOP范式编程 OOP规定程序开发以类为模型,一切围绕对象进行, OOP中完成某个任务首先构建模型,基于模型展开 业务 OOP主要应用于WEB开发,围绕OOP的主思想, 对开发过程进行了分层设计 二、AOP范式编程 AOP范式编程研究的不是层与层之间的关系,也不 是每层内部如何开发,AOP主要关注同一层面的各 个不同功能模块之间的共性功能。 AOP时代的到来,使开发模块化的思想进行了进一 步的提升,从刀耕火种的纯手工开发阶段,向半自 动化/自动化阶段进行了一个大的突破。IT研发朝着 “插拔式组件体系结构”又近了一步。 三、基本概念(重点) 1、连接点(Joinpoint) 2、切入点(Pointcut) 3、通知(Advice) 4、目标对象(Target Object) 5、AOP代理(AOP Proxy) 6、织入(Weaving) 7、切面(Aspect) 1、连接点(Joinpoint) ①、程序运行过程中,JVM负责程序运行。执行到某个 方法时,JVM能识别当前执行的是哪个方法。这些 定义在类中的方法,每个具有独立的功能,在AOP 中

曹工说Spring Boot源码(14)-- AspectJ的Load-Time-Weaving的两种实现方式细细讲解,以及怎么和Spring Instrumentation集成

≡放荡痞女 提交于 2020-02-08 15:37:42
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean Definition到底是什么,咱们对着接口,逐个方法讲解 曹工说Spring Boot源码(3)-- 手动注册Bean Definition不比游戏好玩吗,我们来试一下 曹工说Spring Boot源码(4)-- 我是怎么自定义ApplicationContext,从json文件读取bean definition的? 曹工说Spring Boot源码(5)-- 怎么从properties文件读取bean 曹工说Spring Boot源码(6)-- Spring怎么从xml文件里解析bean的 曹工说Spring Boot源码(7)-- Spring解析xml文件,到底从中得到了什么(上) 曹工说Spring Boot源码(8)-- Spring解析xml文件,到底从中得到了什么(util命名空间) 曹工说Spring Boot源码(9)-- Spring解析xml文件,到底从中得到了什么(context命名空间上) 曹工说Spring Boot源码(10)-- Spring解析xml文件,到底从中得到了什么(context:annotation-config 解析)

Spring AOP

安稳与你 提交于 2020-02-05 18:28:10
SpringAOP 和 AspectJ 的关系: 它们是两种不同的编程风格, SpringAOP 使用 xml 配置的形式配置 aop。而 AspectJ 使用 AspectJ 的注解来配置 aop aspect、JoinPoint、Pointcut、Weaving、Advice JoinPoint: 连接点。表示目标对象中的方法 Pointcut: 切点。表示连接点的集合 Weaving: 织入。把代理逻辑加入到目标对象上的过程叫织入 Advice: 通知。包括 “around”, “before” and “after 等 this、target this: 产生的代理对象 target: 被代理的原始对象 JoinPoint、ProceedingJoinPoint JoinPoint 接口可以拿到连接点的相关信息,比如:方法签名、方法参数、this、target ProceedingJoinPoint 继承自 JoinPoint,它是用来支持环绕(around)通知的,多暴露了一个 proceed() 方法,用来执行目标对象的方法。 来源: https://www.cnblogs.com/kevin-yuan/p/12264747.html

is it possible to make a @Aspect request scope in spring

孤者浪人 提交于 2020-02-03 16:21:28
问题 is it possible to make a @Aspect request scope in spring? Because it seems that it doesn't work, and it kind of makes sense; the proxy object isn't actually injected anywhere, the advice is just applied by the runtime. Just wondering... Example: @Aspect public class MyAspect { // expecting this to get autowired per request @Autowired private HttpServletRequest request; @Around(...) public void doSomething(ProceedingJoinPoint pjp) { // something here pjp.proceed(); // something there } } And