AspectJ

1. 介绍

AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持,@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面,它是一种新版本Spring框架,建议使用AspectJ方式来开发AOP。主要用途:自定义开发

2.切入点表达式

  为了能够灵活定义切入点位置,Spring AOP提供了多种切入点指示符。

execution———用来匹配执行方法的连接点

  语法结构:   execution(   方法修饰符  方法返回值  方法所属类 匹配方法名 (  方法中的形参表 )  方法申明抛出的异常  )

  其中红色字体的部分时不能省略的,各部分都支持通配符 “*” 来匹配全部

  比较特殊的为形参表部分,其支持两种通配符

  •   "*":代表一个任意类型的参数;
  •   “..”:代表零个或多个任意类型的参数。

  例如:

    ()匹配一个无参方法

    (..)匹配一个可接受任意数量参数和类型的方法

    (*)匹配一个接受一个任意类型参数的方法

    (*,Integer)匹配一个接受两个参数的方法,第一个可以为任意类型,第二个必须为Integer。

  下面举一些execution的使用实例:

分类 示例 描述
通过方法签名定义切入点 execution(public * * (..)) 匹配所有目标类的public方法,第一个*为返回类型,第二个*为方法名
execution(* save* (..)) 匹配所有目标类以save开头的方法,第一个*代表返回类型
execution(**product(*,String)) 匹配目标类所有以product结尾的方法,并且其方法的参数表第一个参数可为任意类型,第二个参数必须为String
通过类定义切入点 execution(* aop_part.Demo1.service.*(..)) 匹配service接口及其实现子类中的所有方法
通过包定义切入点 execution(* aop_part.*(..)) 匹配aop_part包下的所有类的所有方法,但不包括子包
execution(* aop_part..*(..)) 匹配aop_part包下的所有类的所有方法,包括子包。(当".."出现再类名中时,后面必须跟“*”,表示包、子孙包下的所有类)
execution(* aop_part..*.*service.find*(..)) 匹配aop_part包及其子包下的所有后缀名为service的类中,所有方法名必须以find为前缀的方法
通过方法形参定义切入点 execution(*foo(String,int)) 匹配所有方法名为foo,且有两个参数,其中,第一个的类型为String,第二个的类型为int
execution(* foo(String,..)) 匹配所有方法名为foo,且至少含有一个参数,并且第一个参数为String的方法(后面可以有任意个类型不限的形参)

within————通过类匹配模式申明切入点(只能通过类型匹配连接点)

    例如:within(aop_part..*)             表示匹配包aop_part以及子包的所有方法

    由于execution可以匹配包、类、方法,而within只能匹配包、类,因此execution完全可以代替within的功能。


this————限定AOP代理必须时指定类型的实例,用于匹配该对象的所有连接点

    例如:this(aop_part.service.GodService)        表示匹配了GodService接口的代理对象的所有连接点


target————通过判断目标类的类型确定判断的是否匹配

    this通过判断代理类的类型来决定是否和切入点匹配,两者限定的对象都是指定类型的实例。

    例如: target(aop_part.service.GodService)      表示匹配实现了GodService接口的目标对象的所有连接点


args————用于对连接点的参数类型进行限制,要求参数类型时指定类型的实例

    例如:args(aop_part.service)                     表示匹配时,出入的参数类型时service的方法

    其与execution(**(aop_part.service))的区别为,execution针对的时方法签名,而args针对的是运行时的实际参数类型。

    args既匹配buyGoods(service newService),也匹配buyGoods(Buyservice newService)   <Buyservice为service的子类>

    execution只匹配buyGoods(service newService)


组合切入点

    支持 &&、 || 、!

    与其他语言所代表的意思相同

    例:args(aop_part.service)  &&execution(**(aop_part.service))

3.AspectJ 通知类型

aop联盟定义通知类型,具有特性接口,必须实现,从而确定方法名称。

aspectj 通知类型,只定义类型名称。已经方法格式。

个数:6种,知道5种,掌握1中。

before:前置通知(应用:各种校验)在方法执行前执行,如果通知抛出异常,阻止方法运行

afterReturning:后置通知(应用:常规数据处理)方法正常返回后执行,如果方法中抛出异常,通知无法执行必须在方法执行后才执行,所以可以获得方法的返回值。

around:环绕通知(应用:十分强大,可以做任何事情)方法执行前后分别执行,可以阻止方法的执行必须手动执行目标方法

afterThrowing:抛出异常通知(应用:包装异常信息)方法抛出异常后执行,如果方法没有抛出异常,无法执行

after:最终通知(应用:清理现场) 方法执行完毕后执行,无论方法中是否出现异常

Spring Aop实例:

方式一 :注解配置 

UserService.java

package com.zk.b_annotation;

public interface UserService {

	public void addUser();
public String updateUser();
public void deleteUser(); }

UserServiceImpl.java

package com.zk.b_annotation;

import org.springframework.stereotype.Service;

