博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring的AOP前置,后置,运行,异常
阅读量:7015 次
发布时间:2019-06-28

本文共 3354 字,大约阅读时间需要 11 分钟。

hot3.png

1.ArithmeticCalculator.java

package com.huangliusong.spring.aop;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);}

2.ArithmeticCalculatorImpl.java

package com.huangliusong.spring.aop;import org.springframework.stereotype.Component;@Component("arithmeticCalculator")public class ArithmeticCalculatorImpl implements ArithmeticCalculator {	@Override	public int add(int i, int j) {		int result = i + j;		return result;	}	@Override	public int sub(int i, int j) {		int result = i - j;		return result;	}	@Override	public int mul(int i, int j) {		int result = i * j;		return result;	}	@Override	public int div(int i, int j) {		int result = i / j;		return result;	}}

3.LoggingAspect.java

package com.huangliusong.spring.aop;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Aspect@Componentpublic class LoggingAspect {	/**	 * 在 com.huangliusong.spring.aop.	 * ArithmeticCalculator接口的每个实现类的每一个方法开始之前执行一段代码	 */	@Before("execution(public  * com.huangliusong.spring.aop.*.*(..))")	// 加入一个连接点Joinpoint joinPoint可以知道连接细节	public void beforeMethod(JoinPoint joinPoint) {		String methodName = joinPoint.getSignature().getName();		Object[] args = joinPoint.getArgs();		System.out.println("方法开始——————" + methodName + Arrays.asList(args));	}	public void afterMethod(JoinPoint joinPoint) {		String methodName = joinPoint.getSignature().getName();		Object[] args = joinPoint.getArgs();		System.out.println("方法结束——————" + methodName + Arrays.asList(args));	}	/**	 * 在方法正常结束后执行的代码 返回通知是可以访问到方法的返回值	 * 	 * @param joinPoint	 */	@AfterReturning(value = "execution(public  * com.huangliusong.spring.aop.*.*(..))",			returning = "result")	public void afterRunning(JoinPoint joinPoint, Object result) {		String methodName = joinPoint.getSignature().getName();		Object[] args = joinPoint.getArgs();		System.out.println("方法结束——————" + methodName + Arrays.asList(args)				+ result);	}			@AfterThrowing(value = "execution(public  * com.huangliusong.spring.aop.*.*(..))",			throwing="ex")	public void afterThrowing(JoinPoint joinPoint,Exception ex){		String methodName = joinPoint.getSignature().getName();		Object[] args = joinPoint.getArgs();		System.out.println("方法结束——————" + methodName + Arrays.asList(args)				+ ex);			}}

4.TestAop.java

package com.huangliusong.spring.aop;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAop {	@Test	public void test1() {		ApplicationContext ctx = new ClassPathXmlApplicationContext(				"applicationContext.xml");		ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx				.getBean("arithmeticCalculator");		System.err.println(arithmeticCalculator.getClass().getName());		int result = arithmeticCalculator.add(11, 11);		System.out.println("结果" + result);		result = arithmeticCalculator.div(12, 0);		System.out.println("结果" + result);	}}

5.applicationContext.xml

190508_q1hp_3015803.png

转载于:https://my.oschina.net/liusonghuang/blog/818079

你可能感兴趣的文章
拒绝旧国标劣质排插,新国标插线板首选品胜
查看>>
新疆国省干线总里程突破2.9万公里
查看>>
国产智轨电车开进“冰城”接受严寒测试 表现良好
查看>>
缓解“钱紧” 央行本周公开市场净投放创两年新高
查看>>
21岁大专学历,刚培训完前端,不造假简历,能找到工作吗?
查看>>
国内首部区块链行业纪录片开播
查看>>
List集合就这么简单【源码剖析】
查看>>
Spring Cloud (十四):Spring Cloud 开源软件都有哪些?
查看>>
前端状态管理与有限状态机
查看>>
读Zepto源码之Data模块
查看>>
Redux助力美团点评前端进阶之路
查看>>
【闭包概念】关于闭包概念不同解读——你可以自己理解。
查看>>
More-iOS国际化一站式解决方案
查看>>
BCH一周年:从硬分叉到顺风顺水
查看>>
数据结构和算法面试题系列—排序算法之快速排序
查看>>
打破行业壁垒!阿里云OpenSearch开启个性化搜索里程碑
查看>>
面试官,你再问我 Bit Operation 试试?
查看>>
PSV 3.60 固化升级到 3.68 破解完全攻略
查看>>
Android 路由框架
查看>>
vue踩坑记- Cannot find module 'wrappy'
查看>>