Spring AOP的开发

AOP介绍

  • AOP(Aspect-Oriented Programming: 面向切面编程): 是一种编程范式,一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充.
  • AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点.
  • 在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.
AOP 的优点:
  1. 降低模块耦合度
  2. 使系统容易扩展
  3. 更好的代码复用性

配置XML文件

所需jar包
  • spring-aop-4.3.3.RELEASE.jar,
  • aspectjweaver-1.8.5.jar
  • aspectjrt-1.8.5.jar

开发步骤

创建XML文件

1、先复制一个表头

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
    ">

2、注册bean

<bean id="bankService" class="stringboot.aop.proxy.BankService"></bean>

3、设置一个切面

<aop:config>
		<aop:pointcut expression="execution(* springboot.aop.*.*(..))" id="loggerPointCut"/>
		<aop:aspect ref="loggerAspect">
		<aop:after-throwing method="loggerAfterThrowing" pointcut-ref="loggerPointCut"  throwing="exception"/>
		</aop:aspect>

	</aop:config>

注意: 在execution中
第一個 表示不限制返回值類型
第二個
表示springboot.aop.xml包下所有的JAVA BEAN
第三個
javabean 所有的方法
(…) 表示對方法的參數沒有限制***

创建通知类

方法模板

//前置通知:在方法前执行
public void beforeMethodAdvice(JoinPoint jp){
	String methodName = jp.getSignature().getName();
	System.out.println("前置通知、方法:" + methodName + "执行完毕!" );
}
//后置通知:在方法执行后执行
public void afterMethodAdvice(JoinPoint jp){
	String methodName = jp.getSignature().getName();
	System.out.println("后置通知、方法:" + methodName + "执行完毕!" );
}
//返回通知:在连接点完成之后执行,及节点返回结果或抛出异常时执行
public void afterReturningMethodAdvice(JoinPoint jp, Object result){
	String methodName = jp.getSignature().getName();
	System.out.println("后置返回结果通知、方法:" + methodName + "执行完毕!,返回:" + result );
}
//异常通知,在方法抛出异常后执行
public void afterThrowingMethodAdvice(JoinPoint jp, Exception e){
	String methodName = jp.getSignature().getName();
	System.out.println("后置异常抛出通知、方法:" + methodName + "执行时发生异常:" + e );
}

基于注解开发AOP
  • 首先配置xml文件
    例如:
<context:component-scan base-package="com.zzxtit.spring.aop.anno"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

代码配置了自动扫描器,并开启了注解支持

  • 通知类Java类编写
    使用@Aspect 和 @Component标记为切面的Spring Bean组件
    例如:
@Aspect
@Component
public class LoggerAspect {

	/**
	 * 通过pointCut可以进行切入点的代码抽离
	 */
	@Pointcut("execution(* com.zzxtit.spring.aop.anno.*.*(..))")
	public void loggerPointCut() {
		
	}
	

	@Before("loggerPointCut()")
	public void beforeAdvice(JoinPoint jp) {
		System.out.println("==================打印日志=====================");

	}
	
	@After("loggerPointCut()")
	public void afterAdvice(JoinPoint jp) {
		System.out.println("-----------打印日志-----后置通知-----------------------------");
	}

注:当不同类中都有注解方法时可以添加@Order(0、1、2、、、)来设置优先级(数字越小优先级越高)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值