panda vernacular - Introduction to Spring AOP + actual combat

Spring two 🐮 Characteristics: IOC, AOP
Interview must ask 😄
Basically, everyone has a deep understanding of IOC. Spring helps us manage bean s through the IOC container
AOP is also widely used, but you haven't paid much attention to it
Because it basically belongs to the framework level, you don't have to care about the basic configuration once. At least you should understand it,
let`s go~~

AOP

Definition: Oriented Object Progamming

Concept:

  • Connection point - joint point

  • Entry point - point cut

  • Notice - advice

  • weaving

  • Section - aspect

    Panda understands:

  • The essence of AOP is dynamic agent

  • The essential function of AOP is to enhance the method of target object

  • We can understand these concepts by looking at the figure above:
    1. First, the method of each target object to be represented by aop is point cut
    2. Aspect is like a wall in front. Aspect is actually a class to manage notifications and pointcuts
    3. Spring will listen to the call of each pointcut method. When there is a pointcut method call, it will dynamically generate an AOP Proxy object
    4. The way to enhance pointcuts is advice, which is divided into five kinds of notifications
    5. The proxy object weaves the notification to the pointcut, so weaving is an action

To put it bluntly, the method you want to implement is represented by me. I want to do something before, after, left, right and right in the implementation of your method

Advice notification type

  • Before - pre Notification - notification before target method execution
  • After - post Notification - notification after target method execution
  • afterReturning - notify after the target method returns normally
  • afterThrowing - notify after the target method throws an exception
  • around - surround notification

dont talk. show me the code

Implementing AOP in configuration file mode

  • Start with a target class and two simple methods, method1 and method2
  • Then make a facet class and a before method for pre notification
  • Then make a configuration file to configure aspect classes, notification methods and pointcut methods,
  • Completion and test

1. Target class

public class TargetObj {
    public void method1() throws Exception { System.out.println("method1. . ");}
    public void method2(){System.out.println("method2. . "); }
    }

2. Section class

public class PandaAspect {
    public void before(){
        System.out.println("before. . ");
    }

3. Configuration file

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <bean id = "target" class = "aop2.TargetObj"></bean>
    <bean id = "advice" class = "aop2.PandaAdvice"></bean>

    <aop:config>
        <!-- section -->
        <aop:aspect ref="advice">
            <aop:before method="before" pointcut="execution(* *..*.method1(..))"/>
        </aop:aspect>
    </aop:config>
</beans>

3. Open test

public static void main(String[] args) throws Exception {
		//First read the configuration file and load the bean into the ioc container
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //Get bean
        TargetObj target = (TargetObj) context.getBean("target");
        //Execute target class method
        target.method1();
    }

Execution result:

One notification type will be, and other types can draw inferences from one instance..

Implement AOP by annotation

  • Start with a target class and two simple methods, method1 and method2
  • Then make a facet class and a before method for pre notification
  • Then make a configuration file to configure aspect classes, notification methods and pointcut methods,
  • Completion and test

1. Target class or that target class

2. Section class, configured by annotation

package annotation;

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

@Component("advice")
@Aspect
public class PandaAdvice {
    @Before(value = "execution(* *..*.method())")
    public void before(){
        System.out.println("...before...");
    }
    @After(value = "execution(* *..*.method())")
    public void after(){
        System.out.println("...after...");
    }
    @AfterThrowing(value = "execution(* *..*.method())")
    public void afterThrowing(){
        System.out.println("...afterThrowing...");
    }
    @AfterReturning(value = "execution(* *..*.method())")
    public void afterReturning(){
        System.out.println("...AfterReturning...");
    }
    @Around(value = "execution(* *..*.method())")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("...around start...");
        joinPoint.proceed();
        System.out.println("...around end...");

    }
}

3. Configuration file

<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns Namespace schemaLocation Namespace corresponding constraint file-->
<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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd ">
    <!--Spring Automatic scanning-->
    <context:component-scan base-package="annotation"></context:component-scan>
    <!--open aop-->
    <aop:aspectj-autoproxy/>
</beans>

4. Open test

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
        TargetObj target = (TargetObj)context.getBean("target");
        target.method();
    }

Execution result:

Tags: Spring AOP Dynamic Proxy

Posted by bimmer528 on Fri, 20 May 2022 22:19:13 +0300