首页 > 其他分享 >Spring 09: Spring原生AOP支持

Spring 09: Spring原生AOP支持

时间:2022-08-23 15:58:29浏览次数:50  
标签:aop Spring 09 切面 AOP import public

常见Spring内置AOP接口

Before通知

  • 在目标方法被调用前调用,
  • 切面需要实现的接口:org.springframework.aop.MethodBeforeAdvice

After通知

  • 在目标方法被调用后调用
  • 切面需要实现的接口:org.springframework.aop.AfterReturningAdvice

Throws通知

  • 在目标方法抛出异常时调用
  • 切面需要实现的接口:org.springframework.aop.ThrowsAdvice

Around通知

  • 环绕通知:拦截对目标对象方法的调用,在被调用方法前后执行切面功能,例如:事务切面就是环绕通知
  • 切面需要实现的接口:org.aopalliance.intercept.MethodInterceptor

原生Before通知示例

  • 其他通知类型同理,不再重复演示

业务接口

package com.example.service;

/**
 * 定义业务接口
 */
public interface Service {
    //购买功能
    default void buy(){}
    //预定功能
    default String order(int orderNums){return null;}
}

业务实现类

package com.example.service.impl;

import com.example.service.Service;

/**
 * 图书业务实现类
 */
public class BookServiceImpl implements Service {
    @Override
    public void buy() {
        System.out.println("图书购买业务....");
    }

    @Override
    public String order(int orderNums) {
        System.out.println("预定图书: " + orderNums + " 册");
        return "预定成功";
    }
}

切面实现类

  • 相当于使用了Spring内置的AOP前置通知接口:org.springframework.aop.MethodBeforeAdvice
package com.example.advice;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class LogAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        //3个参数:目标方法,目标方法返回值,目标对象
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("[业务功能名称] :" + method.getName());
        System.out.println("[业务参数信息] :" + Arrays.toString(args));
        System.out.println("[业务办理时间] :" + sf.format(new Date()));
        System.out.println("--------- 具体业务如下 ---------");
    }
}

业务功能和切面功能整合

  • 使用applicationContext.xml,看起来更加直观
    <!--创建业务对象-->
    <bean id="bookServiceTarget" class="com.example.service.impl.BookServiceImpl"/>
    <!--创建切面的对象-->
    <bean id="logAdvice" class="com.example.advice.LogAdvice"/>


    <!-- 相当于创建动态代理对象,用来在底层绑定业务和切面-->
    <bean id="bookService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置业务接口,底层的jdk动态代理需要用-->
        <property name="interfaces" value="com.example.service.Service"/>

        <!--配置切面,可以有多个-->
        <property name="interceptorNames">
            <list>
                <value>logAdvice</value>
            </list>
        </property>

        <!--待织入切面的业务功能对象,底层的jdk动态代理需要用-->
        <property name="target" ref="bookServiceTarget"/>
    </bean>

对比手写的AOP版本5

将上述applicationContext.xml的内容和AOP版本5中的ProxyFactory对比,上述xml作用就相当于我们手写的ProxyFactory作用:
获取到业务功能对象和切面功能对象,并将他们传给底层来获取动态代理对象,在底层完成切面功能的织入
不管是xml或者通过注解来整合业务和切面,Spring底层都是像手写的AOP版本5一样,通过jdk动态代理来实现的,只不过现在封装起来了

  • AOP版本5中的ProxyFactory代理工厂
package com.example.proxy05;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理工厂,获取动态代理对象
 */
public class ProxyFactory {
    //获取jdk动态代理对象
    //Service target 接口类型的业务功能对象
    //Aop aop 接口功能的切面功能对象
    public static Object getProxy(Service target, Aop aop){
        //使用内置类,返回jdk动态代理对象
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                //获取实现的所有接口
                target.getClass().getInterfaces(),
                //调用目标对象的目标方法
                new InvocationHandler() {
                    @Override
                    public Object invoke(
                            Object obj,
                            Method method,
                            Object[] args) throws Throwable {
                        //存放目标对象的目标方法的返回值
                        Object res = null;
                        try{
                            //切面功能
                            aop.before();
                            //业务功能,根据外部调用的功能,动态代理目标对象被调用的方法
                            res = method.invoke(target, args);
                            //切面功能
                            aop.after();
                        }catch (Exception e){
                            //切面功能
                            aop.exception();
                        }
                        return res;
                    }
                }
        );
    }
}

