Spring learning journey (7) comparison of AOP programming based on XML configuration and AspectJ annotation configuration

Keywords: Java xml Programming Spring

This blog uses a slightly complex case to compare the differences between AOP programming based on XML configuration and AspectJ annotation configuration.

Related to the introduction of packages and other Spring AOP programming preparation, please refer to other blog posts in the small edition.

Case requirements:

Write a simple calculator to realize four operations.

Add AOP function: log function; check whether there is a negative number function in the parameter.

I don't need to talk about much nonsense, just go to the code:

(1) XML based configuration:

An interface class is defined:

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

Implementation class:

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

}

Log slice class:

package com.edu.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class LoggingAspect {
/**
 * Log facet class
 */
    
    public void beforeMethod(JoinPoint joinPoint){
        //Get method name
        String methodName=joinPoint.getSignature().getName();
        //Get method argument value list
        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");
    }
}

Check whether there is a negative slice class in the parameter:

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("Parameters to be verified:"+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("Warning: method"+methodName+"()The first"+(i+1)+"Parameters are negative:"+args[i]);
                }
            }
        }
    }
}

xml configuration file:

<?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">

<!-- To configure bean -->
<bean id="arithmetic" class="com.edu.aop.ArithmeticCalculatorImpl"></bean>

<!-- Configure the tangent class declaration as a bean -->
<bean id="logging" class="com.edu.aop.LoggingAspect"></bean>
<bean id="validation" class="com.edu.aop.ValidationAspect"></bean>

<aop:config>
<!-- Configure log slice class -->
<aop:aspect ref="logging">
<!-- The entry point of configuring pre notice and pre notice-->
<aop:before method="beforeMethod" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:before>
<!-- Configuring post notifications and entry points for post notifications -->
<aop:after method="afterMethod" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:after>
</aop:aspect>

<!-- Configure test parameter slice class -->
<aop:aspect ref="validation">
<!-- The entry point of configuring pre notice and pre notice-->
<aop:before method="validationArgs" pointcut="execution(* com.edu.aop.ArithmeticCalculatorImpl.*(..))"></aop:before>
</aop:aspect>
</aop:config>
</beans>

Main method detection class:

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

}

Operation result:

(2) AOP programming based on AspectJ annotation configuration:

To be continued.

Posted by Kinneh on Wed, 01 Apr 2020 06:21:33 -0700