Introduction to Spring -- 06

Keywords: Spring

Spring

AOP

1. What is AOP?

In the software industry, AOP is the abbreviation of Aspect Oriented Programming, which means: Aspect Oriented Programming, a technology to realize 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 in Spring framework, and a derivative paradigm of functional programming. AOP can isolate each part of business logic, reduce the coupling between each part of business logic, improve the reusability of program, and improve the efficiency of development.                                  ------- From Baidu Encyclopedia

  2. The role of AOP in Spring

1. Declarative transactions are provided

2. Allow the user to customize the section (section oriented programming)

In aspect oriented programming, you may encounter the following terms:

  • Crosscutting concerns: methods or functions that span multiple modules of an application. That is, the part that has nothing to do with our business logic, but we need to focus on is crosscutting concerns. Such as logging, security, caching, transactions, etc

  • ASPECT: a special object whose crosscutting concerns are modularized. That is, it is a class.

  • Advice: work that must be completed in all aspects. That is, it is a method in a class.

  • Target: the target object of the proxy.

  • Proxy: the generated proxy object.

  • PointCut: refers to the connection points to be intercepted.

  • Join point: the execution point that matches the pointcut.

 

Five methods are provided in Advice for us to use

Notification type Connection point Implementation interface
Before advice Before method org.springfraamework.aop.MethodBeforeAdvice
Post notification After method org.springfraamework.aop.AfterReturningAdvice
Around Advice Before and after method org.aopalliance.intercept.MethodInterceptor
Exception throw notification Method throws an exception org.springfraamework.aop.ThrowsAdvice
Introduction notice Add a new method attribute to the class org.springfraamework.aop.IntroductionInterceptor

 

3. Use of AOP

Import its dependencies and constraints before use

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

========================================


<?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/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

 

1. Interface using AOP

1. Write a UserService interface

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

2. Write a UserServiceImpl

public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("Added a user");
    }

    @Override
    public void delete() {
        System.out.println("A user was deleted");
    }

    @Override
    public void update() {
        System.out.println("Modified a user");
    }

    @Override
    public void select() {
        System.out.println("A user was queried");
    }
}

3. Write two pointcuts and inherit the interface of AOP

Advance notice

public class Log implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        assert o != null;
        System.out.println(o.getClass().getName() + "of" + method.getName() + "Executed");
    }
}

Post notification

public class AfterLog implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("Yes" + method.getName() + "The returned result is:" + o);
    }
}

4. Configure AOP

    <!-- bean register -->
    <bean class="com.charles.service.UserServiceImpl" id="userService"/>
    <bean class="com.charles.log.AfterLog" id="afterLog"/>
    <bean class="com.charles.log.Log" id="log"/>

    <!-- Method 1: use native API Interface -->
    <!-- to configure AOP: Import required aop Constraints of -->
    <aop:config>
        <!-- Entry point: expression: Expression (fixed format)
        execution(Location to execute *(Modification value) *(Return value) *(Class name) *(Method name) *(Parameters)) -->
        <aop:pointcut id="pointcut" expression="execution(* com.charles.service.UserServiceImpl.*(..))"/>

        <!-- Perform surround enhancement -->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

 

2. Customize AOP

1. Customize an AOP class

public class DiyLog {

    public void before(){
        System.out.println("===========Before advice ===========");
    }

    public void after(){
        System.out.println("===========Post notification===========");
    }

}

2. Configure AOP

    <bean class="com.charles.log.DiyLog" id="diyLog"/>

    <!-- Party 12: custom class -->
    <aop:config>
        <aop:aspect ref="diyLog">
            <aop:pointcut id="pointcut" expression="execution(* com.charles.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

3. Test

 

3. Use notes

1. Write AOP class

@Aspect
public class AnnovationLog {

    @Before("execution(* com.charles.service.UserServiceImpl.*(..))")
    public void beforeLog(){
        System.out.println("===========Before advice ===========");
    }

    @After("execution(* com.charles.service.UserServiceImpl.*(..))")
    public void afterLog(){
        System.out.println("===========Post notification===========");
    }

    @Around("execution(* com.charles.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Surround front");
        Signature signature = joinPoint.getSignature();
        System.out.println(signature);
        joinPoint.proceed();

        System.out.println("After surround");
    }

}

2. Configure AOP and enable notes

    <!-- Open annotation -->
    <aop:aspectj-autoproxy/>
    <bean class="com.charles.log.AnnovationLog" id="annovationLog"/>

 

 

Posted by cookiemonster4470 on Thu, 04 Nov 2021 08:36:33 -0700