Spring AOP 之二:Pointcut注解表达式
简介
在Spring AOP概述中我们重点注意的是AOP的整体流程和Advice,简化了一些其他的东西,其中就有一些对灵活应用Spring AOP很重要的知识点,例如Pointcut表达式,下面就介绍一下Spring AOP的Pointcut表达式。
如果你对Pointcut表达式的作用还不是很了解,可以先看一下Spring AOP概述,也可以先了解一下匹配规则,后面会有一些具体的例子来帮助理解。
我们使用最多的就是execution表示了,下面就从execution表达式开始介绍吧。
注意:把designators翻译为表达式也许不太合适,所以不必纠结这个问题,只需要知道在看到Spring AOP中的designators知道对应的是个什么东西就可以了。管它是表达式还是指示器只是一个代称而已。
execution表达式
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
throws-pattern?)
上面的就是execution表达式的格式,execution匹配的就是连接点(Joinpoint,一般是方法),看上面的表达式execution是固定的,方法的修饰符是可选的,返回类型是必须的,定义的全限类型也是可选的,名称是必须的,参数是必须的,这些都可以使用通配符。
任何的public方法
execution(public * *(..))
以set开始的方法
execution(* set*(..))
定义在cn.freemethod.business.pack.Say接口中的方法
execution(* cn.freemethod.business.pack.Say.*(..))
任何cn.freemethod.business包中的方法
execution(* cn.freemethod.business.*.*(..))
任何定义在com.xyz.service包或者其子包中的方法
execution(* cn.freemethod.business..*.*(..))
其他表达式
任何在com.xyz.service包中的方法
within(com.xyz.service.*)
任何定义在com.xyz.service包或者其子包中的方法
within(com.xyz.service..*)
任何实现了com.xyz.service.AccountService接口中的方法
this(com.xyz.service.AccountService)
任何目标对象实现了com.xyz.service.AccountService的方法
target(com.xyz.service.AccountService)
一般情况下代理类(Proxy)和目标类(Target)都实现了相同的接口,所以上面的2个基本是等效的。
有且只有一个Serializable参数的方法
args(java.io.Serializable)
只要这个参数实现了java.io.Serializable接口就可以,不管是java.io.Serializable还是Integer,还是String都可以。
目标(target)使用了@Transactional注解的方法
@target(org.springframework.transaction.annotation.Transactional)
目标类(target)如果有Transactional注解中的所有方法
@within(org.springframework.transaction.annotation.Transactional)
任何方法有Transactional注解的方法
@annotation(org.springframework.transaction.annotation.Transactional)
有且仅有一个参数并且参数上类型上有Transactional注解
@args(org.springframework.transaction.annotation.Transactional)
注意是参数类型上有Transactional注解,而不是方法的参数上有注解。
-- bean的名字为tradeService中的方法
bean(simpleSay)
bean名字为simpleSay中的所有方法。
-- bean名字能匹配
bean(*Impl)
bean名字匹配*Impl的bean中的所有方法。
实例
这个实例是对Spring AOP概述中介绍的实例进行了扩展。当然下面给的实例也可以独立测试,最好是一个一个测试观察输出了验证,多个输出结果不够直观。
下面的图片是项目工程目录结构:

