AOP of Spring Learning Notes

Keywords: Spring Programming Java xml

AOP(Aspect-Oriented Programming) is only a programming specification for aspect-oriented programming, in order to improve OOP programming and supplement its shortcomings.
Popularly speaking, it is to extract commonly used modules (methods), group them into classes, and insert them where they need to be used.

Several terms

  1. Cross-cutting concerns: the same kind of non-core business extracted from each method
  2. Aspects: Classes encapsulating "cross-cutting concerns"
  3. Notification: Specific implementation of cross-cutting concerns in cross-cutting
  4. Connection Points: A tangent approach is used
  5. AOP proxy: The AOP framework uses the proxy pattern to create objects to insert notifications (i.e. application facets) at join points, that is, to apply facets to target objects through proxies.
  6. Target classes: Classes that use tangents, that is, sets of join points

Notification type:
1. @Before: Pre-notification, executed before method execution
2. @After: Post-notification, executed after method execution
3. @AfterRunning: Returns notifications, executed after method returns results
4. @AfterThrowing: Exception notification, executed after method throws an exception
5. @Around: Around notifications, around methods

AOP Implementation Based on Annotation

1. guide pack

com.springsource.net.sf.cglib -2.2.0.jar
com.springsource.org.aopalliance-1.0.0 .jar
com.springsource.org.aspectj.weaver-1.6.8 .RELEASE.jar
commons-logging-1.1.3. jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE. jar

2. Configure in applicationContext.xml to enable annotation-based AOP functionality while configuring package scanning


3. Define a section class and add it to the IOC container

@ Aspect// indicates that this is a tangent class
@ Component//Add IOC Container

4. Declare notification methods in aspect classes

/*----AOPtosimpleCalculateClass-----*/
package com.zst.testAOP;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
//Around Advice
@Aspect
@Component
public class AOPtosimpleCalculateClass {

     @Around(value="execution(public *   com.zst.testAOP.simpleCalculateImp.*(..) )")
    public Object theAround(ProceedingJoinPoint point) {
        Object[] args = point.getArgs();// Get a list of the parameters of the method
        List<Object> asList = Arrays.asList(args);

        Signature signature = point.getSignature();// Get the signature of this method
        String sigName = signature.getName();
        System.out.println("signature=" + signature);
        System.out.println("signature.getName()=" + sigName);
        Object obj = null;
        try {
            try {
                System.out.println("Amount to @Before  ");
                obj = point.proceed();// Execute this method
            } finally {
                System.out.println("Amount to @After  ");
            }
            System.out.println("Amount to @AfterRunning ");
        } catch (Throwable e) {
            //This corresponds to @AfterThrowing
            System.out.println("Abnormal");
            e.printStackTrace();
        } 
        return obj;
    }
}


/*----AOPTwo-----*/
package com.zst.testAOP;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AOPTwo {

    @Pointcut(value="execution(public * com.zst.testAOP.simpleCalculateImp.*(..)  )")
    public void setIT(){

    }

    @Before(value="setIT()")
    public void before(JoinPoint point){//org.aspectj.lang.JoinPoint
        System.out.println("The sectional class of the second configuration————@Before start");
        Object[] args = point.getArgs();
        List<Object> asList = Arrays.asList(args);
        System.out.println(asList);
        System.out.println("The sectional class of the second configuration————@Before End");
    }

    @After(value="setIT()")
    public void after(){
        System.out.println("The sectional class of the second configuration————@After");
    }

    @AfterReturning(value="setIT()" ,returning="theReturn")
    public void afterReturnning(Object theReturn){
        System.out.println("The sectional class of the second configuration————@AfterReturning start");
        System.out.println("The parameters obtained are"+theReturn);
        System.out.println("The sectional class of the second configuration————@AfterReturning End");
    }

    @AfterThrowing(value="setIT()" ,throwing="ex")
    public void catchThrow(Exception ex){
        System.out.println("The sectional class of the second configuration————@AfterThrowing start");
        System.out.println(ex);
        System.out.println("The sectional class of the second configuration————@AfterThrowing End");

    }
}


/*test method*/
    public void tett(){
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        simpleCalculate bean = app.getBean("theCalculateBean",simpleCalculate.class);
        bean.div(23,6);
        //Normal: @Before, @After, @AfterReturning
        //Exceptions: @Before, @After, @AfterThrowing
    }

Posted by supratwinturbo on Mon, 27 May 2019 15:05:55 -0700