spring AOP -- implemented by annotations

spring AOP - Implemented by Annotation

Catalog

Explain

This example is not a web project.

Configure applicationContext.xml

Configuration file, code slice.

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- Turn on automatic scanning text3_AOP_text4 Annotations for all classes under the package -->
	<context:component-scan base-package="text3_AOP_text4" />
	<!-- open aspectj Notes -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

Explain:
With annotations, configuration operations can be greatly reduced.

Face class MyAspect.java

Aspect class: MyAspect.java, code slice.

package text3_AOP_text4;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//Registering a bean is a facet
@Component
@Aspect
public class MyAspect {
	// Connection Point, Method
	@Pointcut("execution(* text3_AOP_text3.StudentImpl.*(..))")
	public void pointcut1() {
	}

	// Connection points, classes
	@Pointcut(argNames = "pointcut2", value = "within(text3_AOP_text3.StudentImpl.*)")
	public void pointcut2() {
	}

	// Pre-notification method
	@Before("pointcut()")
	public void before(JoinPoint joinPoint) {
		System.out.println("Before advice---Method name: " + joinPoint.getSignature().getName());
	}

	// Pre-notification method with parameters and parameter names are (arg1,arg2,arg3,...)
	@Before("pointcut() && args(arg1,arg2,arg3)")
	public void beforeWithParam(JoinPoint joinPoint,String arg1, String arg2, String arg3) {
		System.out.println(arg1);
	}

	// Pre-notification method with parameters, and additional parameters can be obtained, @Method type is a comment, custom parameter name Method
	@Before("pointcut() && @annotation(Method)")
	public void beforeWithAnnotaion(Method Method) {
		System.out.println("BeforeWithAnnotation." + Method.value());
	}

	// Notify the method after returning, return the value name re,
	@AfterReturning(pointcut = "pointcut2", returning = "re")
	public void afterReturning(JoinPoint joinPoint, Object re) {
		System.out.println("after returning advise---Method name: " + joinPoint.getSignature().getName() + ", Return parameters re: " + re);
	}

	// Exception notification method, exceptional variable e
	@AfterThrowing(pointcut = "pointcut()", throwing = "e")
	public void afterThrowing(JoinPoint joinPoint, Throwable e) {
		System.out.println("Exception Notification: " + "Method name with exception: " + joinPoint.getSignature().getName() + ", Abnormal information e: " + e.getMessage());
		System.exit(0);
	}

	// Post-notification method
	@After("pointcut()")
	public void after(JoinPoint joinPoint) {
		System.out.println("Post-notification---Method name: " + joinPoint.getSignature().getName());
	}

	// Circumferential Notification Method
	@Around("pointcut()")
	public Object around(ProceedingJoinPoint joinPoint) {
		Object re = null;
		try {
			System.out.println("Before circular notification");
			re = joinPoint.proceed();
			System.out.println("After circular notification");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return re;
	}

	// Around notification method with parameters, execution context ProceedingJoinPoint
	@Around("pointcut() && args(name)")
	public Object aroundInit(ProceedingJoinPoint joinPoint, String name) {
		System.out.println("Full name:" + name);
		Object re = null;
		try {
			System.out.println("Before circular notification");
			re = joinPoint.proceed();
			System.out.println("After circular notification");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return re;
	}
	
	//When using People objects, you can automatically force People objects to FBIImpl objects.
	@DeclareParents(value = "text3_AOP_text3.People", defaultImpl = text3_AOP_text4.FBIImpl.class)
    public FBI fbi;
}

Explain:

--------------------------------------------------------------------------------------------------------------------------------------------
	 * Learn about the main methods of spring aop's two interfaces:  
	 * 1)JoinPoint 
	 * java.lang.Object[] getArgs(): Gets the set of parameters for the join point method 
	 * Signature getSignature(): The method object to get the join point;
	 * java.lang.Object getTarget(): Gets the target object of the class where the join point is located; 
	 * java.lang.Object getThis(): Get the tangent object itself;  
	 * 2)ProceedingJoinPoint 
	 * ProceedingJoinPoint inherits the JoinPoint subinterface and adds two new methods for executing the join point method:
	 * java.lang.Object proceed() throws java.lang.Throwable: the method of executing the connection point of the target object by reflection;
	 * java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable: Performs the method at the target object join point by reflection, but replaces the original input with a new input. 
	 * 

spring does not automatically recognize Aspect annotations and needs to be annotated with the @Component annotation

Custom Annotation @Method

@ Method annotations, code slices.

package text3_AOP_text4;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//Methodologically, run time
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Method {	
	String value();
}

Explain:
Annotations are used in methods and run time is valid

summary

	1. Using annotations to implement AOP is more convenient than API and configuration, and can greatly simplify the development process.
** Referencing packages is the biggest difficulty (step by step)**

Keywords: Java Spring xml encoding

Added by AnAmericanGunner on Wed, 31 Jul 2019 07:38:04 +0300