测试

package test;

import com.example.service.Service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringAOP {
    @Test
    public void testSpringAop(){
        //创建Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取动态代理对象
        Service agent = (Service) ac.getBean("bookService");
        //调用业务功能
        String res = agent.order(10);
        System.out.println("结果: " + res);
    }
}

测试输出

  • Spring原生AOP前置通知在业务功能前顺利执行。底层的jdk动态代理也正确的反射调用了外部调用的目标方法,正确接收了参数并给出了返回值
[业务功能名称] :order
[业务参数信息] :[10]
[业务办理时间] :2022-08-23
--------- 具体业务如下 ---------
预定图书: 10 册
结果: 预定成功

Process finished with exit code 0

注意

  • 在开发中一般不常用Spring原生的AOP支持,在需要时,常使用其他专门的AOP框架
  • 手写AOP框架和简单演示Spring内置AOP通知接口是为了更好的理解AOP面向接口编程的思想

标签:aop,Spring,09,切面,AOP,import,public
From: https://www.cnblogs.com/nefu-wangxun/p/16616523.html

相关文章

  • springboot中配置文件的读取顺序
    springboot中配置文件的加载顺序1.简介 在一个springboot项目中是可以存在多个配置文件的,那这些配置文件的所在位置以及具体内容的不同会影响他们被springboot加载的优......
  • SpringBoot excel文件下载
    Filefile=newFile(xxx);response.setCharacterEncoding("utf-8");response.addHeader("Content-Disposition","attachment;filename*=UTF-8''"+URLEncoder.encod......
  • spring cloud gateway rce(CVE-2022-22947)分析
    环境搭建https://github.com/spring-cloud/spring-cloud-gateway/releases/tag/v3.0.6漏洞分析该漏洞造成原因是因为配置可写+SPEL表达式的解析导致的SpEL表达式的触......
  • 1029 [NOIP2009]最优贸易 路径最小值最大值 spfa
    链接:https://ac.nowcoder.com/acm/contest/26077/1029来源:牛客网题目描述C国有n个大城市和m条道路,每条道路连接这n个城市中的某两个城市。任......
  • Spring事务的隔离级别
    之前我们说过了事务的四个特性(ACID),不了解的可以点击这里看看->Spring事务的四个特性(ACID)今天来简单说一说隔离级别...在操作数据的时候,一般就会牵扯到数据库......
  • spring boot的静态文件
    原理:基于http协议获取远程文件实现:远程为HTTP服务器,浏览器发出请求即可基于SpringBoot下载静态文件,tomcat作为http服务器,从配置的角度完成两步即可 第一步:spring.......
  • IDEA中用Maven构建Spring Boot项目
    第一步,创建一个Maven项目第二步,配置pom.xml文件添加父依赖 <parent><artifactId>spring-boot-dependencies</artifactId><groupId>org.springfram......
  • springboot~elasticsearch对nested集合类型的字段进行不等于的检索
    对于es的数据类型来说,如果它是一个复杂类型,而我们需要把复杂类型进行检索,那么应该定义成nested类型,而对于它的检索,如果是非集合数据,它与其它类型没有分别;而如果你的nested......
  • 轻量级分布式任务调度平台(XXL-JOB介绍、原理、工作流程、XXL-JOB环境搭建集成springb
    轻量级分布式任务调度平台(一、XXL-JOB介绍、原理、工作流程)XXL-JOB#【轻量级分布式任务调度平台】(1)基本介绍#XXL-JOB是一个轻量级分布式任务调度平台,主打特点是......
  • 基于SpringSecurity的@PreAuthorize实现自定义权限校验方法
    一、前言在我们一般的web系统中必不可少的就是权限的配置,也有经典的RBAC权限模型,是基于角色的权限控制。这是目前最常被开发者使用也是相对易用、通用权限模型。当然Sprin......