Spring的学习之路(九)的通知类型

1 前置通知:在目标方法执行之前进行的操作

前置通知:可以获得切入点的信息。

这里使用上一篇文章的代码

修改MyAspect类

给checkPri方法传参

package com.spring.heshihua.demo2;

import org.aspectj.lang.JoinPoint;

/*
 * 切面类
 * */
public class MyAspectXml {
	public void checkPri(JoinPoint joinPoint) {
		System.out.println("权限校验==========="+joinPoint);
	}
}

运行测试类结果

如图所示显示了切入点的信息

 

2 后置通知:在目标方法执行之后进行的操作

后置通知:获得方法的返回值

修改MyAspect类

增加writeLog方法

package com.spring.heshihua.demo2;

import org.aspectj.lang.JoinPoint;

/*
 * 切面类
 * */
public class MyAspectXml {
	/*
	 * 前置通知
	 * */
	public void checkPri(JoinPoint joinPoint) {
		System.out.println("权限校验==========="+joinPoint);
	}
	/*
	 * 后置通知
	 * */
	public void writeLog() {
		System.out.println("日志记录==============");
	}
}


配置applicationContext.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 definitions here -->
	<!-- 配置目标对象: 被增强的对象 -->
	<bean id = "productDao" class = "com.spring.heshihua.demo2.ProductDaoImpl"></bean>
	<!-- 将切面类交给Spring管理 -->
	<bean id = "myAspect" class = "com.spring.heshihua.demo2.MyAspectXml"></bean>
	<!-- 通过aop的配置完成对目标类产生代理 -->
	<aop:config>
		<!-- 表达式的哪些类的那些方法需要进行增强 -->
		<aop:pointcut expression="execution(* com.spring.heshihua.demo2.ProductDaoImpl.save(..))" id="pointcut1"/>
		<aop:pointcut expression="execution(* com.spring.heshihua.demo2.ProductDaoImpl.delete(..))" id="pointcut2"/>
		<!-- 配置切面 -->
		<aop:aspect ref = "myAspect">
			<!--前置通知  -->
			<aop:before method="checkPri" pointcut-ref="pointcut1"/>
			<!-- 后置通知 -->
			<aop:after-returning method="writeLog" pointcut-ref="pointcut2"/>
		</aop:aspect>
	</aop:config>
</beans>

测试类运行结果

如何获取参数呢

修改delete方法,让他有返回值

修改配置文件,添加一个下图中箭头所指向的returning = "result"

修改MyAspect中得到writeLog方法

运行的测试类结果

3 环绕通知:在目标方法执行之前和之后进行的操作

修改MyAspect类新增around方法

	public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("还绕前通知===========");
		Object obj = joinPoint.proceed();
		System.out.println("还绕后通知===========");
		return obj;
	}

修改配置文件添加新的切入点

测试类运行结果

 

4 异常抛出通知:程序出现异常的时候进行的操作

抛出异常通知,同时获取异常的信息

修改MyAspect类添加afterThrowing

	/*
	 * 异常抛出通知
	 * */
	public void afterThrowing(Throwable ex) {
		System.out.println("异常抛出通知============"+ex.getMessage());
	}

 

修改配置文件

测试类运行结果

如图所示,抛出异常通知的同时打断了程序 

5 最终通知:无论代码是否有异常,总是会执行

修改MyAspect类添加after方法

	/*
	 * 最终通知
	 * */
	public void after() {
		System.out.println("最终通知=======================");
	}

修该配置文件

测试类运行结果运行之前把ProductDaoImpl类中的i = 1/0注释掉了

如果不注释掉i = 1/0,结果如何

无论是否有异常都会运行最终通知

6 引介通知(无需掌握)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值