下面的代码列表因为比较多,只能展示部分代码。
--OtherPointcutAspect:
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class OtherPointcutAspect {
// @After("cn.freemethod.pointcut.OtherPointcut.argsSerializable()")
public void argsSerialize(){
System.out.println("OtherPointcutAspect argsSerialize after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.inBusinessPackage()")
public void inBusinessPackage(){
System.out.println("OtherPointcutAspect inBusinessPackage after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.inBusinessOrSubPackage()")
public void inBusinessOrSubPackage(){
System.out.println("OtherPointcutAspect inBusinessOrSubPackage after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.proxyImplementHaveResultBusinessInteferce()")
public void proxyImplementHaveResultBusinessInteferce(){
System.out.println("OtherPointcutAspect proxyImplementHaveResultBusinessInteferce after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.targetImplementBusinessInteferce()")
public void targetImplementBusinessInteferce(){
System.out.println("OtherPointcutAspect targetImplementBusinessInteferce after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.targetHaveTransactional()")
public void targetHaveTransactional(){
System.out.println("OtherPointcutAspect targetHaveTransactional after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.targetDeclareHaveTransactional()")
public void targetDeclareHaveTransactional(){
System.out.println("OtherPointcutAspect targetDeclareHaveTransactional after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.methodHaveTransactional()")
public void methodHaveTransactional(){
System.out.println("OtherPointcutAspect methodHaveTransactional after...");
}
@After("cn.freemethod.pointcut.OtherPointcut.argsHaveValueAnnotation()")
public void argsHaveTransactional(){
System.out.println("OtherPointcutAspect argsHaveValueAnnotation after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.beanName()")
public void beanName(){
System.out.println("OtherPointcutAspect beanName after...");
}
// @After("cn.freemethod.pointcut.OtherPointcut.matchBeanName()")
public void matchBeanName(){
System.out.println("OtherPointcutAspect matchBeanName after...");
}
}
上面是一个测试Pointcut表达式的切面,很多Advice都注释了,请为了从输出中直观的了解Pointcut表达式匹配到了什么方法(Joinpoint),请根据自己的需要测试的Pointcut表达式取消添加注释。
--ExecutionPointcutAspect:
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class ExecutionPointcutAspect {
@Before("cn.freemethod.pointcut.ExecutionPointcut.allPublicMethod()")
public void allPublicBefore(){
System.out.println("PointcutExpressionAspect allPublicBefore...");
}
@After("cn.freemethod.pointcut.ExecutionPointcut.allStartSayMethod()")
public void sayAfter(){
System.out.println("PointcutExpressionAspect sayAfter...");
}
@AfterReturning("cn.freemethod.pointcut.ExecutionPointcut.allInSayInterfaceMethod()")
public void allInSayInterfaceMethod(){
System.out.println("PointcutExpressionAspect allInSayInterfaceMethod...");
}
@Before("cn.freemethod.pointcut.ExecutionPointcut.allInBusinessPackage()")
public void allInBusinessPackageBefore(){
System.out.println("PointcutExpressionAspect all business before...");
}
@After("cn.freemethod.pointcut.ExecutionPointcut.allInBusinessOrSubPackage()")
public void allallInBusinessOrSubPackageAfter(){
System.out.println("PointcutExpressionAspect allallInBusinessOrSubPackageAfter...");
}
}
---ParamBean:
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class ParamBean {
}
--HaveResultBusinessImpl:
import org.springframework.stereotype.Service;
import cn.freemethod.business.HaveResultBusiness;
@Service
public class HaveResultBusinessImpl implements HaveResultBusiness {
@Override
public Integer getResult(Integer div) {
System.out.println("HaveResultBusinessImpl getResult");
Integer result = 100 / div;
return result;
}
}
--IOtherPointcutImpl:
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.freemethod.bean.ParamBean;
import cn.freemethod.business.pack.IOtherPointcut;
@Transactional
@Service
public class IOtherPointcutImpl implements IOtherPointcut {
@Override
public Integer printInteger(Integer arg) {
System.out.println("IOtherPointcutImpl printInteger");
return arg;
}
@Override
public void withAnotationParam(ParamBean arg) {
System.out.println("IOtherPointcutImpl withAnotationParam");
}
}
--SimpleSay:
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.freemethod.business.pack.Say;
@Transactional
@Service
public class SimpleSay implements Say {
@Override
public String sayChiness(String name) {
String result = "你好:" + name;
System.out.println(result);
return result;
}
@Override
public String sayEnglish(String name) {
String result = "Hello " + name;
System.out.println(result);
return result;
}
@Transactional
@Override
public String whatSay() {
System.out.println("what say");
return "what say";
}
}
--IOtherPointcut:
import cn.freemethod.bean.ParamBean;
public interface IOtherPointcut {
Integer printInteger(Integer arg);
void withAnotationParam(ParamBean arg);
}
--Say:
public interface Say {
String sayChiness(String name);
String sayEnglish(String name);
String whatSay();
}
--HaveResultBusiness:
public interface HaveResultBusiness {
Integer getResult(Integer div);
}
--AspectConfig:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"cn.freemethod"})
public class AspectConfig {
}
--ExecutionPointcut:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class ExecutionPointcut {
@Pointcut("execution(public * *(..))")
public void allPublicMethod(){}
@Pointcut("execution(* say*(..))")
public void allStartSayMethod(){}
@Pointcut("execution(* cn.freemethod.business.pack.Say.*(..))")
public void allInSayInterfaceMethod(){}
@Pointcut("execution(* cn.freemethod.business.*.*(..))")
public void allInBusinessPackage(){}
@Pointcut("execution(* cn.freemethod.business..*.*(..))")
public void allInBusinessOrSubPackage(){}
}
--OtherPointcut :
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class OtherPointcut {
@Pointcut("within(cn.freemethod.business.impl.*)")
public void inBusinessPackage(){}
@Pointcut("within(cn.freemethod.business..*)")
public void inBusinessOrSubPackage(){}
@Pointcut("this(cn.freemethod.business.HaveResultBusiness)")
public void proxyImplementHaveResultBusinessInteferce(){}
@Pointcut("target(cn.freemethod.business.HaveResultBusiness)")
public void targetImplementBusinessInteferce(){}
@Pointcut("args(java.io.Serializable)")
// @Pointcut("args(java.lang.Integer)")
public void argsSerializable(){}
@Pointcut("@target(org.springframework.transaction.annotation.Transactional)")
public void targetHaveTransactional(){}
@Pointcut("@within(org.springframework.transaction.annotation.Transactional)")
public void targetDeclareHaveTransactional(){}
@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")
public void methodHaveTransactional(){}
@Pointcut("@args(org.springframework.transaction.annotation.Transactional)")
public void argsHaveValueAnnotation(){}
@Pointcut("bean(simpleSay)")
public void beanName(){}
@Pointcut("bean(*Impl)")
public void matchBeanName(){}
}
--AnnotationConfigStart:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import cn.freemethod.business.HaveResultBusiness;
import cn.freemethod.business.pack.Say;
import cn.freemethod.config.AspectConfig;
public class AnnotationConfigStart {
public static void main(String[] args) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AspectConfig.class);
HaveResultBusiness haveResultBusiness = context.getBean(HaveResultBusiness.class);
haveResultBusiness.getResult(100);
Say say = context.getBean(Say.class);
say.sayChiness("tim");
say.sayEnglish("tim");
say.whatSay();
context.close();
}
}
--PointcutExpressionStart:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import cn.freemethod.bean.ParamBean;
import cn.freemethod.business.HaveResultBusiness;
import cn.freemethod.business.pack.IOtherPointcut;
import cn.freemethod.business.pack.Say;
import cn.freemethod.config.AspectConfig;
public class PointcutExpressionStart {
public static void main(String[] args) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AspectConfig.class);
IOtherPointcut iOtherPointcut = context.getBean(IOtherPointcut.class);
iOtherPointcut.printInteger(new Integer(10));
iOtherPointcut.withAnotationParam(new ParamBean());
HaveResultBusiness haveResultBusiness = context.getBean(HaveResultBusiness.class);
haveResultBusiness.getResult(100);
Say say = context.getBean(Say.class);
say.sayChiness("tim");
say.sayEnglish("tim");
say.whatSay();
context.close();
}
}
Spring AOP 之二:Pointcut注解表达式的更多相关文章
- spring AOP 之二:@AspectJ注解的3种配置
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
- 深入理解Spring AOP之二代理对象生成
深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解
引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...
- 【Spring AOP】Spring AOP之如何通过注解的方式实现各种通知类型的AOP操作进阶篇(3)
一.切入点表达式的各种类型 切入点表达式的作用:限制连接点的匹配(满足时对应的aspect方法会被执行) 1)execution:用于匹配方法执行连接点.Spring AOP用户可能最经常使用exec ...
- spring AOP (使用AspectJ的注解方式 的aop实现) (6)
目录 一.在 Spring 中启用 AspectJ 注解支持 二.AspectJ 支持 5 种类型的通知注解: 2.1.使用之前的 计算器接口和实现类 ArithmeticCalculator.jav ...
- Spring AOP(5)-- 注解
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...
- Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容
© 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...
- Spring AOP编程(二)-AOP实现的三种方式
AOP的实现有三种方式: l aop底层将采用代理机制进行实现. l 接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l 实现类: ...
随机推荐
- Maven整合JaCoCo和Sonar,看看你的测试写够了没
1 简介 单元测试是保证代码质量的重要一环,而如何衡量单元测试写得好不好呢?覆盖率(Coverage)是一个重要指标.而JaCoCo则是专门为Java提供的用于检测测试覆盖率的工具,英文全称为Java ...
- Mac更新系统后提示xcrun error
更新mac os后无法运行git,gcc等命令,出现missing xcrun错误. 这是因为更新系统后会卸载xcode,重新安装后会缺失xcode的命令行工具(command line tools) ...
- UVALive8518 Sum of xor sum
题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...
- 设计模式系列之单例模式(Singleton Pattern)——确保对象的唯一性
模式概述 模式定义 模式结构图 饿汉式单例与懒汉式单例 饿汉式单例 懒汉式单例 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 适用场景 说明:设计模式系列文章是读刘伟所著 ...
- layui加tp5图片上传实例
<div class="layui-fluid"> <div class="layui-row"> <form class=&qu ...
- WordPress美化百度分享默认图标
因代码中使用了Font Awesome字体图标,如果你的主题没有加载字体图标,可以到WP后台--插件--安装插件页面搜索:Font Awesome 4 Menus 安装并启用,才能显示替换后的图标. ...
- JSP页面基础用法和方法查询
导包jar:jstl.jar standard.jar jsp嵌套java代码,使用jsp脚本:1)<%java代码%> ----- 内部的java代码翻译到service方法的内部 ...
- 自动网络搜索(NAS)在语义分割上的应用(二)
前言: 本文将介绍如何基于ProxylessNAS搜索semantic segmentation模型,最终搜索得到的模型结构可在CPU上达到36 fps的测试结果,展示自动网络搜索(NAS)在语义分割 ...
- Intellij IDEA 2020.1.1 破解 永久有效 亲测100%成功
申明:本教程 WebStorm 破解补丁.激活码均收集于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除. 前言 作为一个有强迫症的码农,怎么能忍受自己的开发工具跟不上潮流呢?笔者以前一直用 ...
- 深入理解JS:var、let、const的异同
目录 序言 var 与 let 的区别 作用域 重复声明 绑定全局对象 变量提升与暂存死区 let 与 const 异同 参考 1.序言 var.let 和 const 都是 JavaScript 中 ...