Spring的AOP前置,后置,运行,异常

本文通过实战案例介绍如何使用 Spring AOP 进行方法级别的增强处理。案例中定义了一个简单的算术运算接口及其实现类,并利用 LoggingAspect 类实现了对算术操作的日志记录功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
@Component
public 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

<?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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<!-- 配置自动扫描的包  -->
	<context:component-scan base-package="com.huangliusong.spring.aop"></context:component-scan>
	<!--  -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

190508_q1hp_3015803.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值