1 Spring AOP
1.1 AOP introduction
In the software industry, AOP is the abbreviation of Aspect Oriented Programming, which means: Aspect Oriented Programming, a technology that realizes the unified maintenance of program functions through precompiled mode and dynamic agent during operation. AOP is the continuation of OOP, a hot spot in software development, an important content of Spring framework and a derivative paradigm of functional programming. AOP can isolate all parts of business logic, so as to reduce the coupling between all parts of business logic, improve the reusability of programs, and improve the efficiency of development.
Knowledge expansion: 1 Process oriented programming C language Tan Haoqiang!!!
2. Semi object oriented programming C++
3. Object Oriented Programming Technology java
Programming mode:
1. Interface oriented programming
2. Aspect oriented programming
Summary: AOP (aspect oriented programming) mainly uses dynamic agent mode to reduce program coupling and expand business functions
1.2 introduction to AOP terms
1). Connection point: a method by which users can be extended
2). Entry point: the method of user's actual expansion
3). Notice: specific implementation of extension method
4). Aspect: the process of applying notifications to pointcuts
1.1 why AOP
Question 1: if the business code is written directly, the code is not universal
Problem 2: code redundancy and high coupling of code
AOP: aspect oriented programming
AOP function: without modifying the original method Extend the original method
Formula: AOP = pointcut expression + notification method
2 notification method
- before the execution of the target method
- After throwing is executed when an exception is thrown after the target method is executed
- After returning is executed when the result is returned after the target method is executed
- after the target method is executed (finally)
- The around notification function is the most powerful, which can control the execution of the target method before and after the execution of the target method
Summary of notification methods:
1. Surround notification is the first choice for business processing You can modify the execution track of the program
2. The other four notifications are generally used for program monitoring (monitoring system) record only
3 pointcut expression
- bean(bean Id) matches according to bean!! Spring container managed objects are called coarse-grained beans
- Within (package name. Class name) matches according to the package path, in which wildcards can be used instead
within("com.jt.service. ") At com jt. All classes of the package in the service will match Coarse grain size - Execution (return value type, package name, class name, method name (parameter list)) matches the fine granularity of the method parameter level
execution(* com.jt.service... (...)) explanation: the return value type is arbitrary in com jt. Any parameter of any method of any class in the package path of service
execution(* com.jt.service.userService.add*(int,String)) - @Annotation (package name. Annotation name) matches according to the annotation
Comments: @ Find
@annotation(com.jt.anno.Find)
4 cases
package com.jt.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import java.util.Arrays; /*@Service @Controller @Repository*/ @Component //The component gives the class to the spring container for management @Aspect //It means I'm a cut public class RedisAOP { //Formula aop = pointcut expression + notification method //@Pointcut("bean(itemCatServiceImpl)") //@Pointcut("within(com.jt.service.*)") //@Pointcut("execution(* com.jt.service.*.*(..))") //.* The first level subdirectory of the current package @Pointcut("execution(* com.jt.service..*.*(..))") //.. * all subdirectories of the current package public void pointCut(){ } //How to obtain the relevant parameters of the target object? //ProceedingJoinPoint is only supported for around advice @Before("pointCut()") public void before(JoinPoint joinPoint){ //Connection point Object target = joinPoint.getTarget(); Object[] args = joinPoint.getArgs(); String className = joinPoint.getSignature().getDeclaringTypeName(); String methodName = joinPoint.getSignature().getName(); System.out.println("Target object:"+target); System.out.println("Method parameters:"+Arrays.toString(args)); System.out.println("Class name:"+className); System.out.println("Method name:"+methodName); } }