SpringAOP_ Set injection implementation

SpringAOP_ Set injection implementation

Coupon website m.cps3 cn

AOP_ Preliminary understanding of aspect oriented programming

Let's imagine a scenario. You are writing a project, and multiple modules in the development process have a piece of repeated code, so you choose to abstract it into a method, and then call this method where necessary. When you need to modify this piece of code, you only need to modify this method. One day, your Boss gives a new requirement. You need to abstract a method, and then call this method in each module that needs this method. This may give you a headache. You need to modify a lot of code, so you will think, can you add some function to the system business without modifying the source code? Fortunately, AOP can solve this problem well.

Brief introduction

AOP: on the premise of ensuring that developers do not modify the source code, they add some general functions to the business components in the system. In essence, the AOP framework modifies the source code of multiple methods of business components. We divide them into two categories:

  • Static AOP
    The AOP framework modifies the program source code in the compilation stage and generates static AOP proxy classes (the generated *. class file has been changed and a specific compiler needs to be used), such as AspectJ.
  • Dynamic AOP:
    The AOP framework dynamically generates proxy objects (JDK dynamic proxy in memory or CGlib dynamic generation of AOP proxy classes) at run time, such as spring AOP.

detailed description

Notification type of Spring

namelabelexplain
Before advice < aop:before >Used to configure pre notification. Specifies that the enhanced method is executed before the pointcut method
Post notification< aop:after-returning >Used to configure post notification. Specifies that the enhanced method is executed after the pointcut method
Around Advice < aop:around >Used to configure surround notifications. Specifies that the enhanced method is executed before and after the pointcut method
Exception notification< aop:throwing >Used to configure exception throw notifications. Specifies that the enhanced method is executed when an exception occurs
Final notice< aop:after >Used to configure the final notification. The enhanced mode will be executed regardless of whether there are exceptions

Actual combat drill

Import dependent packages

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Create an enhanced class and its interface

Enhanced class interface:

public interface VisitService {
    //Used to realize pre notification, post notification, exception notification and final notification
    void visit(String str) throws Exception;

    //Used to implement surround notification
    void around();
}

Enhancement class:

public class VisitServiceImpl implements VisitService {
    //Enhanced classes for pre -, post -, and final exception notifications
    public void visit(String str) throws Exception{
        System.out.println(str);
        if(!str.equalsIgnoreCase("agree")){
            throw new Exception("Illegal access");
        }
    }
    //Enhanced classes around notifications
    public void around() {
        System.out.println("Around Advice ");
    }
}

Create a facet class

public class VisitAspect {
    //Before advice 
    public void visitBefore(JoinPoint joinPoint){
        System.out.println("Password:");
    }
    //The final notice shall be executed whether an error is reported or not
    public void visitAfter(JoinPoint joinPoint){
        System.out.println("Input complete");
    }
    //The post notification will not be executed if an error is reported
    public void visitSuccess(JoinPoint joinPoint){
        System.out.println("Request successful, welcome");
    }
    //Exception notification, which is executed after an error is reported
    public void visitThrow(JoinPoint joinPoint, Throwable ex){
        System.out.println("Request failed, rejected");
    }
    //Around the notice, if an error is reported, only the previous sentence will be executed
    public Object visitAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("-------surround-------");
        Object obj = proceedingJoinPoint.proceed();
        System.out.println("-------surround-------");
        return obj;
    }
}

xml configuration files, third-party constraints required

    <bean id="userDao" class="Spring_AOP.service.impl.VisitServiceImpl"></bean>
    <bean id="aspect" class="Spring_AOP.service.VisitAspect"></bean>
    
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* Spring_AOP.service.impl.VisitServiceImpl.visit(..))"/>
        <aop:pointcut id="pointcut1" expression="execution(* Spring_AOP.service.impl.VisitServiceImpl.around())"/>
        <aop:aspect ref="aspect">
             <aop:before method="visitBefore" pointcut-ref="pointcut"></aop:before>
             <aop:after method="visitAfter" pointcut-ref="pointcut"></aop:after>
             <aop:after-returning method="visitSuccess" pointcut-ref="pointcut"></aop:after-returning>
             <aop:around method="visitAround" pointcut-ref="pointcut1"></aop:around>
             <!-- Execute after error reporting aop:after-throwing -->
             <aop:after-throwing method="visitThrow" pointcut-ref="pointcut" throwing="ex"></aop:after-throwing>
         </aop:aspect>
    </aop:config>

Note: for execution()
1. execution(): the body of the expression (execution must be added).
2. The first * indicates the return value type, and the * indicates all types.
3. Package name: indicates the name of the package to be intercepted. The following two periods represent the current package and all sub packages of the current package, CN smd. service. Methods of all classes under impl package and descendant package.
4. The second * sign: indicates the class name, and the * sign indicates all classes.
5,* (..): Finally, the asterisk represents the method name, the * sign represents all methods, the parentheses behind it represent the parameters of the method, and the two periods represent any parameters.
Notes for writing: execution(* cn.smd.service.impl.. (..))

Create a test class

 public class visitTest {
    @Test
    public void VisitTest(){
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext_AOP.xml");
        VisitService visitService = app.getBean(VisitService.class);
        try {
            visitService.visit("agree");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            visitService.visit("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        visitService.around();
    }
}

test run

Password:
agree
Request successful, welcome
Input complete
Password:
ok
Request failed, rejected
Input complete
-------Surround-------
Around Advice
-------Surround-------

summary

  • Spring AOP further reduces the coupling of components and realizes decoupling
  • It can better monitor programs and intercept permissions
  • Note: when learning AOP setting injection, you need to pay attention to the status of each notice when an error is reported

The above is the implementation of spring AOP framework setting injection with annotations. If there is any error, please point it out. Thank you for your patience and constant efforts of your friend, Yang By

Added by eternalprophet on Thu, 17 Feb 2022 21:35:25 +0200