五种通知"/>
基于xml实现AOP的五种通知
1.创建目标资源
①接口
public interface Calculator {int add(int i,int j);int sub(int i,int j);int mul(int i,int j);int div(int i,int j);
}
②实现类
@Component
public class CalculatorImpl implements Calculator {@Overridepublic int add(int i, int j) {int result = i + j;System.out.println("方法内部result = " + result);return result;}@Overridepublic int sub(int i, int j) {int result = i - j;System.out.println("方法内部result = " + result);return result;}@Overridepublic int mul(int i, int j) {int result = i * j;System.out.println("方法内部result = " + result);return result;}@Overridepublic int div(int i, int j) {int result = i / j;System.out.println("方法内部result = " + result);return result;}
}
2.创建切面类
//切面类
@Component//ioc容器进行管理
public class LogAspect {// 前置通知public void beforeMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();//获取增强方法的名称Object[] args = joinPoint.getArgs();//获取参数的名称System.out.println("Logger-->前置通知,方法名称:" + methodName + ",参数:" + Arrays.toString(args));}// 后置通知@After(value = "pointCut()")public void afterMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();//获取增强方法的名称System.out.println("Logger-->后置通知,方法名称:" + methodName);}// 返回通知public void afterReturningMethod(JoinPoint joinPoint,Object result){String methodName = joinPoint.getSignature().getName();//获取增强方法的名称System.out.println("Logger-->返回通知,方法名称:" + methodName + ",返回结果:" + result);}// 异常通知//目标方法出现异常,这个通知执行public void afterThrowingMethod(JoinPoint joinPoint,Throwable ex){String methodName = joinPoint.getSignature().getName();//获取增强方法的名称System.out.println("Logger-->异常通知,方法名称:" + methodName + ",异常信息:" + ex);}// 环绕通知public Object aroundMethod(ProceedingJoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();//获取增强方法的名称Object[] args = joinPoint.getArgs();String argString = Arrays.toString(args);Object result = null;try{System.out.println("环绕通知==目标方法之前执行");//调用目标方法result = joinPoint.proceed();System.out.println("环绕通知==目标方法返回值之后执行");}catch (Throwable throwable){throwable.printStackTrace();System.out.println("环绕通知==目标方法出现异常执行");}finally {System.out.println("环绕通知==目标方法执行完毕执行");}return result;}
}
3.配置bean文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""xmlns:context=""xmlns:aop=""xsi:schemaLocation="://www.springframework/schema/beans/spring-beans.xsd://www.springframework/schema/context/spring-context.xsd://www.springframework/schema/aop/spring-aop.xsd"><!-- 开启组件扫描--><context:component-scan base-package="com.yogurt.spring6.aop.xmlaop"></context:component-scan><!-- 配置aop五种通知--><aop:config>
<!-- 配置切面类--><aop:aspect ref="logAspect">
<!-- 配置切入点--><aop:pointcut id="pointcut" expression="execution(* com.yogurt.spring6.aop.xmlaop.CalculatorImpl.*(..))"/>
<!-- 配置五种通知类型-->
<!-- 前置通知--><aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
<!-- 后置通知--><aop:after method="afterMethod" pointcut-ref="pointcut"></aop:after>
<!-- 返回通知--><aop:after-returning method="afterReturningMethod" returning="result" pointcut-ref="pointcut"></aop:after-returning>
<!-- 异常通知--><aop:after-throwing method="afterThrowingMethod" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
<!-- 环绕通知--><aop:around method="aroundMethod" pointcut-ref="pointcut"></aop:around></aop:aspect></aop:config>
</beans>
4.测试
public class TestAop {@Testpublic void testAdd(){ApplicationContext context = new ClassPathXmlApplicationContext("beanaop.xml");Calculator calculator = context.getBean(Calculator.class);calculator.add(2,3);}
}
更多推荐
基于xml实现AOP的五种通知
发布评论