【Spring4.0】基于XML方式配置SpringAOP

##一、前程提要

《基于注解方式配置SpringAOP》文章已经出炉,需要等同学可以通过超链接进行查看,由于使用注解方式配置SpringAOP在开发中时主流,所以关于SpringAOP的知识都放在那篇文章,这篇文章只会简单地介绍下基于XML方式配置SpringAOP。



##二、代码演示

###1.新建一个名为top.cheungchingyin.spring.aop.xml的包

###2.创建一个名为ArithmeticCalculator的接口

package top.cheungchingyin.spring.aop.xml;


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);
}

###3.创建一个名为ArithmeticCalculatorImpl的类,实现ArithmeticCalculator接口

package top.cheungchingyin.spring.aop.xml;

import org.springframework.stereotype.Component;

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;
	}

}

###4.创建切面类LoggingAspect

package top.cheungchingyin.spring.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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.core.annotation.Order;
import org.springframework.stereotype.Component;

public class LoggingAspect {

	public void declareJoinPointExpression() {
	}

	public void beforeMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		System.out.println("The Method " + methodName + " Begins with " + Arrays.asList(args));
	}

	public void afterMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The Method " + methodName + " ends with");

	}

	public void afterReturningMethod(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The Method " + methodName + " ends with " + result);

	}

	public void afterThrowing(JoinPoint joinPoint, Exception ex) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The Method " + methodName + " occurs with " + ex);

	}

	public Object arroundMethod(ProceedingJoinPoint pjd) {
		Object result = null;
		String methodName = pjd.getSignature().getName();
		// 执行目标方法
		try {
			// 前置通知
			System.out.println("【Around】The method 【" + methodName + "】 begins with" + Arrays.asList(pjd.getArgs()));
			result = pjd.proceed();
			// 后置通知
			System.out.println("【Around】The Method " + " ends with 【" + result + "】");
		} catch (Throwable e) {
			// 异常通知
			System.out.println("The method occurs exception" + e);
		}
		// 后置通知
		System.out.println("【Around】The Method 【" + methodName + "】 ends ");
		return result;
	}
}

###5.创建了另一个切面类VlidationAspect

package top.cheungchingyin.spring.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/*
 * 可以使用@order注解来指定切面的优先级,值越小优先级越高
 */
public class VlidationAspect {
	public void validateArgs(JoinPoint joinpoint){
		System.out.println("validate :"+Arrays.asList(joinpoint.getArgs()));
	}
}

###6.配置xml文件applicationContext-xml.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: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-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
	<!-- 配置bean -->
	<bean id="arithmeticCalculator"
		class="top.cheungchingyin.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
	<!-- 配置切面的bean -->
	<bean id="loggingAspect" class="top.cheungchingyin.spring.aop.xml.LoggingAspect"></bean>
	<bean id="vlidationAspect" class="top.cheungchingyin.spring.aop.xml.VlidationAspect"></bean>
	<!-- 配置AOP -->
	<aop:config>
		<!-- 配置切点表达式 -->
		<aop:pointcut
			expression="execution(* top.cheungchingyin.spring.aop.xml.ArithmeticCalculator.*(int , int ))"
			id="pointcut" />
		<!-- 配置切面及通知 -->
		<aop:aspect ref="loggingAspect" order="2">
			<!-- <aop:before method="beforeMethod" pointcut-ref="pointcut"/> <aop:after 
				method="afterMethod" pointcut-ref="pointcut"/> <aop:after-throwing method="afterThrowing" 
				pointcut-ref="pointcut" throwing="ex"/> <aop:after-returning method="afterReturningMethod" 
				pointcut-ref="pointcut" returning="result"/> -->
				<aop:around method="arroundMethod" pointcut-ref="pointcut"/>
		</aop:aspect>
		<aop:aspect ref="vlidationAspect" order="1">
			<aop:before method="validateArgs" pointcut-ref="pointcut" />
		</aop:aspect>
	</aop:config>
</beans>

结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_33596978/article/details/81389839