Spring-AOP @AspectJ切点函数之target()和this()
文章目录
- 概述
- 实例
- target()
- this()
概述
target()切点函数通过判断目标类是否按类型匹配指定类来决定连接点是否匹配. 用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;
this()切点函数则通过判断代理类是否按类型匹配指定类来决定是否和切点匹配。 用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配。 this中使用的表达式必须是类型全限定名,不支持通配符。
两者都仅接受类名的入参,虽然类名可以带“+”,但是对于这两个函数来讲,使用或者不是用,效果完全相同。
实例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
target()
target(M)表示如果目标类按类型匹配于M,这目标类的所有方法都匹配切点。
target(com.xgj.IBussiness)
:IBussiness为接口,匹配接口实现类中所有方法,包括未在接口中声明的方法target(com.xgj.IBussiness)
等同于target(com.xgj.IBussiness+)
接口
package com.xgj.aop.spring.advisor.aspectJ.function.target;public interface IBussinessService {String doSomething();}
目标Bean
package com.xgj.aop.spring.advisor.aspectJ.function.target;import org.springframework.stereotype.Component;/*** * * @ClassName: BussinessService* * @Description: @Component标注的bean* * @author: Mr.Yang* * @date: 2017年9月5日 下午8:18:03*/@Component
public class BussinessService {public String doSomething() {System.out.println("doSomething executed");return "success";}
}
切面
package com.xgj.aop.spring.advisor.aspectJ.function.target;import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;/*** * * @ClassName: TargetAspect* * @Description: @Aspect标注的切面* target(com.xgj.aop.spring.advisor.aspectJ.function.* target.Class1)等同于* target(com.xgj.aop.spring.advisor.aspectJ.function* .target.Class1+)* * @author: Mr.Yang* * @date: 2017年9月5日 下午7:53:52*/@Aspect
public class TargetAspect {@AfterReturning("target(com.xgj.aop.spring.advisor.aspectJ.function.target.IBussinessService)")public void crossCuttingCode() {System.out.println("some logic is here");}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)声明Context命名空间以及Schema文件 (2)扫描类包以及应用注解定义的bean -->
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJ.function.target"/><!-- 基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面类 -->
<bean class="com.xgj.aop.spring.advisor.aspectJ.function.target.TargetAspect"/></beans>
测试类
package com.xgj.aop.spring.advisor.aspectJ.function.target;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TargetAspectTest {private ApplicationContext applicationContext;@Testpublic void test() {applicationContext = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJ/function/target/conf-target.xml");BussinessService bussinessService = applicationContext.getBean("bussinessService", BussinessService.class);// 织入增强bussinessService.doSomething();// 织入增强bussinessService.doAnother();}
}
运行结果:
2017-09-05 20:37:52,147 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@285211ef: startup date [Tue Sep 05 20:37:52 BOT 2017]; root of context hierarchy
2017-09-05 20:37:52,264 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/target/conf-target.xml]
doSomething executed
some logic is here
doAnother executed
some logic is here
this()
一般情况下,使用this()和target()来匹配定义切点,二者是等效的
target(com.xgj.IBussiness)
等价于this(com.xgj.IBussiness)
target(com.xgj.BussinessService)
等价于this(com.xgj.BussinessService)
二者的区别体现在通过引介切面产生代理对象时的具体表现。
看个例子:
接口
package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;public interface IBussinessService {String doBussiness();
}
实现类
package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;import org.springframework.stereotype.Component;/*** * * @ClassName: BussinessService* * @Description: @Component标注的bean* * @author: Mr.Yang* * @date: 2017年9月5日 下午8:18:03*/@Component
public class BussinessService implements IBussinessService {@Overridepublic String doBussiness() {System.out.println("doBussiness executed");return "success";}public String doAnother() {System.out.println("doAnother executed");return "success";}
}
另外一个通过引介切面要实现的接口
package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;public interface ITransportService {public void doTransport();
}
实现类
package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;public class TransportService implements ITransportService {@Overridepublic void doTransport() {System.out.println("doTransport executed");}}
为Bussiness添加 ITransportService接口的切面
package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.core.Ordered;/*** * * @ClassName: AddTransportForBussinessAspect* * @Description: 为Bussiness添加 ITransportService接口的切面* * @author: Mr.Yang* * @date: 2017年9月5日 下午9:18:50*/@Aspect
public class AddTransportForBussinessAspect implements Ordered {// (1)value 为BussinessService添加接口实现, (2)defaultImpl要添加的接口的默认的接口实现类@DeclareParents(value = "com.xgj.aop.spring.advisor.aspectJ.function.thisFun.BussinessService", defaultImpl = TransportService.class)public ITransportService iTransportService; // (3) 要实现的目标接口@Overridepublic int getOrder() {return 2;}}
横切逻辑切面
package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;/*** * * @ClassName: ThisAspect* * @Description: @Aspectn标注的切面* * * @author: Mr.Yang* * @date: 2017年9月5日 下午8:50:26*/@Aspect
public class ThisAspect implements Ordered {// 织入任何运行期对象为ITransportService的Bean中@AfterReturning("this(com.xgj.aop.spring.advisor.aspectJ.function.thisFun.ITransportService)")public void corssCuttingCode() {System.out.println("some logic is here \n ");}@Overridepublic int getOrder() {return 1;}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)声明Context命名空间以及Schema文件 (2)扫描类包以及应用注解定义的bean -->
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJ.function.thisFun"/><!-- 基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面类 -->
<bean class="com.xgj.aop.spring.advisor.aspectJ.function.thisFun.ThisAspect"/>
<bean class="com.xgj.aop.spring.advisor.aspectJ.function.thisFun.AddTransportForBussinessAspect"/></beans>
测试类
package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ThisAspectTest {private ApplicationContext applicationContext;@Testpublic void test() {applicationContext = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJ/function/thisFun/conf-this.xml");BussinessService bussinessService = (BussinessService) applicationContext.getBean("bussinessService");// 匹配 thisbussinessService.doBussiness();// 匹配 thisbussinessService.doAnother();// 匹配 this((ITransportService) bussinessService).doTransport();}
}
运行结果
2017-09-05 22:24:03,301 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24177697: startup date [Tue Sep 05 22:24:03 BOT 2017]; root of context hierarchy
2017-09-05 22:24:03,397 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/thisFun/conf-this.xml]
doBussiness executed
some logic is here doAnother executed
some logic is here doTransport executed
some logic is here
如果有多个切面,注意多切面织入的顺序,如果不加织入的顺序, doTransport 方法的切面无法织入。
可见代理对象的方法都织入了this()函数定义的切面。
Spring-AOP @AspectJ切点函数之target()和this()相关推荐
- Spring-AOP @AspectJ切点函数导读
概述 annotation execution args和args within within和target target和this 概述 切点函数是AspectJ表达式语言的核心,也是使用@Aspe ...
- 关于 Spring AOP (AspectJ) 你该知晓的一切
[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/54629058 出自[zejian ...
- @AspectJ切点函数详解
@AspectJ切点函数详解 1 方法切点函数 1.1 execution() 1.1.1 通过方法签名定义切点 1.1.2 通过类定义切点 1.1.3 通过类包定义切点 1.1.4 通过方法入参定义 ...
- @AspectJ切点函数之execution()
@AspectJ切点函数之execution() execution()是最常用的切点函数,语法如下: execution(<修饰符模式>?<返回类型模式><方法名模式& ...
- 关于Spring AOP中切点修饰符@annotation、@args与args约束说明
前言 于其说这是一篇文章,不如说这是一篇笔记,主要介绍了@annotation.@args和args的作用以及一些坑点.这里主要记录一些项目用到的,没有成一套体系,网上其他文章对Spring AOP的 ...
- Spring AOP + AspectJ Annotation Example---reference
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- Spring AOP / AspectJ AOP 的区别?
Spring AOP / AspectJ AOP 的区别? Spring AOP属于运行时增强,而AspectJ是编译时增强. Spring AOP基于代理(Proxying),而AspectJ基于字 ...
- Spring-AOP @AspectJ切点函数之@within()和@target
文章目录 概述 @target(M)的匹配规则 @within(M)的匹配规则 实例 @target @within 注意事项 概述 除了@annotation和@args外,还有另外两个用于注解的切 ...
- 说说 Spring 支持的 AspectJ 切点函数
1 @annotation() 它表示标注了某个注解的所有方法. 假设有一个接口 Cook,它有两个实现类 CookA.CookB: Cook: public interface Cook {/*** ...
最新文章
- 【组队学习】【32期】Linux实践
- Codeforces 771C
- oracle设置超时时间
- QT的QAxFactory类的使用
- [Java基础]Date类基础
- Intel Core Enhanced Core架构/微架构/流水线 (4) - 流水线前端概述
- mybatis工作笔记003---Mybatis批量删除deleteByIds的用法
- Makefile编写练习题
- android gallery滑动监听,利用RecyclerView和ViewPager实现GalleryView可无限左滑右滑
- 两个很重要的极限和夹逼准则
- python3 print和format函数
- 从血红细胞衰老看中老年疑难病和亚健康--
- ssm+Vue计算机毕业设计益学(程序+LW文档)
- ssl证书是什么,ssl证书有什么作用
- http请求报错context deadline exceeded (Client.Timeout exceeded while awaiting headers)
- 常用快捷键cmd常用dos命令java安装
- 关于服务端与客户端的数据交互
- 单例模式-高性能单例模式
- 26日pr更新 祝愿大家的站点更上一层楼
- DataGridView的列宽设置(自动调整列宽)
热门文章
- java contions_Java数据结构与算法
- gtk移植到嵌入式_物联网时代的盛行,应届毕业生是学嵌入式好呢,还是安卓或ios呢?...
- 欧拉角推算旋转矩阵的问题
- 深度学习100例 | 第34天:如何进行数据增强?
- Python编程基础:第二十一节 函数返回Return
- 计算机科学和机器学习中的代数学、拓扑学、微积分以及最优化理论
- 邮件协议(SMTP)性能测试总结(Foxmail邮箱)
- 在路上---一个平凡人的2015年总结及2016年展望
- java中如何忽略字符串中的转义字符--转载
- Java 理论与实践: 用弱引用堵住内存泄漏---转载