One, Spring AOP concept and specific use

1. Spring AOP definition:

(Aspect Oriented Programming) Oriented Programming : the software process, before and after the execution can be additional appropriate extensions, we called this feature section. The cut surface can be easily installed and removed. We can visually understand the aspect as a browser plug-in, install it when needed, and uninstall it when not needed.

  • The AOP approach is to abstract and encapsulate general, business-independent functions into aspect classes
  • The ultimate goal : expand the program behavior in the case of stainless steel source code
  • Why are extended functions called aspects?
    Answer: The normal software business process is executed sequentially from top to bottom. The functions we add are added in the original software's running process like a cross-section.
    Insert picture description here

Second, AOP related concepts

One, the relationship between spring aop and AspectJ:

  1. Eclipse AspectJ, an aspect-oriented programming language based on the java platform, has a complete system that can realize the concept of aop aspect-oriented programming at runtime

  2. But spring aop is not completely implemented using AspectJ, it just relies on AspectJWeaver at the bottom to achieve class and method matching

As shown in the screenshot, the process of this scope delineation is to rely on AspectJWeaver to achieve
The process of this scope delineation is achieved by relying on AspectJWeaver
3. The core principle of Spring AOP is to use the proxy mode to implement object runtime function extension

Second, the key concept

annotation Description
Aspect Aspects, specific functions of pluggable components, usually one aspect only implements one general function. The essence is a standard class, no need to inherit and implement other classes. The format must be public, and the return value can be void or object
Target Class/Method The target class/target method refers to the method that really needs to be executed related to the business. The function is similar to the insert and create methods in service and dao
PointCut Entry point: use the execution expression to explain which classes of the system the aspect should act on
JoinPoint The connection point is the object that contains the target class/method metadata during the operation of the aspect. This must be passed in the aspect method. Metadata: Information describing the target class and target method
Advice Notifications, which indicate the timing of execution of specific aspects. Spring includes five different types of notifications.

Three, AOP configuration process:

  1. Depend on AspectJ
 <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <!--spring aop的底层依赖-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>

    </dependencies>
  1. Realize the aspect class, method

// 切面类
public class MethodAspect {
    
    
    // 切面方法,用于扩展额外功能
    public void printExecutionTime(JoinPoint joinpoint) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");
        String now = sdf.format(new Date());
        String className = joinpoint.getTarget().getClass().getName();//获取目标类名
        String methodName = joinpoint.getSignature().getName();// 获取目标方法名
        System.out.println("---->" + now + ":" + className + "." + methodName);
    }
}
  1. Configure Aspect Bean

  2. Define PointCut (which methods of which classes of the system are affected by the current aspect)

  3. Configure Advice Notification

    The full version of the configuration file is as follows:

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userDao" class="com.imooc.spring.aop.dao.UserDao"/>
    <bean id="userService" class="com.imooc.spring.aop.service.UserService">
        <property name="userDao" ref="userDao"/>
    </bean>
    <bean id="employeeDao" class="com.imooc.spring.aop.dao.EmployeeDao"/>
    <bean id="employeeService" class="com.imooc.spring.aop.service.EmployeeService">
        <property name="employeeDao" ref="employeeDao"/>
    </bean>
    <!--2,aop配置:配置切面类Aspect Bean-->
    <bean id="methodAspect" class="com.imooc.spring.aop.aspect.MethodAspect">
    </bean>
    <aop:config>
        <!--3,定义PointCut-->
        <!--pointcut:切点,使用expression表达式说明作用范围-->
        <!--execution(public * com.imooc..*.*(..))说明切面的作用com.imooc包下所有类的所有方法-->
        <aop:pointcut id="pointcut" expression="execution(public * com.imooc..*.*(..))"/>
        <!--4,定义切面类-->
        <aop:aspect ref="methodAspect">
            <!--5,配置Advice通知:before通知,代表在目标方法运行前运行methodAspect.printExecutionTime方法-->
            <aop:before method="printExecutionTime" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

Fourth, the JoinPoint object

JoinPoint: Get the core information of the target class and target method

Core method
Specific code demonstration
Code demo
If you want to track and debug online projects in actual work, you can add this aspect and use the logback log to output the parameters

Five, PointCut cut point expression
Insert picture description here

  • public can be omitted
  • The return value can match any (*), it can also be void or String, object, etc.
  • For example, if I want to match the class name that contains service, it can be written as * service , and the others are the same
  • Give a chestnut
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/113611868