@Service("userServiceId")
public class UserServiceImpl implements UserService { @Override
public void addUser() {
System.out.println("d_aspect.b_anno addUser");
} @Override
public String updateUser() {
System.out.println("d_aspect.b_anno updateUser");
int i = 1/ 0;
return "阳志就是";
} @Override
public void deleteUser() { System.out.println("d_aspect.b_anno deleteUser");
}
}

  

切面

MyAspect.java

package com.zk.b_annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* 切面类,含有多个通知
*/
@Component
@Aspect
public class MyAspect { //前置通知
// @Before("execution(* com.itheima.d_aspect.b_anno.UserServiceImpl.*(..))")
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知" + joinPoint.getSignature().getName());
} //声明公共切入点
@Pointcut("execution(* com.zk.b_annotation.UserServiceImpl.*(..))")
private void myPointCut(){
} // @AfterReturning(value="myPointCut()" ,returning="ret")
@AfterReturning("myPointCut()")
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知" + joinPoint.getSignature().getName() + " , -->" + ret);
} // @Around(value = "myPointCut()")
@Around("myPointCut")
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("前置通知");
//手动执行目标方法
Object obj = joinPoint.proceed(); System.out.println("后置通知");
return obj;
} // @AfterThrowing(value="execution(* com.itheima.d_aspect.b_anno.UserServiceImpl.*(..))" ,throwing="e")
@AfterThrowing("myPointCut()")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知" + e.getMessage());
} @After("myPointCut()")
public void myAfter(JoinPoint joinPoint){
System.out.println("后置通知");
} }

  

beans.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 1.扫描 注解类 -->
<context:component-scan base-package="com.zk.b_annotation"></context:component-scan> <!-- 2.确定 aop注解生效 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

  

TestAspectAnno.java

package com.zk.b_annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAspectAnno { @Test
public void demo01(){
String xmlPath = "com/zk/b_annotation/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); //获得目标bean
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
userService.updateUser();
userService.deleteUser();
} }

  

运行效果图:

方式二:xml配置 

UserService.java

package com.itheima.d_aspect.a_xml;

public interface UserService {

	public void addUser();
public String updateUser();
public void deleteUser(); }

 

UserServiceImpl.java

package com.itheima.d_aspect.a_xml;

public class UserServiceImpl implements UserService {

	@Override
public void addUser() {
System.out.println("d_aspect.a_xml addUser");
} @Override
public String updateUser() {
System.out.println("d_aspect.a_xml updateUser");
int i = 1/ 0;
return "阳志就是屌";
} @Override
public void deleteUser() { System.out.println("d_aspect.a_xml deleteUser");
} }

  

MyAspect.java

package com.itheima.d_aspect.a_xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 切面类,含有多个通知
*/
public class MyAspect { public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知 : " + joinPoint.getSignature().getName());
} public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret);
} public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("前");
//手动执行目标方法
Object obj = joinPoint.proceed(); System.out.println("后");
return obj;
} public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知 : " + e.getMessage());
} public void myAfter(JoinPoint joinPoint){
System.out.println("最终通知");
} }

  

TestAspectXml.java

