SpringAOP

Spring AOP 使用纯 Java 实现,不需要专门的编译过程和类加载器,在
运行期通过代理方式向目标类织入增强代码。

  1. AOP 相关术语
  • Joinpoint(连接点): 所谓连接点是指那些被拦截到的点。在 spring 中,这些点指的是方法,因为 spring只支持方法类型的 连 接点。

  • Pointcut(切入点): 所谓切入点是指我们要对哪些 Joinpoint 进 行拦截的定义。

  • Advice(通知/增强): 所谓通知是指拦截到 Joinpoint 之后所要 做的事情就是通知。
    通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。

  • Introduction(引介): 引介是一种特殊的通知在不修改类代码的 前提下, Introduction
    可以在运行期为类动态地添加一些方 法或 Field。

  • Target(目标对象): 代理的目标对象。 Weaving(织入): 是指 把增强应用到目标对象来创建新的代理对象的过程。
    spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载 期织入。

  • Proxy(代理): 一个类被 AOP 织入增强后,就产生一个结果代 理类。

  • Aspect(切面): 是切入点和通知(引介)的结合

环境配置:有两种方法,
方法一:下载相关的jar包
在这里插入图片描述
方法二:使用maven自动下载相关的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gem.demo</groupId>
  <artifactId>spring0331</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 添加依赖 -->
  <dependencies>
  	<!-- spring核心 -->
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>5.2.2.RELEASE</version>
	</dependency>
	<!-- 引用aspectj -->
	<dependency>
    	<groupId>org.aspectj</groupId>
    	<artifactId>aspectjweaver</artifactId>
    	<version>1.8.13</version>
    	</dependency>
  </dependencies>
</project>

2.创建spring的配置文件并导入约束

xmlns:aop="http://www.springframework.org/schema/aop"
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd">
 

完整的配置文件:要注意加入的顺序
在这里插入图片描述

<?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">



</beans>

业务层;

package com.hadwinling.service;

public interface StudentService {
	void save();
	void delete(int id);
	void update();
}

业务层实现接口

package com.hadwinling.service.impl;

import com.hadwinling.service.StudentService;

public class StudentServiceImpl implements StudentService {

	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("业务层保存数据成功");
//		throw new RuntimeException("出错了");

	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		System.out.println("业务层删除数据");
	}

	@Override
	public void update() {
		System.out.println("业务层修改信息");
	}

}

增强/通知(我这里将其放在util工具包下)

package com.hadwinling.util;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAdvice {
	
	public void logger() {
		System.out.println("打印执行的sql语句");
	}
	public void txManage() {
		System.out.println("事务管理");
	}
	
	public void afterReturn() {
		System.out.println("正常结束的后置通知");
	}
	public void printExecption() {
		System.out.println("异常结束的后置通知");
	}
	/*
	 * 环绕通知
	 *  ProceedingJoinPoint 
	 */
	public Object around(ProceedingJoinPoint pjp) {
		try {
			//获取参数
			Object[] args = pjp.getArgs();
			//执行程序
			Object obj = pjp.proceed(args);
			//自定义通知操作
			logger();
			//自定义后置通知
			txManage();
			return obj;
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}

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
">
	<!-- 配置业务bean -->
	<bean id="studentService" class="com.hadwinling.service.impl.StudentServiceImpl"></bean>
	<!-- 配置通知bean -->
	<bean id="log" class="com.hadwinling.util.MyAdvice"></bean>
	<!-- spring aop -->
	<aop:config>
		<!-- 切入点:对连接点进行定义 
			expression:定义连接点(方法)的表达式
			* com.gem.demo.service.impl.*.*(..):
			com.gem.demo.service.impl包中的所有类的所有方法,任意参数,任意返回值类型 都是连接点,都可以被拦截到
			*  表示任意
			
		-->
		<aop:pointcut expression="execution(* com.hadwinling.service.impl.*.*(..))" id="pl"/>
		<!-- 定义切面 -->
		<aop:aspect ref="log" id="asp">
			<!-- 通知 
				before:前置通知
				after:最终通知(无论是否异常  最终执行的通知)
				after-returning  正常执行后置通知
				after-throwing   异常后置通知
				around:环绕通知   使用更灵活  可以自定义增强的执行顺序  
						单独使用
	
				属性:
				method:具体的增强方法
				pointcut-ref:引用切入点
				pointcut:定义切入点
			-->
			<!-- <aop:before method="logger" pointcut-ref="pl" />
			<aop:after method="txManage" pointcut-ref="pl"/>
			<aop:after-returning method="afterReturn" pointcut-ref="pl"/>
			<aop:after-throwing method="printExecption" pointcut-ref="pl"/> -->
			<aop:around method="around" pointcut-ref="pl"/>
		</aop:aspect>
	</aop:config>
</beans>

说明:

aop:config:
作用:用于声明开始 aop 的配置
aop:aspect:
作用: 用于配置切面。
属性:
id:给切面提供一个唯一标识。
ref:引用配置好的通知类 bean 的 id。
aop:pointcut:
作用:
用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。
属性:
expression:用于定义切入点表达式。
id:用于给切入点表达式提供一个唯一标识
Expression说明
execution:匹配方法的执行(常用)
execution(表达式)
表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
写法说明:
public void com.gem.springdemo02.service.impl.StudentServiceImpl.save()

访问修饰符可以省略
返回值可以使用* 号,表示任意返回值
包名可以使用* 号,表示任意包,但是有几级包,需要写几个*
使用 来表示当前包,及其子包
类名可以使用*号,表示任意类
方法名可以使用 * 号,表示任意方法
参数列表可以使用 *,表示参数可以是任意数据类型,但是必须有参数 参数列表可以使用 … 表示有无参数均可,有参数可以是任意类型

通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。
execution(
com.hadwinling.service.impl..(…))
*

aop:xxx 配置对应的通知类型
aop:before
作用:
用于配置前置通知。指定增强的方法在切入点方法之前执行
属性:
method:用于指定通知类中的增强方法名称
ponitcut-ref:用于指定切入点的表达式的引用
poinitcut:用于指定切入点表达式
执行时间点:
切入点方法执行之前执行
aop:after-returning
作用:
用于配置后置通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
执行时间点:
切入点方法正常执行之后。它和异常通知只能有一个执行
aop:after-throwing
作用:
用于配置异常通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
执行时间点:
切入点方法执行产生异常后执行。它和后置通知只能执行一个
aop:after
作用:
用于配置最终通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
执行时间点:
无论切入点方法执行时是否有异常,它都会在其后面执行。

环绕通知
aop:around:

作用:
用于配置环绕通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
说明:
它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
注意:
通常情况下,环绕通知都是独立使用的

事务管理 自己的功能
主线业务 程序员
重复功能 抽取 通知 切面 AOP
后期维护

测试类

package com.hadwinling.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hadwinling.service.StudentService;

public class Client {
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
		StudentService studentService=ac.getBean("studentService", StudentService.class);
		//执行业务
		studentService.save();
		/*studentService.delete(1);
		studentService.update();*/
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值