Knowledge points of notification

Explanation of notice

Explanation of AOP notification

Category I:

  • @Before("pointcut()")
  • @AfterReturning("pointcut()")
  • @AfterThrowing("pointcut()")
  • @After("pointcut()")

It can record each process executed by the program. It provides records for the log

Category II:

  • @Around("pointcut()")
    You can control whether the target method is executed. Surround notification can control the process of business flow!!!

example:

  1. Verification of permissions
  2. Cache system
  3. Exception handling, etc
    ProceedingJoinPoint can only be used in surround

API s commonly used in notifications

Requirements:

  1. Gets the type of the current target object
  2. Gets the name of the current method
  3. Gets the currently passed parameter
    Common error reports: processingjoinpoint is only supported for around advice
//1. Pre notification: executed before the target method is executed
    @Before("pointcut()")
    public void before(JoinPoint joinPoint){//Connection point: get data in method
        Class targetClass = joinPoint.getTarget().getClass();
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getSignature().getDeclaringTypeName();
        Object[] objs = joinPoint.getArgs();
        System.out.println("I'm an advance notice!!!!");
        System.out.println("type:"+ targetClass);
        System.out.println("Method name:"+methodName);
        System.out.println("Name of the class:"+className);
        System.out.println("Parameters carried in method:"+ Arrays.toString(objs));
    }

Edit SpringAop1

package com.jt.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component//Leave this class to container management
@Aspect //Identify the
@Order(2)
public class SpringAop {
    /**
     * Formula: facet = pointcut expression + notification method
     * Pointcut expression: if the target object satisfies the judgment (if) of the pointcut expression, spring automatically creates a proxy object for it
     * Notification method: an encapsulation method that extends the target method
     * beanID of target object: userServiceImpl
     * Pointcut expression:
     *      1.bean("bean id of (")
     * AOP Rules;
     * */
//    @Pointcut("bean(userServiceImpl)")
//    @Pointcut("within(com.jt.service.*)")
//    @Pointcut("within(com.jt.service..*)")
//    @Pointcut("within(com.*.service..*)")
//    @Pointcut("execution(* com.jt.service.UserServiceImpl.addUser())")
//    @Pointcut("execution(* com.jt.service..*.*(..))")
    @Pointcut("@annotation(com.jt.anno.Wxin)")
    public void pointcut(){
    }

    //1. Pre notification: executed before the target method is executed
    //@Before("pointcut()")
    public void before(JoinPoint joinPoint){//Connection point: get data in method
        //Gets the type of the target object
        Class targetClass = joinPoint.getTarget().getClass();
        //Get method name
        String name1 = joinPoint.getSignature().getName();
        String name2 = joinPoint.getSignature().getDeclaringTypeName();
        Object[] objs = joinPoint.getArgs();
        System.out.println("I'm an advance notice");
        System.out.println("Type:"+targetClass);
        System.out.println("Method name:"+name1);
        System.out.println("Name of class:"+name2);
        System.out.println("Parameters carried in method:"+ Arrays.toString(objs));
    }

//    2. Post notification: executed after the target method is executed
    //Get the return value of the target method through returning = "result" and pass it to result as a parameter
    //@AfterReturning(value = "pointcut()",returning = "result")
    public void afterReturn(Object result){
        System.out.println("I'm a post notification:"+result);
    }

    //3. Exception notification: this notification is executed when the target method reports an error
    //@AfterThrowing(value = "pointcut()",throwing = "exception")
    public void afterThrowing(Exception exception){
        System.out.println("I'm an exception notification");
        System.out.println("I'm an exception message"+exception.getMessage());
        exception.printStackTrace();
    }

    //4. Final notice: the notice to be executed after the target method
    //@After("pointcut()")
    public void after(){
        System.out.println("The final notice shall be executed");
    }
    //5. Focus on mastering the surrounding notice: it shall be implemented before and after the implementation of the target method, and the target method shall be controlled
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Before execution of surround notification");
        //The underlying layer calls the invoke method of the dynamic agent to execute the target method
        Object result = joinPoint.proceed();
        System.out.println("After the notification is executed");
        return result;
    }
}

Edit SpringAop2

package com.jt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect //Identify facet class
@Order(1)//aop is executed first. The smaller the number, the higher the number
public class StringAop2 {

    //Specify a pointcut expression by wrapping notifications
    @Around("@annotation(com.jt.anno.Wxin)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //1. If there is a next notification, execute the notification method. If there is no notification, execute the target method
        System.out.println("Execute surround notification B start");
        Object result = joinPoint.proceed();
        System.out.println("Execute surround notification B end");
        return result;
    }
}

Description of proxy object generation policy

Default policy:

  1. The default rule of dynamic proxy in Spring is JDK proxy. If the agent has no interface, CGLIB will be used automatically
  2. If you need to modify to cglib proxy, add the following code
@EnableAspectJAutoProxy(proxyTargetClass = true)//Enable AOP

The default proxy mode in SpringBoot uses CGLIB proxy. If you need to modify it to JDK proxy, you need to modify the configuration file

  • Why does learning spring framework make programming loosely coupled
  • After interface oriented programming, attributes in objects are generally written to interfaces. The embodiment of polymorphism in java. Attribute types are more flexible and loosely coupled
  • What is the full name of IOC? Inversion of
    Control, or "inversion of control", is a design idea. The right to create objects is completed by the Spring framework. The life cycle of objects is managed by containers
  • Spring container startup mode 1. xml mode 2. Annotation mode
  • When to use factory mode: 1. When objects cannot be instantiated directly. 2. When spring framework integrates other third-party frameworks. FactoryBean
  • Single case multiple case problem: the default condition is single case mode @ Scope("prototype")
  • Lazy loading rules: 1. Lazy loading is invalid by default. Annotation @ lazy is valid. It is only valid for singleton mode. spring lifecycle management includes four processes:
    1. Object creation 2. Object initialization @ PostConstruct 3. Business call 4. Object destruction @ PreDestroy
  • Annotation @ Autowired for dependency injection in Spring 1. Inject by type by default 2. Inject @ Qualifier("cat") by name
    @Annotations in Resource java
  • 10.MVC design idea View layer Model business layer Control layer dynamically assigns values to attributes according to MVC design idea: Hierarchical Code Structure Controller/Service/Mapper|Dao @Value spring
    Basic types and strings and collections (rarely used) dynamic proxies JDK dynamic proxies / CGLib dynamic proxies

AOP aspect - oriented programming extends the method without changing the source code
AOP common notes

  1. @Aspect identification facet
  2. @Pointcut identifies the pointcut. There are 4 ways to write the pointcut expression and 2 common expressions
  3. Five notice notes
  4. @EnableAspectJAutoProxy(proxyTargetClass = true) / / enable AOP
  5. Sort annotation @ Order

Keywords: Java Back-end

Added by rodneykm on Tue, 07 Dec 2021 04:41:41 +0200