springAOP的各种通知

切面及切面的目标方法


package com.mo.transaction;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class Transaction {
	
	/**
	 * 前置通知
	 * 	在目标方法执行之前执行
	 * 		参数:连接点,可以获取连接点相关的信息
	 */
	public void beginTransaction(JoinPoint joinPoint){
		String name = joinPoint.getSignature().getName();
		System.out.println("连接点的名称" + name);//客户端调用那个方法,那个方法就是连接点
		System.out.println("目标类" + joinPoint.getTarget().getClass());//通过连接点可以获取目标类
		System.out.println("beginTransaction");
	}
	
	/**
	 * 后置通知
	 * 	在目标方法执行之后执行
	 * 		可以获取目标方法的返回值:通过在后置通知中配置的returning="val",在通知这里需要配置参数Object val,用来收集参数
	 * 注意:当调用连接点的方法的时候,抛出了异常,那么后置通知将不再执行
	 */
	public void commit(JoinPoint joinPoint,Object val){//这里的参数,val要与spring配置文件中的后置通知中的returning="val" 一致
		System.out.println(val);
		System.out.println("commit");
	}
	
	/**
	 * 最终通知
	 */
	public void finallyMethod(){
		System.out.println("finallyMethod");
	}
	
	/**
	 * 异常通知
	 */
	public void throwingMehtod(JoinPoint joinPoint,Throwable ex){
		System.out.println(ex.getMessage());
		System.out.println("throwingMehtod");
	}
	
	/**
	 * 这是一个环绕通知
	 * 		joinPoint.proceed(); 这行代码代表执行目标方法,不写就是不执行目标方法
	 */
	public void aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("qqq");
		joinPoint.proceed();//这里是调用代码
	}
}



applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        
	<bean id="personDao" class="com.mo.transaction.PersonDaoImpl"></bean>   
	<bean id="transaction" class="com.mo.transaction.Transaction"></bean>
	
	<aop:config>
		<!-- 切入点表达式,确定目标类 -->
		<aop:pointcut 
			expression="execution(* com.mo.transaction.PersonDaoImpl.*(..))" 
			id="perform"/>
			
		<!-- ref指向的对象就是切面 -->
		<aop:aspect ref="transaction">
		
			<!-- 
				前置通知,指向切入点表达式
					1.在目标方法执行之前
					2.获取不到目标方法的返回值
			 -->
			<aop:before method="beginTransaction" pointcut-ref="perform"/>
			
			<!--
				 后置通知
					1.后置通知可以获取到目标方法的返回值
						注意:这里设定的val是接受目标方法的返回值的,在后置通知中接受返回值 参数的名称也是val,要与这里定义的一致
					2.当目标方法抛出异常,后置通知将不再执行
					
			 -->
			 <aop:after-returning method="commit" pointcut-ref="perform" returning="val"/> 
			 
			 <!-- 
			 	最终通知
			 		无论目标方法是否抛出异常都将执行
			  -->
			 <aop:after method="finallyMethod" pointcut-ref="perform"/>
			 
			 <!--
				  异常通知
				  获取目标方法抛出异常的信息
			 -->
			 <aop:after-throwing method="throwingMehtod" pointcut-ref="perform" throwing="ex"/>
			 
			 <!--
				  环绕通知
				  	能够控制目标方法的执行
				  	前置通知和后置通知能在目标方法的前面和后面加一些代码,但是不能控制目标方法的执行
				  	
				  注意:环绕通知中参数中的连接点是用ProceedingJoinPoint,是JoinPoint的一个子类
			   -->
			 <aop:around method="aroundMethod" pointcut-ref="perform"/>
		</aop:aspect>
	</aop:config>
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值