Spring AOP 的引入(三 基于xml)

    xiaoxiao2021-12-10  16

    还是一样的需求

    在执行计算的时候,给控制台答应的格式为 begain  method  with [ args1,args2 ]  end method with result

    这次使用Spring AOP

    定义接口:

    package com.dxf.ArithmethicCalculatorSpringAOP; public interface ArithmethicCalculator { int add(int a, int b); int sub(int a, int b); int sul(int a, int b); int div(int a, int b); } 定义实现类:

    package com.dxf.ArithmethicCalculatorSpringAOP; import org.springframework.stereotype.Component; @Component public class ArithmethicCalculatorimp implements ArithmethicCalculator { public int add(int a, int b) { int result = a + b; return result; } public int sub(int a, int b) { int result = a - b; return result; } public int sul(int a, int b) { int result = a * b; return result; } public int div(int a, int b) { int result = a / b; return result; } } 编写切面类 package com.dxf.ArithmethicCalculatorSpringAOPXml; 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 ArithmethicCalculatorAspect { public void methodBefor(JoinPoint joinPoint) { System.out .println("begain " + joinPoint.getSignature().getName() + " with" + Arrays.asList(joinPoint.getArgs())); } public void methodAfter(JoinPoint joinPoint) { System.out.println("end " + joinPoint.getSignature().getName() + " with" + Arrays.asList(joinPoint.getArgs())); } public void methodThrow(JoinPoint joinPoint, Exception e) { System.out.println(joinPoint.getSignature().getName() + "发生异常: " + e.getMessage()); } public void methodAfterReturn(JoinPoint joinPoint, Object result) { // System.out.println(value); System.out.println("methodAfterReturn.....result " + result); } // 环绕通知,此通知就类似于一个代理类,上面的四种通知都在本通知中可以实现 public Object methodAroud(ProceedingJoinPoint joinPoint) { Object[] args = joinPoint.getArgs();// 获取参数列表 String method = joinPoint.getSignature().getName();// 获取方法名 Object result = null; try { // 前置通知 System.out.println("methodAroud before"); result = joinPoint.proceed();// 执行函数 System.out.println("methodAroud after"); // 后置通知 } catch (Throwable e) { // 异常通知 System.out.println("methodAroud Throwable" + e.getMessage()); e.printStackTrace(); } // 返回通知 System.out.println("methodAroud afterReturn " + result); return result; } } 编写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" 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-4.0.xsd"> <bean id="arithmethicCalculator" class="com.dxf.ArithmethicCalculatorSpringAOPXml.ArithmethicCalculatorimp"> </bean> <bean id="arithmethicCalculatorAspect" class="com.dxf.ArithmethicCalculatorSpringAOPXml.ArithmethicCalculatorAspect"> </bean> <!-- 配置Aop --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut expression="execution(* com.dxf.ArithmethicCalculatorSpringAOPXml.ArithmethicCalculatorimp.*(int ,int))" id="pointcut" /> <!-- 配置切面及通知 --> <aop:aspect ref="arithmethicCalculatorAspect"> <!-- 前置通知 --> <aop:before method="methodBefor" pointcut-ref="pointcut" /> <!-- 后置通知 --> <aop:after method="methodAfter" pointcut-ref="pointcut" /> <!-- 返回通知 --> <aop:after-returning method="methodAfterReturn" pointcut-ref="pointcut" returning="result" /> <!-- 异常通知 --> <aop:after-throwing method="methodThrow" pointcut-ref="pointcut" throwing="e" /> <!-- 环绕通知 --> <aop:around method="methodAroud" pointcut-ref="pointcut" /> </aop:aspect> </aop:config> </beans> 测试类: package com.dxf.ArithmethicCalculatorSpringAOPXml; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "application-aop-xml.xml"); ArithmethicCalculator arithmethicCalculator = applicationContext.getBean(ArithmethicCalculator.class); System.out.println(arithmethicCalculator.div(1, 1)); } }
    转载请注明原文地址: https://ju.6miu.com/read-700188.html

    最新回复(0)