Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较
本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同。
相关引入包等Spring AOP编程准备,请参考小编的其他博文,这里不再赘述。
案例要求:
写一个简单的实现四则运算的计算器。
加入AOP功能:日志功能;检测参数中是否有负数的功能。
废话不多说了,直接上代码:
(一)基于XML配置:
定义了一个接口类:
package com.edu.aop;
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);
}实现类:
package com.edu.aop;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
return i+j;
}
@Override
public int sub(int i, int j) {
return i-j;
}
@Override
public int mul(int i, int j) {
return i*j;
}
@Override
public int div(int i, int j) {
return i/j;
}
}日志切片类:
package com.edu.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
public class LoggingAspect {
/**
* 日志切面类
*/
public void beforeMethod(JoinPoint joinPoint){
//获取方法名
String methodName=joinPoint.getSignature().getName();
//获取方法实参值列表
Object[] args=joinPoint.getArgs();
System.out.println("The method "+methodName+" begin with "+Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends");
}
}检测参数中是否有负数的切片类:
package com.edu.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
public class ValidationAspect {
public void validationArgs(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs();
System.out.println("待验证参数:"+Arrays.asList(args));
if(args!=null&&args.length>0){
for(int i=0;i<args.length;++i){
if(((Integer)args[i]).intValue()<0){
System.out.println("警告:方法"+methodName+"()第"+(i+1)+"个参数为负数:"+args[i]);
}
}
}
}
}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.xsd">
<!-- 配置bean -->
<bean id="arithmetic" class="com.edu.aop.ArithmeticCalculatorImpl"></bean>
<!-- 分别将切面类声明配置成一个bean -->
<bean id="logging" class="com.edu.aop.LoggingAspect"></bean>
<bean id="validation" class="com.edu.aop.ValidationAspect"></bean>
<aop:config>
<!-- 配置日志切片类 -->
<aop:aspect ref="logging">
<!-- 配置前置通知 及前置通知的切入点-->
<aop:before method="beforeMethod" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:before>
<!-- 配置后置通知及后置通知的切入点 -->
<aop:after method="afterMethod" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:after>
</aop:aspect>
<!-- 配置检测参数切片类 -->
<aop:aspect ref="validation">
<!-- 配置前置通知 及前置通知的切入点-->
<aop:before method="validationArgs" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:before>
</aop:aspect>
</aop:config>
</beans>主方法检测类:
package com.edu.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
ArithmeticCalculator arithmetic=(ArithmeticCalculator)act.getBean("arithmetic");
int result=arithmetic.add(10, 20);
System.out.println("result:"+result);
result=arithmetic.div(-36, 4);
System.out.println("result:"+result);
}
}运行结果:

(二)基于AspectJ注解配置的AOP编程:
未完待续。
相关推荐
justlike 2020-09-02
牧场SZShepherd 2020-07-19
也许不会看见 2020-07-08
也许不会看见 2020-06-28
zmysna 2020-06-25
HappyHeng 2020-06-25
itjavashuai 2020-05-27
mituan 2020-05-26
zhongliwen 2020-05-11
smalllove 2020-05-09
neweastsun 2020-05-05
也许不会看见 2020-05-04
MicroBoy 2020-05-04
CallMeV 2020-05-02
方志朋 2020-05-01
横云断岭 2020-04-26
weigenzongderoot 2020-04-20