Implementation of 5 notifications based on the Aspectj annotation method under SpringAOP

1. First create the enhanced class and the enhanced class and create objects with annotations

Those who don’t have the jar package required for the AOP project can
pick it up https://pan.baidu.com/s/1wj11GkWhlLYq3nfnJFTYbA Extraction code: jdbc

User.java (enhanced class)

package AopAnon;

import org.springframework.stereotype.Component;
@Component
public class User {

    public  void add(){
        System.out.println("add.......");
    }
}


Userprotect.java (enhanced class)

package AopAnon;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Userprotect {
    //前置通知  0在被增强代码前执行
    @Before(value = "execution(* AopAnon.User.add(..))")
    public void before(){
        System.out.println("before..........");
    }
    //最终通知  原代码执行后触发
    @After(value = "execution(* AopAnon.User.add(..))")
    public void after(){
        System.out.println("After..........");
    }
    //返回通知 代码正常执行完毕后执行
    @AfterReturning(value = "execution(* AopAnon.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning..........");
    }
    //环绕通知  在被增强代码执行前和执行后都会执行一遍
    @Around(value = "execution(* AopAnon.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around前..........");
        proceedingJoinPoint.proceed();
        System.out.println("around后..........");
    }
    // 异常通知 当被增强代码执行时发生了异常时执行
    @AfterThrowing(value = "execution(* AopAnon.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing..........");
    }
}



**value = "execution(* AopAnon.User.add(..)) // Given the class and method to be enhanced, the grammatical structure of this line is (modifier returns the package of the enhanced class. Class name. Method ( Parameter list: represents 1 or more parameters)) Modifier can not be written * represents any return type

bean2.xml (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: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="AopAnon"></context:component-scan>

<!--    扫描Aspectj注释作为代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


</beans>


TestDemo.java (test class)

package AopAnon;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Testdemo {
    @Test
    public void testdemo1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");
        User user = context.getBean("user", User.class);
        user.add();

    }
}

Required resources
Insert picture description here




Implementation results
Insert picture description here



When an error occurs in the enhanced class
Insert picture description here

Guess you like

Origin blog.csdn.net/ssdssa/article/details/111349028