基于XML配置文件的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;

public class AllLogAdvice {
	// 此方法作为前置通知
	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);
	}
	
	// 此方法作为返回通知
	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);
	}
	
	// 此方法作为异常通知
	public void myThrowingAdvice(JoinPoint joinPoint) {
		//获取被调用的类名
		String targetClassName=joinPoint.getTarget().getClass().getName();
		//获取被调用的方法名
		String targetMethodName=joinPoint.getSignature().getName();
		//日志格式的字符串
		String logInfoText="异常通知:执行"+targetClassName+"类中的"+targetMethodName+"方法时发生异常!";
		//将日志信息输出到控制台
		System.out.println(logInfoText);
	}
	
	// 此方法作为环绕通知
	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"
	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">
	<!-- 实例化业务类的Bean -->
	<bean id="productService" class="com.ssm.service.impl.ProductServiceImpl"></bean>
	
	<!-- 实例化日志通知/增强处理(切面)的Bean -->
	<bean id="allLogAdvice" class="com.ssm.aop.AllLogAdvice" />
	
	<!-- 配置aop -->
	<aop:config>
		<!-- 配置日志切面 -->
		<aop:aspect id="logaop" ref="allLogAdvice">
			<!-- 定制切入点,可采用正则表达式,含义是对browse方法,进行拦截 -->
			<aop:pointcut expression="execution(public void browse(String,String))" id="logpointcut"/>
				<!-- expression="execution(* com.ssm.service.*.*(..))" -->
			<!-- 将日志通知类中的myBeforeAdvice方法指定为前置通知 -->
	<!-- 		<aop:before method="myBeforeAdvice" pointcut-ref="logpointcut"/> -->
			<!-- 将日志通知类中的myAfterReturnAdvice方法指定为返回通知 -->
	<!-- 		<aop:after-returning method="myAfterReturnAdvice" pointcut-ref="logpointcut"/> -->
			<!-- 将日志通知类中的myThrowingAdvice方法指定为异常通知 -->
	<!-- 		<aop:after-throwing method="myThrowingAdvice" pointcut-ref="logpointcut" throwing="e"/> -->
			<!-- 将日志通知类中的myAroundAdvice方法指定为环绕通知 -->
			<aop:around method="myAroundAdvice" pointcut-ref="logpointcut"/>
		</aop:aspect>		
	</aop:config>
	
	
</beans>

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

猜你喜欢

转载自blog.csdn.net/qq_37769323/article/details/104297530