Spring框架AOP面向切面编程原理全面分析

 更新时间:2021年09月15日 14:52:10   作者:DrLai  
这篇文章主要介绍了Spring框架AOP面向切面编程的全面分析,文中附含详细的示例代码分析,有需要的朋友可以借鉴参考下,希望能够有所帮助

1.什么是AOP

AOP:Aspect Oriented Programming ⾯向切⾯编程。

AOP面向切面的优势

  • 降低模块之间的耦合度。
  • 使系统更容易扩展。 更好的代码复⽤。
  • ⾮业务代码更加集中,不分散,便于统⼀管理。
  • 业务代码更加简洁存粹,不参杂其他代码的影响。

AOP 是对⾯向对象编程的⼀个补充,在运⾏时,动态地将代码切⼊到类的指定⽅法、指定位置上的编程 思想就是⾯向切⾯编程。将不同⽅法的同⼀个位置抽象成⼀个切⾯对象,对该切⾯对象进⾏编程就是 AOP。

AOP需要添加的依赖

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.3.9</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.3.9</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.9</version>
    </dependency>

2.简述AOP工作运行原理

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class MyInvocationHandler implements InvocationHandler {
    private Object object=null;
    public Object bind(Object object){
        this.object=object;
        return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName()+"方法的参数"+ Arrays.toString(args));
        Object result=method.invoke(this.object,args);
        System.out.println(method.getName()+"的结果是"+result);
        return result;
    }
}

以上是动态创建AOP的方法,首先通过bind返回

Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass.getInterfaces(),this)返回代理对象

接着运用反射的invoke方法,实现面向切面编程

用method.getName()获取方法名

用arg获取属性

动态创建的总结:

以上是通过动态代理实现 AOP 的过程,⽐较复杂,不好理解,Spring 框架对 AOP 进⾏了封装,使⽤ Spring 框架可以⽤⾯向对象的思想来实现 AOP

3.使用Spring创建AOP

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Aspect
@Component
public class LoggerAspect {
    //@before表适方法执行的具体位置和时机
    @Before("execution(public int Util.impl.Calimpl.*(..))")
    public void before(JoinPoint joinPoint){
        //获取方法名
        String name=joinPoint.getSignature().getName();
        //获取参数
        String args= Arrays.toString(joinPoint.getArgs());
        System.out.println(name+"方法的参数是"+args);
    }
    @After("execution(public int Util.impl.Calimpl.*(..))")
    public  void after(JoinPoint joinPoint){
        String name=joinPoint.getSignature().getName();
        System.out.println(name+"执行完毕");
    }
    @AfterReturning(value = "execution(public int Util.impl.Calimpl.*(..))",returning = "result")
    public void returning(JoinPoint joinPoint,Object result){
        System.out.println("结果是:"+result);
    }
}

注解@Aspect指的是面向切面编程

@Component指的是交给Ioc容器管理

通过Joinpoint.getSignature().getname()获取方法名

然后实现在调用这个方法前,进行运行所需要的日志信息

测试类

import Util.Cal;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-aop.xml");
        Cal proxy = (Cal) applicationContext.getBean("calimpl");
        proxy.add(1,1);
    }
}

还是通过Application 读取Spring.xml 再将getBean 默认小写名字,再调用方法即可

Spring.xml

<?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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
    <!-- ⾃动扫描 -->
    <context:component-scan base-package="Util">
    </context:component-scan>
    <!-- 是Aspect注解⽣效,为⽬标类⾃动⽣成代理对象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

以上就是Spring框架AOP面向切面编程全面分析的详细内容,更多关于Spring框架AOP面向切面的资料请关注脚本之家其它相关文章!

相关文章

  • 小白教程! Linux服务器上JDK安装配置方法

    小白教程! Linux服务器上JDK安装配置方法

    这篇文章主要为大家详细介绍了Linux服务器上JDK安装配置方法,小白教程!具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • springboot中使用redis的方法代码详解

    springboot中使用redis的方法代码详解

    &#8203;redis 作为一个高性能的内存数据库,如果不会用就太落伍了,之前在 node.js 中用过 redis,本篇记录如何将 redis 集成到 spring boot 中。感兴趣的朋友跟随小编一起看看吧
    2019-05-05
  • Java构造方法有什么作用?

    Java构造方法有什么作用?

    在本篇文章里小编给大家介绍了关于Java构造方法的作用以及相关的基础知识点,对此有需要的朋友们可以跟着学习下。
    2022-11-11
  • springboot配置文件中属性变量引用方式@@解读

    springboot配置文件中属性变量引用方式@@解读

    这篇文章主要介绍了springboot配置文件中属性变量引用方式@@解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • java spring整合junit操作(有详细的分析过程)

    java spring整合junit操作(有详细的分析过程)

    这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • 使用AOP的@Around后无返回值的解决

    使用AOP的@Around后无返回值的解决

    这篇文章主要介绍了使用AOP的@Around后无返回值的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • SpringMVC通过模型视图ModelAndView渲染视图的实现

    SpringMVC通过模型视图ModelAndView渲染视图的实现

    这篇文章主要介绍了SpringMVC通过模型视图ModelAndView渲染视图的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Java获取客户端真实IP地址过程解析

    Java获取客户端真实IP地址过程解析

    这篇文章主要介绍了Java获取客户端真实IP地址过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • java获得mysql和oracle链接的类

    java获得mysql和oracle链接的类

    这篇文章主要介绍了java获得mysql和oracle链接的类,可实现基于jdbc的mysql与oracle数据库连接,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • Spring Boot 使用Druid详解

    Spring Boot 使用Druid详解

    本篇文章主要介绍了Spring Boot 使用Druid配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05

最新评论