Day32 -- configure AOP based on XML

I. review

We talked about using annotations to configure AOP. Today we talk about using XML to configure AOP

The source code of this article has been uploaded to the "resources" of this blog. You can download it for free if you need

2, Knowledge reserve

Use XML, so delete some annotations, such as @ Aspect.

Three. Example

Use XML to configure aop and realize the function of log.

ArithmeticCalculator.java

package com.atguigu.spring.aop.aspectJ_xml;

/**
 * Arithmetic calculator
 * @author user
 *
 */
public interface ArithmeticCalculator {
    /**
     * addition
     * @param i
     * @param j
     * @return
     */
	public int add(int i, int j);
	
	/**
	 * subtraction
	 * @param i
	 * @param j
	 * @return
	 */
	public int sub(int i, int j);
	
	/**
	 * multiplication
	 * @param i
	 * @param j
	 * @return
	 */
	public int mul(int i, int j);
	
	/**
	 * division
	 * @param i
	 * @param j
	 * @return
	 */
	public int div(int i, int j);
	
}

ArithmeticCalculatorImpl.java

package com.atguigu.spring.aop.aspectJ_xml;


public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		// TODO Auto-generated method stub
		int result = i + j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		// TODO Auto-generated method stub
		int result = i - j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		// TODO Auto-generated method stub
		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		// TODO Auto-generated method stub
		int result = i / j;
		return result;
	}

}

LoggingAspect.java

package com.atguigu.spring.aop.aspectJ_xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Cut: log cut
 * @author user
 *
 */
public class LoggingAspect {

	public void beforeMethod(JoinPoint joinPoint) {
		
		//Method name
		String methodName = joinPoint.getSignature().getName();
		
		//Parameter list for method
		Object[] args = joinPoint.getArgs();
		
		System.out.println("LoggingAspect==> The method " + methodName + " begin with : " + Arrays.asList(args));
		
	}
	
	
	public void afterMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("LoggingAspect==> The method " + methodName + " ends.");
	}
	
	
	
	public void afterThrowingMethod(JoinPoint joinPoint, Exception ex) {
		//Get method name
		String methodName = joinPoint.getSignature().getName();
		System.out.println("LoggingAspect==> The method " + methodName + " occurs Exception: " + ex);
	}
	
	
	public void afterReturningMethod(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("LoggingAspect==> The method " + methodName + "ends with: " + result);
	}
	
	
	public Object aroundMethod(ProceedingJoinPoint pjp) {
	
		try {
			//Preposition
			
			
			//Call target method
			Object result = pjp.proceed();
			
			
			//Return
			return result;
			
			
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			//abnormal
			e.printStackTrace();
		}finally {
			//Postposition
		}
	
		return null;
	
	}
	
	
}

ValidationAspect.java

package com.atguigu.spring.aop.aspectJ_xml;

import java.util.Arrays;
import org.aspectj.lang.JoinPoint;

/**
 * Verifying section
 * @author user
 *
 */
public class ValidationAspect {
    
	public void beforeMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		System.out.println("ValidationAspect==> The method " + methodName + " begin with : " + Arrays.asList(args));
		
	}
}

Main.java

package com.atguigu.spring.aop.aspectJ_xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
         public static void main(String[] args) {
        		ApplicationContext ctx = 
        				new ClassPathXmlApplicationContext("spring-aop_xml.xml");
        		
        		ArithmeticCalculator  ac = 
        				ctx.getBean("arithmeticCalculatorImpl",ArithmeticCalculator.class);
        		
        		System.out.println("ac: " + ac.getClass().getName());
        		
        		int result = ac.add(1, 1);
        		
        		System.out.println("Main===>: " +result );
        		
        		System.out.println("============================================================='");
        		
		}
}

Note: = if an error is reported, 99% of the errors are due to the wrong bean configuration in xml, because as long as one bean configuration is not successful, an error will be reported.

Published 198 original articles, won praise 16, visited 10000+
Private letter follow

Tags: Java Spring xml calculator

Posted on Sat, 01 Feb 2020 07:37:35 -0800 by designsubway