Spring Boot Aspect Aop thought

Keywords: SpringBoot Java Spring jvm

Spring Boot Aop

Scope of Aspect

In order to reduce the coupling, each class should improve a fixed function as far as possible, but many classes need to be called in the program to achieve the function in order. At this time, we need to use the aspect to reference other classes into a class, that is to say, where I need to use them.

Comment writing of interception rules

package com.springboot.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
//Declare a method, @ target describes the object scope decorated by Annotation: Annotation can be used for packages, types (class, interface, enumeration, Annotation type), type members (method, constructor, member variable, phyll value), method parameters and local variables (such as loop variable, catch parameter). Target is used in the declaration of Annotation type to make its decorated target clearer.
@Retention(RetentionPolicy.RUNTIME)
//1. RetentionPolicy.SOURCE: the annotation is only kept in the source file. When the Java file is compiled into a class file, the annotation is discarded;
//2. RetentionPolicy.CLASS: the annotation is reserved to the class file, but it is abandoned when the jvm loads the class file, which is the default life cycle;
//3. RetentionPolicy.RUNTIME: annotations are not only saved to the class file, but still exist after the jvm loads the class file;
@Documented
//@The Javadoc tool will include the annotation information of the annotated element in the Javadoc. By default, annotation information is not included in Javadoc. An example is as follows:
//Write a comment on the interception rule
public @interface action 
{
	String name();
}

Writing configuration file Aopconfig

package com.springboot.aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
//Declaration is a configuration class
@ComponentScan("com.springboot.aop")
//Scan path
@EnableAspectJAutoProxy
//Start AOP section
public class Aopconfig 
{
	
}

The compilation of add operation of comment interception

package com.springboot.aop;

import org.springframework.stereotype.Service;

@Service
//Write blocked classes using annotations
public class DemoAnnotationService 
{
	@action(name="Comment intercepted add operation")
	public void add() 
	{
		System.out.println("Comment intercepted add");
	}
}

Operation writing of add intercepted by method

package com.springboot.aop;

import org.springframework.stereotype.Service;

@Service
public class DemoMethodService 
{
	public void add() 
	{
		System.out.println("Method intercepted add");
	}
}

The writing of logAspect facet

package com.springboot.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;;
@Aspect
@Component
//Annotated as Spring container managed bean s
public class LogAspect
{
	@Pointcut("@annotation(com.springboot.aop.action)")
	//Declare a pointcut
	public void annotationPointCut() {}
	@After("annotationPointCut()")
	//Declare a suggestion and use pointcut
	public void after(JoinPoint joinpoint) 
	{
		MethodSignature signature=(MethodSignature)joinpoint.getSignature();
		Method method=signature.getMethod();
		action antion1=method.getAnnotation(action.class);
		System.out.println("Comment interception"+antion1.name());
	}
	@Before("execution(* com.springboot.aop.DemoMethodService.*(..))")
	//Declare a suggestion using interception rules as parameters.
	public void before(JoinPoint joinpoint) 
	{
		MethodSignature signature=(MethodSignature)joinpoint.getSignature();
		Method method=signature.getMethod();
		System.out.println("Method regular interception"+method.getName()+"Operation of");
	}
}

Writing of Main

package com.springboot.aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main 
{
	public static void main(String ager[]) 
	{
		AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Aopconfig.class);
		DemoAnnotationService demoannotation=context.getBean(DemoAnnotationService.class);
		
		DemoMethodService demomethodservice=context.getBean(DemoMethodService.class);
		
		demoannotation.add();
		
		demomethodservice.add();
		
		context.close();
	}
}
//Get attributes on annotations by reflection

Operation results


Print out the add method, and also print out the slicing method of the annotation. Pay attention to the location of after and before methods.

Original article 23 praised 0 visitors 893
follow Private letter

Posted by rocky on Wed, 29 Apr 2020 21:17:19 -0700