Spring AOP(二)——基于xml方式

3 篇文章 0 订阅

Spring AOP(一)——基于注解的方式

步骤

此处导包和在配置文件中加入命名空间同上篇一样,不在赘述

1.编写目标类

public interface ArithmeticCalculator {
	int add(int i, int j);
	int sub(int i, int j);
	
	int mul(int i, int j);
	int div(int i,int j);	
}
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
	
	public int add(int i, int j) {
		int result = i+ j;
		return result;
	}
	
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}

	public int mul(int i, int j) {
		int result = i * j;
		return result;
	}

	public int div(int i, int j) {
		int result = i / j;
		return result;
	}
}

2.配置目标类

	<bean id="arithmeticCalculator" class="com.atguigu.spring.aop.xml.ArithmeticCalculatorImpl"></bean>

3.编写切入类

public class LoggingAspect {
	
	public void beforeMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		System.out.println("The method "+ methodName +" begins with" + args);
	}
	
	public void afterMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method "+ methodName +" end ");
	}
	
	public void afterReturning(JoinPoint joinPoint,Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method " + methodName+ " end result:" + result);
	}
	
	public void afterThrowing(JoinPoint joinPoint,Exception ex) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method " + methodName +" occurs exception:" + ex );
	}
	
	public Object aroundMethod(ProceedingJoinPoint pjd) {
		
		Object result = null;
		String methodName = pjd.getSignature().getName();
		//执行方法
		try {
			//前置通知
			System.out.println("The method " + methodName+ " begins with" + Arrays.asList(pjd.getArgs()));
			result = pjd.proceed();
			//返回通知
			System.out.println("The method " + methodName + " end with:" + result);
		} catch (Throwable e) {
			System.out.println("The method " + methodName + " occurs exception:" + e);
			
		}
		System.out.println("The method " + methodName + " ends ");
		return 100;
		
	}

}
public class VlidationAspect {

	public void validataArgs(JoinPoint joinPoint) {
		System.out.println("--->validate:" +Arrays.asList(joinPoint.getArgs()));
	}
	
}

4.配置切面类

<bean id="loggingAspect" class="com.atguigu.spring.aop.xml.LoggingAspect"></bean>
	
<bean id= "vlidationAspect" class="com.atguigu.spring.aop.xml.VlidationAspect"></bean>

5.进行AOP配置

<aop:config>
		<!-- 配置切点表达式 -->
		<aop:pointcut expression="execution(* com.atgui.spring.aop.xml.*.*(..))" id="pointcut"/>
		<!-- 配置切面及通知 -->
		<aop:aspect ref="loggingAspect" order="2">
			<aop:before method="beforeMethod" pointcut-ref="poincut"/>
			<aop:after method="afterMethod" pointcut-ref="pointcut"/>
			<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>
			<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
			<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
		</aop:aspect>
		
		<aop:aspect ref="vlidationAspect" order="1">
			<aop:before method="validataArgs" pointcut-ref="pointcut"/>		
		</aop:aspect>
	</aop:config>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值