package com.itheima.d_aspect.a_xml;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAspectXml { @Test
public void demo01(){
String xmlPath = "com/itheima/d_aspect/a_xml/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); //获得目标类
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
userService.updateUser();
userService.deleteUser();
} }

  

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1 创建目标类 -->
<bean id="userServiceId" class="com.itheima.d_aspect.a_xml.UserServiceImpl"></bean>
<!-- 2 创建切面类(通知) -->
<bean id="myAspectId" class="com.itheima.d_aspect.a_xml.MyAspect"></bean>
<!-- 3 aop编程
<aop:aspect> 将切面类 声明“切面”,从而获得通知(方法)
ref 切面类引用
<aop:pointcut> 声明一个切入点,所有的通知都可以使用。
expression 切入点表达式
id 名称,用于其它通知引用
-->
<aop:config>
<aop:aspect ref="myAspectId">
<aop:pointcut expression="execution(* com.itheima.d_aspect.a_xml.UserServiceImpl.*(..))" id="myPointCut"/> <!-- 3.1 前置通知
<aop:before method="" pointcut="" pointcut-ref=""/>
method : 通知,及方法名
pointcut :切入点表达式,此表达式只能当前通知使用。
pointcut-ref : 切入点引用,可以与其他通知共享切入点。
通知方法格式:public void myBefore(JoinPoint joinPoint){
参数1:org.aspectj.lang.JoinPoint 用于描述连接点(目标方法),获得目标方法名等
例如: -->
<aop:before method="myBefore" pointcut-ref="myPointCut"/>
<!-- 3.2后置通知 ,目标方法后执行,获得返回值
<aop:after-returning method="" pointcut-ref="" returning=""/>
returning 通知方法第二个参数的名称
通知方法格式:public void myAfterReturning(JoinPoint joinPoint,Object ret){
参数1:连接点描述
参数2:类型Object,参数名 returning="ret" 配置的
例如: -->
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" />
<!-- 3.3 环绕通知
<aop:around method="" pointcut-ref=""/>
通知方法格式:public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
返回值类型:Object
方法名:任意
参数:org.aspectj.lang.ProceedingJoinPoint
抛出异常
执行目标方法:Object obj = joinPoint.proceed();
例如: -->
<aop:around method="myAround" pointcut-ref="myPointCut"/>
<!-- 3.4 抛出异常
<aop:after-throwing method="" pointcut-ref="" throwing=""/>
throwing :通知方法的第二个参数名称
通知方法格式:public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
参数1:连接点描述对象
参数2:获得异常信息,类型Throwable ,参数名由throwing="e" 配置
例如: -->
<aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/>
<!-- 3.5 最终通知 -->
<aop:after method="myAfter" pointcut-ref="myPointCut"/> </aop:aspect>
</aop:config>
</beans>

  

运行效果:

Spring-Aop编程(三)-AspectJ的更多相关文章

  1. Spring AOP编程(二)-AOP实现的三种方式

    AOP的实现有三种方式: l         aop底层将采用代理机制进行实现. l         接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l         实现类: ...

  2. 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记

    AOP = Aspect Oriental Programing  面向切面编程 文章里不讲AOP术语,什么连接点.切点.切面什么的,这玩意太绕,记不住也罢.旨在以简单.直白的方式理解AOP,理解Sp ...

  3. Spring AOP支持的AspectJ切入点语法大全

    原文出处:http://jinnianshilongnian.iteye.com/blog/1420691 Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的, ...

  4. spring AOP 之四:@AspectJ切入点标识符语法详解

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  5. spring AOP编程--AspectJ注解方式

    1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...

  6. spring AOP 编程--AspectJ注解方式 (4)

    1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...

  7. 二)Spring AOP编程思想与动态代理

    一.aop编程思想 1.面向切面,就是能够不动源码的情况下,从横切面切入新的代码功能. 2.实现原理是动态代理 动态代理的步骤 a.写生产厂家,实现接口,代理只能代理接口 b.动态代理类实现Invoc ...

  8. Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式

    背景: 1)指定切面优先级示例:有的时候需要对一个方法指定多个切面,而这多个切面有时又需要按照不同顺序执行,因此,切面执行优先级别指定功能就变得很实用. 2)重复使用切入点表达式:上一篇文章中,定义前 ...

  9. Spring AOP编程(一)-AOP介绍

    1. AOP介绍 l         在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...

随机推荐

  1. TODO:rds数据库实例

    rds数据库实例怎么创建的 rds数据库实例高可用是怎么实现的 rds备份是怎么实现的 参考: https://www.cnblogs.com/jackyzzy/p/7384355.html http ...

  2. Java代码手段防止非法请求——防盗链

    Java代码手段防止非法请求,思路如下:        1. 获取到当前请求的域名,如www.a.com        2. 获取到请求资源的上一个地址        3. 判断上一个地址是否为空,如 ...

  3. js获取URL里的参数

    第一种 通过正则获取URL中指定的参数 /** * 获取指定的URL参数值 * URL:http://www.xxx.com/index?name=123 * 参数:param URL参数 * 调用方 ...

  4. linux - redis-trib.rb 命令详解

    参考网站 http://www.cnblogs.com/ivictor/p/9768010.html 简介 redis-trib.rb是官方提供的Redis Cluster的管理工具,无需额外下载,默 ...

  5. SaltStack之return与job管理

    目录 1. SaltStack组件之return 1.1 return流程 1.2 使用mysql作为return存储方式 2. job cache 2.1 job cache流程 2.2 job管理 ...

  6. python3 读取串口数据

    python3 读取串口数据 demo import serial import time ser = serial.Serial("COM3",115200,timeout = ...

  7. [NOI2010] 超级钢琴 - 贪心,堆,ST表

    这也算是第K大问题的套路题了(虽然我一开始还想了个假算法),大体想法就是先弄出最优的一批解,然后每次从中提出一个最优解并转移到一个次优解,用优先队列维护这个过程即可. 类似的问题很多,放在序列上的,放 ...

  8. TD - 单选框 - RadioButton

    基本方法 Html - 默认选中 //checked="true" - 默认选中 <input dojoType="bootstrap.form.RadioButt ...

  9. Centos7 安装 GitLab 代码管理服务器

    一.安装依赖 yum -y install policycoreutils openssh-server openssh-clients postfix 二.启动postfix,并设置开机自启动 sy ...

  10. OpenCV函数 重映射

    重映射是什么意思? 把一个图像中一个位置的像素放置到另一个图片指定位置的过程. 为了完成映射过程, 有必要获得一些插值为非整数像素坐标,因为源图像与目标图像的像素坐标不是一一对应的. 我们通过重映射来 ...