Aop-related concepts and applications in Spring

Common concepts of Aop:

Aspect (aspect) : The Aspect declaration is similar to the class declaration in Java, and some Pointcuts and corresponding Advice will be included in the Aspect.
Joint point (connection point) : Indicates a point clearly defined in the program, including method calls, access to class members, and execution of exception handler blocks. It can also nest other joint points.
**Pointcut (cut point):** represents a group of joint points, these joint points are either combined through logical relationships, or gathered through wildcards, regular expressions, etc., which define what the corresponding Advice will happen place.
**Advice (enhanced): **Advice defines the specific operations to be done by the program points defined in Pointcut. It uses before, after and around to distinguish whether it is before, after or instead of the code executed at each joint point.
Target (target object) : weaves into the target object of Advice.
Weaving (weaving) : The process of connecting Aspect and other objects and creating an Adviced object.

package aopanno;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(1)
public class PersonDemo {
    
    
    @After(value="execution(* aopanno.User.add(..))")
    public void after(){
    
    
        System.out.println("我是Personafter先执行的哦................");
    }
}

package aopanno;

import org.springframework.stereotype.Component;

@Component
public class User {
    
    
    public void add(){
    
    
        System.out.println("add........");
    }
}

package aopanno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//被增强的类
@Component
@Aspect
@Order(9)
//生成代理对象
public class UserProxd {
    
    
    //相同切入点抽取
    @Pointcut(value = "execution(* aopanno.User.add(..))")
    public  void pointcut(){
    
    

    }
    @Before(value="pointcut()")
    //@Before 表示add方法前置通知
    public void before(){
    
    
        //前置通知
        System.out.println("before....................");
    }
    @After(value="execution(* aopanno.User.add(..))")
    public void after(){
    
    
        System.out.println("after................");
    }
    //返回后通知
    @AfterReturning(value="execution(* aopanno.User.add(..))")
    public void afterreturning(){
    
    
        System.out.println("afterreturning................");
    }
    //异常通知
    @AfterThrowing(value="execution(* aopanno.User.add(..))")
    public void afterthrowing(){
    
    
        int i=9/7;
        System.out.println("afterthrowing................");
    }
    @Around(value="execution(* aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
    
    
        System.out.println("around前");
        //被增强得方法执行
        proceedingJoinPoint.proceed();
        System.out.println("around后");

    }
}

test class

package aopanno.Test;

import aopanno.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    
    
    @Test
    public static void main(String[] args) {
    
    
        ApplicationContext  context=new ClassPathXmlApplicationContext("Bean1.xml");
        User user = context.getBean("user",User.class);
        user.add();
    }
}

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://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 http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="aopanno"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

It's not easy to create, I heard that handsome guys and beautiful women will like and follow below!

おすすめ

転載: blog.csdn.net/a910247/article/details/125398599