Engineering structure preview

Custom annotations

package com.example.springaopdemo.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CalculateExecuteTime {
    // Indicates which module of the service the operation is under
    String module(a) default"XXXX services";

    // Type of operation, add, update, delete
    String type(a) default "add";

    / / the operator
    String user(a) default "system";

    // Operation description
    String operation(a) default "";

}
Copy the code

section

package com.example.springaopdemo.aspect;

import com.example.springaopdemo.annotation.CalculateExecuteTime;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/** ** section */
@Aspect
@Component
public class CalculateExecuteTimeAspect {}Copy the code

Pointcut expressions and notifications

package com.example.springaopdemo.aspect;

import com.example.springaopdemo.annotation.CalculateExecuteTime;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/** ** section */
@Aspect
@Component
public class CalculateExecuteTimeAspect {

    // Pointcut expression
    @Pointcut("@annotation(com.example.springaopdemo.annotation.CalculateExecuteTime)")
    public void pointcut(a){}// Specify the actual code to execute at the connection point
    @Around("@annotation(log)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint, CalculateExecuteTime log) throws Throwable {

        long start = System.currentTimeMillis();
        // Simulate the save operation record function
        List<String> list = new LinkedList<>();
        list.add(log.operation());
        list.add(log.module());
        list.add(log.type());
        list.add(log.user());

        list.stream().forEach(i -> {
            System.out.println("Log:" + i);
        });
// Object proceed = joinPoint.proceed();

        long executionTime = System.currentTimeMillis() - start;
        System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");

        return null; }}Copy the code

Custom annotation injection site

package com.example.springaopdemo.test;

import com.example.springaopdemo.annotation.CalculateExecuteTime;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    @CalculateExecuteTime
    public void share(String url){
        try {
            Thread.sleep(3);
        }catch (Exception e){

        }
    }
}
Copy the code

Test call

package com.example.springaopdemo;

import com.example.springaopdemo.test.TestService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringAopDemoApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(SpringAopDemoApplication.class, args);
        TestService testService = applicationContext.getBean(TestService.class);
        testService.share("www.baidu.com/u/2014828"); }}Copy the code

The running results are as follows: