前言:
初步学习接触Spring框架使用,而在这里对自己刚学的Aop使用做个小篇幅的总结,方便日后需要用到是可以快速入手!
仅用于对自己学习个人笔记,不做任意分享,纯属个人理解不想误认子弟!
一、Aop知识点理解
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待
(1)Aspect(切面):通常是一个类,里面可以定义切入点和通知
(2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的调用
(3)Advice(通知):AOP在特定的切入点上执行的增强处理,有before,after,afterReturning,afterThrowing,around
(4)Pointcut(切入点):就是带有通知的连接点,在程序中主要体现为书写切入点表达式
二、示例如下
项目目录,基于SpringBoot项目【主要文件为如下4个标记文件】
AspectClass.java 【切面文件,用于编写一些用于给某些需要修饰方法额外嵌入的代码】
package aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * @author Luck * Aop 插入示例 */ public class AspectClass {
// 在处理业务逻辑前先执行 public void start() { System.out.println("前面插入方法"); } // 在处理完业务逻辑后执行 public void end() { System.out.println("后面插入方法"); } // 环绕执行,可通过条件来判断是否要处理执行该业务逻辑 public void around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕插入前。。。。。。。。。。"); joinPoint.proceed(); System.out.println("环绕插入后。。。。。。。。。。"); } }
AopServer.java 【业务逻辑处理类,简单输出一个标记】
package aop;
import org.springframework.stereotype.Service;
@Service
public class AopServer { public void add() { System.out.println("旧版本添加业务处理"); } }
Main.java 【测试入口文件】
package aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// 加载配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
// 通过context对象获取业务对象 AopServer server = (AopServer) context.getBean(AopServer.class);
// 调用需要修饰的方法 server.add(); } }
application.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 扫描器:告知Spring容器那些文件是Bean --> <context:component-scan base-package="aop" /> <!-- 配置切面Bean --> <bean id="aspect" class="aop.AspectClass"></bean> <aop:config> <aop:pointcut expression="execution(* aop.AopServer.add(..))" id="point" /> <aop:aspect ref="aspect">
<!-- 前置配置 --> <aop:before method="start" pointcut-ref="point" />
<!-- 后置配置 --> <aop:after method="end" pointcut-ref="point" />
<!-- 环绕配置 -->
<aop:around method="around" pointcut-ref="point"/> </aop:aspect> </aop:config> </beans>
执行顺序应该是先环绕-前置-后置,运行结果:
三、配置使用
配置aop的切入点
<aop:pointcut expression="execution(* aop.AopServer.add(..))" id="point" />
id 是切入点的标识
expression 为切入点的表达式
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)
throws-pattern?)
modifiers-pattern 修饰符 可选 public private protected
ret-type-pattern 返回类型 必选 * 代表任意类型
declaring-type-pattern 方法的声明类型
name-patterm 方法名称类型
set* 以set开头的所有的方法名称
update* 以update开头的所有的方法名称
param-pattern 参数匹配
(..) 任意多个参数,每个参数任意多个类型
(*,String) 两个参数 第一个是任意类型,第二个是String
(String,*,Integer) 三个参数,第一个是String类型,第二个是任意类型,第三个是Integer类型
throws-pattern 异常的匹配模式
例子:
execution(* aop.AopServer.*(..));
aop.AopServer下的所有的方法
execution(public * aop..*.*(..))
返回值为任意类型,修饰符为public,在aop包及子包下的所有的类的所有的方法
exectuion(* aop..*.update*(*,String))
返回值是任意类型,在aop包及子包下所有的以update开头的参数为两个,第一个为任意类型
第二个为String类型的所有类的所有的方法
execution(* dao.service..*.*(..))
第一个*表示匹配任意的方法返回值,..(两个点)表示零个或多个,上面的第一个..表示service包及其子包,第二个*表示所有类,第三个*表示所有方法,第二个..表示方法的任意参数个数