SpringAop (二)
2020-12-13 06:05
标签:coding roc www result class div 通知 ica text 基于配置的方式 xml 切面 测试 SpringAop (二) 标签:coding roc www result class div 通知 ica text 原文地址:https://www.cnblogs.com/smokerBig/p/11165551.htmlxml 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"
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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
bean id="arithmeticCalculator" class="com.aspectsAopxml.bean.ArithmeticCalculatorImpl">
bean>
bean id="aopxx" class="com.aspectsAopxml.ArithmeticCalculatorAspects">
bean>
bean id="aopAround" class="com.aspectsAopxml.ArithmeticCalculatorAspectsAround">
bean>
aop:config>
aop:pointcut
expression="execution(* com.aspectsAopxml.bean.ArithmeticCalculator.*(..))"
id="pointcut" />
aop:aspect ref="aopxx" 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="e" />
aop:after-returning method="afterReturning"
pointcut-ref="pointcut" returning="result" />
aop:aspect>
aop:aspect ref="aopAround" order="1">
aop:around method="aroundMethods" pointcut-ref="pointcut"/>
aop:aspect>
aop:config>
beans>
package com.aspectsAopxml;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
public class ArithmeticCalculatorAspectsAround {
public Object aroundMethods(ProceedingJoinPoint point) {
Object result = null;
Object args = point.getArgs();
String methodName = point.getSignature().getName();
try {
// 1.前置通知
System.out.println("环绕前置通知信息Method:" + methodName + "==》" + Arrays.asList(args));
// 2.执行目标方法
result = point.proceed();
// 3.返回通知
System.out.println("环绕后置通知信息Method:" + methodName + "==》" + Arrays.asList(args) + "》》结果:" + result);
} catch (Throwable e) {
// 4.异常通知
System.out.println("环绕后置通知信息Method:" + methodName + "==》" + Arrays.asList(args) + "异常信息:" + e);
throw new RuntimeException(e);
}
// 5.方法完成后的返回通知
System.out.println("环绕方法执行完成通知信息Method:" + methodName + "==》" + Arrays.asList(args));
return result;
}
}
package com.aspectsAopxml;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
public class ArithmeticCalculatorAspects {
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("Methods befor......." + methodName + "-->args=>" + Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("Methods after......." + methodName + "-->args=>" + Arrays.asList(args));
}
public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Methods afterReturn......." + methodName + "===" + result);
}
public void afterThrowing(JoinPoint joinPoint, Exception e) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Methods afterTh......." + methodName + "===异常信息:" + e);
}
}
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aspectsAopxml.bean.ArithmeticCalculator;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("application-aspects.xml");
ArithmeticCalculator target = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
int result = target.add(1, 2);
System.out.println("result--->:" + result);
result = target.sub(20, 12);
System.out.println("result--->:" + result);
result = target.mul(8, 8);
System.out.println("result--->:" + result);
result = target.div(1000, 0);
System.out.println("result--->:" + result);
}
}
上一篇:写个js 分页玩玩(原创)
下一篇:poj 3694 Network