基于@AspectJ的AOP实现

package com.ssm.aop;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.stereotype.Component;
@Aspect
@Component
public class AllLogAdvice {
	// 定义切入点表达式
	@Pointcut("execution(* com.ssm.service.*.*(..))")
	public void allMethod() {
		//使用一个返回值为void,方法体为空的方法来命名切入点
	}
	
	// 此方法作为前置通知
	@Before("allMethod()")
	public void myBeforeAdvice(JoinPoint joinPoint) {
		// 获取业务方法参数
		List<Object> args=Arrays.asList(joinPoint.getArgs());
		//日志格式的字符串
		String logInfoText="前置通知:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
			.format(new Date())+" "+args.get(0).toString()+" 浏览商品 "+args.get(1).toString();
		//将日志信息输出到控制台
		System.out.println(logInfoText);
	}
	
	// 此方法作为返回通知
	@AfterReturning("allMethod()")
	public void myAfterReturnAdvice(JoinPoint joinPoint) {
		// 获取业务方法参数
		List<Object> args=Arrays.asList(joinPoint.getArgs());
		//日志格式的字符串
		String logInfoText="返回通知:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
			.format(new Date())+" "+args.get(0).toString()+" 浏览商品 "+args.get(1).toString();
		//将日志信息输出到控制台
		System.out.println(logInfoText);
	}
	
	// 此方法作为异常通知
	@AfterThrowing(pointcut="allMethod()",throwing="e")
	public void myThrowingAdvice(JoinPoint joinPoint) {
		//获取被调用的类名
		String targetClassName=joinPoint.getTarget().getClass().getName();
		//获取被调用的方法名
		String targetMethodName=joinPoint.getSignature().getName();
		//日志格式的字符串
		String logInfoText="异常通知:执行"+targetClassName+"类中的"+targetMethodName+"方法时发生异常!";
		//将日志信息输出到控制台
		System.out.println(logInfoText);
	}
	
	// 此方法作为环绕通知
	@Around("allMethod()")
	public void myAroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
		long beginTime=System.currentTimeMillis();
		joinPoint.proceed();
		long endTime=System.currentTimeMillis();
		//获取被调用的方法名
		String targetMethodName=joinPoint.getSignature().getName();
		//日志格式的字符串
		String logInfoText="环绕通知:"+targetMethodName+"方法调用前时间"+beginTime+"毫秒,"
				+"调用后的时间"+endTime+"毫秒。共执行了"+(endTime-beginTime)+"毫秒。";
		//将日志信息输出到控制台
		System.out.println(logInfoText);
	}
	
		
}
<?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:p="http://www.springframework.org/schema/p"
	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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.ssm" />
	<!-- 开启基于@AspectJ切面的注解处理器 -->
	<aop:aspectj-autoproxy />
	
</beans>

发布了233 篇原创文章 · 获赞 1 · 访问量 9156

猜你喜欢

转载自blog.csdn.net/qq_37769323/article/details/104297752
今日推荐