AOP development specification and steps of Spring framework based on AspectJ

Relationship between AOP and dynamic agent:

AOP terminology:

  1. Horizontal focus:
    Functions to be added to the business code (called crosscutting concerns in the target object and notifications in the aspect class)
  2. Section class:
    Classes that encapsulate enhanced methods (horizontal concerns)
  3. Notice:
    Each enhanced method in a tangent class is called notification, which acts on a join point through a pointcut expression
  4. Objectives:
    Object to be used by extracted code
  5. Connection point:
    Facets can only affect the specific location of the target object in four ways: before method execution, after method execution, after exception capture, and after finally
  6. Pointcut:
    AOP's tangent class finds the corresponding join point through the pointcut

In the vernacular, we put all the methods that need to be enhanced in one class, which is the tangent class. The tangent class contains the notification, and the notification is the method that needs to be enhanced. aop uses the pointcut expression to let the notification act on the connection point of the specified target method, which may be the connection point before the method and the connection point after the method
A facet should contain @ Aspect, notification, and pointcut expressions to act on join points

How to write pointcut expressions:

//First * represents permission modifier, return value
//The second * represents all classes under the package
//The third * represents all methods of all classes under the package
//… Represents any parameter list
//Cut into com.sms.spring Before all methods of all classes under the. AOP package

@Before(value = "execution(* com.sms.spring.aop.*.*(..)")

/*
	@Before:Pre notification, before method execution 
	@After:Post notification for finally
	@AfterReturning Returns a notification, which acts on the method after execution, in combination with the pointcut expression returning
	Get the variable name of the return value of the method, where the parameter of the method must be the same as the parameter name of the pointcut, and the class of the parameter
	Type is Object. If the type is wrong, the post notification will not be executed
	 			@AfterReturning(value = "execution(*com.sms.spring.aop.*.*(..)",returning="result")
	@AfterThrowing:Exception notification: when there is an exception in a method, it is used with the pointcut expression throwing to get the exception, where the parameter of the method must be the same as the parameter name of the pointcut
				@AfterThrowing(value="execution(*com.sms.spring.aop.*.*(..)",throwing = "e")  

*/

After spring parses this pointcut expression, when performing a pre | post | return notification, it will encapsulate the method information in the JointPoint class:

 public void before(JoinPoint joinpoint) {
		//joinPoint.getArgs(): get parameters of method
		Object[] args = joinpoint.getArgs();
		String methodName = joinpoint.getSignature().getName();
	}

Public entry point writing :@Pointcut

	@Pointcut(value="execution(* com.sms.spring.aop.*.*(..))")
	public void test(){
	}

Use of common entry points:

	@Before(value="test()")

Define the priority of slice action :@Order (number), lower value, higher priority

Development steps:

① Import jar package, or use maven to import directly

Four core jars (core, bean, context, expression) + aopalliance + spring AOP + cglib + spring aspects + aspectjweaver) of spring are required

② Create the spring core xml file and introduce the namespace of aop and context

③ Open component scanning and AspectJ dynamic agent of spring

<?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" 
    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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
	 	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- Turn on component scanning -->
	<context:component-scan base-package="com.sms.spring.aop"></context:component-scan>
	
	<!-- open Asceptj Dynamic agent function of -->
	<aop:aspectj-autoproxy />
</beans>

④ Give the target class to spring management

Add @ Component annotation to the target class

⑤ Give the tangent class containing the enhancement method to Spring for management and identify it as a tangent class

Add @ Component annotation and @ Aspect annotation to the tangent class

⑥ Write the enhanced method in the tangent class, add the pre | post | return | exception notification to the method, write the pointcut expression, and apply the notification (enhanced method) to the connection point

@Before(value = "execution(* com.sms.spring.aop.*.*(..)")
 public void before(JoinPoint joinpoint) {
		//joinPoint.getArgs(): get parameters of method
}

⑦ Using Spring to create proxy objects

The interface object must be obtained instead of the implementation class object, because the proxy object and the implementation class object implement the common interface

ApplicationContext ac= new ClassPathXmlApplicationContext("aop.xml");
MathI mathI = context.getBean("mathImpl", MathI.class);

⑧ Use this proxy object to call methods: methods are enhanced

ApplicationContext ac= new ClassPathXmlApplicationContext("aop.xml");
MathI mathI = context.getBean("mathImpl", MathI.class);
mathI.method();

Tags: Spring xml Maven encoding

Posted on Tue, 23 Jun 2020 01:11:30 -0400 by trp