Two ways to implement Spring AOP: annotation and XML configuration

What is AOP?

AOP (aspect oriented programming) is face-to-face programming. A technology that can add functions to the program dynamically and uniformly without modifying the source code by precompiling and runtime dynamic agent. In my opinion, the function of this way is equivalent to writing a composition in a Chinese exam. After writing a sentence, you find that there is something missing. You need to add a few words to it, and then you pull a sign at the front or back of a word to write the missing words. Adding this before and after corresponds to pre enhancement and post enhancement. Of course, the principle and mechanism of AOP cannot be so simple, but it just looks like this when it runs.

To understand AOP, you need to know several things

1. Aspect: aspect is a modularization of concerns, such as transaction management, log management, permission management, etc;
2. Join point: a specific point in the execution of a program, which is the execution of a method in Spring;
3. Advice: a notification is an operation performed at a connection point in the aspect, that is, transaction management, log management, etc;
4. Pointcut: pointcut is to describe a certain type of selected join point, that is, to specify a certain type of method to weave notifications;
5. Target: the target object that is dynamically proxied by AOP;

Let's start with an example of using annotations

import org.springframework.stereotype.Service;

@Service
public class CalculatorService implements ICalculatorService {
	
	public int mul(int a,int b) {		
		return a*b;		
	}
	
	public int div(int a,int b) {
		return a/b;
	}
}

Let's define a small calculator first, and then give the class to Spring container management through @ Service annotation

At the same time, add XML in Spring

<context:component-scan base-package="com.jd"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

If the method in the target class that creates the target object matches the pointcut expression in the AspectJ aspect, a proxy object is automatically generated for the target object, which uses the JDK dynamic proxy by default,

@Aspect
@Component
public class CalculatorAspect {
	//Pre enhancement.
	@Before("execution(public int com.jd.CalculatorService.*(..))")
	public void b(JoinPoint jp) {
		Object []args = jp.getArgs();
		Signature signature = jp.getSignature();
		String name = signature.getName();
		
		System.out.println("The "+name+" method begin");
		System.out.println("The "+name+" method args:["+args[0]+","+args[1]+"]");
	}
}

@Aspect is to declare the class as an aspect class

@component puts the class into the IOC container

@Before("execution(public int com.jd.CalculatorService.*(..))")
@This is the notice. There are five. befor,after,afterRunturning,afterThorwing,around
The filter condition of "execution(public int com.jd.CalculatorService. * (..)" is pointcut. It will filter out all the qualified classes with the value of int put back in com.jd.CalculatorService. * (..))
Cut to notice + cut to point
Connection point: the method that meets the above filter criteria is the connection point

At this point, we write a test class

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml");
//The IOC container is created when executing this line of code, because of the pointcut table in the CalculatorService class and CalculatorAspect class
//So by default, IOC automatically creates a proxy class for CalculatorService class and corresponding proxy objects,
//If the CalculatorService object is created in lazy loading mode, a proxy object is automatically created for the object when it is acquired.
		ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class);
		System.out.println(calculatorService.div(2, 2));
		applicationContext.close();
		
	}
}

Function... At this time, the output of the pre enhanced class is displayed before the output of the program call.

 

Here's another way to implement AOP using XML

Let's delete the annotations in the CalculatorAspect just now. Here I built another class

public class MethodAspect {
	public void before(JoinPoint jp) {
		Signature signature = jp.getSignature();
		String name = signature.getName();
		System.out.println("The "+name+" method begin");
	}
}

You can see that this method is similar to the befor enhancement before

Then we delete < AOP: AspectJ AutoProxy > < AOP: AspectJ AutoProxy > in XML and add the following code

	<bean id="argAspect" class ="com.jd.aspect.ArgAspect"></bean>
	<bean id ="methodAspect" class = "com.jd.aspect.MethodAspect"></bean>
	
        <!-- To configure AOP-->
	<aop:config>
                <!--Configure tangent expression-->
		<aop:pointcut expression="execution(public int com.jd.CalculatorService.*(..))" id="pointCut"/>
                <!--Configure facets and enhancement types-->
		<aop:aspect ref="methodAspect">
			<aop:before method="before" pointcut-ref="pointCut"/>
		</aop:aspect>
	</aop:config>
	</beans>

At this point, we return to the Test class and execute the Test code.

 

Published 20 original articles, won praise 20, visited 8420
Private letter follow

Tags: xml Spring Programming calculator

Posted on Wed, 05 Feb 2020 01:19:44 -0500 by mikewhy