Spring(6)——AOP

1 AOP

1 What is AOP
AOP (Aspect Oriented Programming) means: aspect-oriented programming , a technology that realizes the unified maintenance of program functions through pre-compilation and runtime dynamic agents. AOP is a continuation of OOP, a hot spot in software development, and an important content in the Spring framework, and a derivative paradigm of functional programming. The use of AOP can isolate the various parts of the business logic, so that the coupling between the various parts of the business logic is reduced, the reusability of the program is improved, and the development efficiency is improved.

2 The role of Aop in Spring

3 Use Spring to implement Aop

Import dependencies

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

3.1 Implementation through Spring API

First write our business interface and implementation class

package com.zs.service;

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

package com.zs.service;

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

    @Override
    public void delete() {
    
    
        System.out.println("deleteUser");
    }

    @Override
    public void update() {
    
    
        System.out.println("updateUser");
    }

    @Override
    public void search() {
    
    
        System.out.println("searchUser");
    }
}

Then to write our enhancement class, we write two, one pre-enhancement and one post-enhancement

package com.zs.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    
    
    @Override
    //o 返回值
    //method被调用的方法
    //objects 被调用的方法的对象的参数
    //o1 被调用的目标对象
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
    
    
        System.out.println(o1.getClass().getName() + "的" + method.getName() + "方法被执行了,返回值" + o );
    }
}

package com.zs.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforeLog implements MethodBeforeAdvice {
    
    

    @Override
    //method : 要执行的目标对象的方法
    //objects : 被调用的方法的参数
    //Object : 目标对象
    public void before(Method method, Object[] objects, Object o) throws Throwable {
    
    
        System.out.println(o.getClass().getName() + "的" + method.getName() + "方法被执行了");
    }
}

Finally, go to the spring file to register, and implement the aop cut-in implementation, pay attention to the import constraints

<?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">

    <bean id="userService" class="com.zs.service.UserServiceImpl"/>
    <bean id="before" class="com.zs.log.BeforeLog"/>
    <bean id="after" class="com.zs.log.AfterLog"/>

<!--    配置aop:
            -->
    <aop:config>
<!--        配置切入点-->
        <aop:pointcut id="pointcut" expression="execution(* com.zs.service.UserServiceImpl.*(..))"/>
<!--        配置环绕-->
        <aop:advisor advice-ref="before" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="after" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

test

import com.zs.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 动态代理的是接口
        UserService userService = (UserService) context.getBean("userService");
        userService.delete();
    }
}

3.2 Custom class to achieve Aop

The target business class remains unchanged is still the
first step of userServiceImpl : write our own entry class

package com.zs.log;

public class LogPointCut {
    
    
    public void before() {
    
    
        System.out.println("======before======");
    }
    public void after() {
    
    
        System.out.println("======after======");
    }
}

Go to spring configuration

<?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">

    <bean id="logPointCut" class="com.zs.log.LogPointCut"/>
    <bean id="service" class="com.zs.service.UserServiceImpl"/>

    <aop:config>
<!--        配置切面-->
        <aop:aspect id="" ref="logPointCut">
<!--            配置切入点-->
            <aop:pointcut id="pointcut" expression="execution(* com.zs.service.UserServiceImpl.*(..))"/>
<!--            配置通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

</beans>

test

import com.zs.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) context.getBean("service");
        userService.delete();
    }
}

3.1 Implementation using annotations

Step 1: Write an enhanced class implemented by annotations

package com.zs.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotationPointcut {
    
    
    @Before("execution(* com.zs.service.UserServiceImpl.*(..))")
    public void before(){
    
    
        System.out.println("---------方法执行前---------");
    }

    @After("execution(* com.zs.service.UserServiceImpl.*(..))")
    public void after(){
    
    
        System.out.println("---------方法执行后---------");
    }

    @Around("execution(* com.zs.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
    
    
        System.out.println("环绕前");
        System.out.println("签名:"+jp.getSignature());
        //执行目标方法proceed
        Object proceed = jp.proceed();
        System.out.println("环绕后");
        System.out.println(proceed);
    }
}

Step 2: In the Spring configuration file, register the bean and add configuration that supports annotations

<?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">

    <bean id="service" class="com.zs.service.UserServiceImpl"/>
   <!--第三种方式:注解实现-->
    <bean id="annotationPointcut" class="com.zs.log.AnnotationPointcut"/>
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>

</beans>

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/113028198