Aop:面向切面编程,在不改变业务逻辑的基础上,对横切逻辑进行扩展。
spring-aop是实现aop思想的一种框架,底层使用jdk代理和cglib代理方式实现的。
jdk代理:必须有接口
cglib代理:没有借口
主要用于:
事务管理
权限管理
日志管理
性能测试
概念:
连接点:业务逻辑方法
切点:执行切面的连接点
spring-aop使用步骤:
1)导入aop相关包:详见下发maven坐标
2)定义切面类:@Aspect注解+@componet注解,定义切面方法,5种通知类型(具体参考下方实例)
3)配置文件:<context:component-scan base-package="cn.hanhanstore.aop">
</context:component-scan>
<!-- aop相关 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
以下是相关实例
extract from https://www.cnblogs.com/jasonHome/p/6123508.html
package cn.hanhanstore.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* @Before: 前置通知, 在方法执行之前执行
* @After: 后置通知, 在方法执行之后执行
* @AfterRunning: 返回通知, 在方法返回结果之后执行
* @AfterThrowing: 异常通知, 在方法抛出异常之后
* @Around: 环绕通知, 围绕着方法执行
* @author air
*
*/
@Component
@Aspect
public class StudentAop {
/**
* 1.连接点方法执行之前执行的通知,无论结果是否异常
* 基于对象的切点的定义
* 原理:
* 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,
* Aop框架会自动的为那个方法所在的类生成一个代理对象,
* 在目标方法执行之前,执行注解的方法
*/
@Before("bean(StundentProxy)")
public void log(){
System.out.println("我是一个aop---before");
}
/**
* 2.连接点方法执行之前执行的通知,无论结果是否异常
* 基于方法的切点的定义
*/
@Before("execution(* StundentProxy.addStudent(..))")
public void log2(){
System.out.println("我是一个aop---after-");
}
/**
* 3.连接点执行方法之后执行的通知,无论结果是否异常
* 三个*依次代表:1)任意匹配修饰符及任意返回值 2)任意类 3)任意方法
*/
@After("execution(* *.*(..))")
public void log3(){
}
/**
* 4.返回通知:在切点方法又返回的时候才执行切面方法log4
*/
@AfterReturning("execution(* *.*(..))")
public void log4(){
}
/**
* 5.返回通知:获取切点方法的返回值
*/
@AfterReturning(pointcut="execution(* *.*(..)) " ,returning="result")
public void log5(){
}
/**
* 6.异常通知:连接点方法返回异常时执行
* 如果将throwable改为具体的子类,通知则会匹配切点方法抛出该类型或子类时才会执行通知方法
*/
@AfterThrowing(pointcut="execution(* *.*(..))",throwing="e")
public void log6(JoinPoint jp,Throwable e){
}
/**
* 7.环绕通知:功能最强大,可以控制连接点是否被执行。类似于动态代理的过程
* 必须要执行ProceedingJoinPoint的proceed()方法,
* 否则会出现通知被执行,但是目标方法未被执行。
* 通知需要返回目标方法的返回值,即:jp.proceed()。
* 否则会抛空指针异常
*/
@Around("execution(* *.*(..))")
public Object log7(ProceedingJoinPoint proceedingJoinPoint){
Object result = null;
String methodName = proceedingJoinPoint .getSignature().getName();
//执行目标方法
try {
//前置通知
System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs()));
result = proceedingJoinPoint.proceed();
//后置通知
System.out.println("The method " + methodName + "end with" + result);
} catch (Throwable e) {
//异常通知
System.out.println("The method occurs exception : " + e);
throw new RuntimeException();
}
//后置通知
System.out.println("The method " + methodName + "end with" + result);
return result;
}
}
spring--事务切面的实现:
1)配置切面对象
<!-- 实例化事务的切面对象 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
2) 目标类引用注解:@Transactional
@Transactional public class AddressService implements