spring----aop----五类通知(通过xml来配置)----笔记

依着上一篇spring----aop----五类通知----笔记
去掉注释全部采用xml来配置
无非就是在xml里把用到的bean用bean标签配置好
然后用aop:config标签配置好aop的切点和切面以及方法

配置文件

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

	<!-- 注册bean 和 切面bean -->
	<bean id="cola" class="top.demo.xmlaop.Cola"></bean>
	<bean id="aopim" class="top.demo.xmlaop.AopIm"></bean>
	
	<!--aop:config标签用来配置有关切面的配置  -->
	<aop:config>
		<!-- 设置切点表达式 以便下面引用 -->
		<aop:pointcut expression="execution(* top.demo.xmlaop.Cola.Open(int))" id="cut"/>
		<!-- 配置切面所用的bean 和优先级 -->
		<aop:aspect ref="aopim" order="1" >
			<!-- 配置切面方法 -->
			<aop:before method="beforeCheck" pointcut-ref="cut"/>
			<aop:after method="afterCheck"  pointcut-ref="cut"/>
			<aop:after-returning method="afterReturn" pointcut-ref="cut" returning="res"/>
			<aop:after-throwing method="afterThrow" pointcut-ref="cut" throwing="ex"/>
			<aop:around method="around" pointcut-ref="cut"/>	
		</aop:aspect>
	
	</aop:config>


</beans>

主要的类 去掉了注解

package top.demo.xmlaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


public class AopIm {

	
	/*
	 * 前置通知
	 * 注意 可以使用通配符 public void替换成* 表示任何的权限修饰符和返回值 等等等等 。。。。。
	 * 参数替换成..可以表示不限参数的匹配
	 * */
	public void beforeCheck(JoinPoint joinPoint) {
		Signature sig=joinPoint.getSignature();
		
		System.out.println("before at "+sig.getName()+"and arg[0] is "+joinPoint.getArgs()[0]);
		
	}
	
	/*
	 *后置通知
	 *无法获取返回值 。可以通过返回通知获取返回值
	 *且后置通知无论方法是不是异常都会执行
	 * */
	public void afterCheck(JoinPoint joinPoint) {
		Signature sig=joinPoint.getSignature();
		
		System.out.println("After at "+sig.getName()+"and arg[0] is "+joinPoint.getArgs()[0]);
		
	}
	
	/*
	 * 返回通知
	 * */
	public void afterReturn(JoinPoint joinPoint,Object res) {
		Signature sig=joinPoint.getSignature();
		System.out.println("After at "+sig.getName()+"return. res= "+res);
		
	}
	

	/*
	 * 异常通知
	 * 注意如果这个方法的参数:假设异常类型为数学除零的异常 
	 * afterThrow(JoinPoint joinPoint,空指针异常类 ex) 然后我这里写了空指针异常
	 * 那afterThrow这个方法就执行不了 因为类型不对
	 * */
	public void afterThrow(JoinPoint joinPoint,Exception ex) {
		
		Signature sig=joinPoint.getSignature();
		System.out.println("After at "+sig.getName()+"Throw. message= ");
		System.out.println(ex.getMessage());
	}
	
	/*
	 * 环绕通知
	 * 环绕通知就等于整个代理过程交给你自己处理  连被代理对象的要执行的目标方法要不要执行也取决你
	 * 上面几个通知比较像 处理目标方法调用的某个时刻的 处理过程
	 * */
	public Object around(ProceedingJoinPoint pJoinPoint) {
		
		Object res=null;
		String methodName=pJoinPoint.getSignature().getName();
		
		System.out.println(methodName+" 执行前(前置通知)");
		try {
			
			res=pJoinPoint.proceed();
			System.out.println(methodName+" 执行后有结果(返回通知)");
		} catch (Throwable e) {

			System.out.println("异常通知 "+e.getMessage());
		}
		System.out.println(methodName+" 执行后(后置通知)");
		return res;
	}

}

输出的结果和上一篇用注解配置的是一样的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值