ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Spring之AOP面向切面编程详细分析

2021-08-01 12:02:32  阅读:139  来源: 互联网

标签:object Spring AOP Object import org 详细分析 public


1.1什么是AOP

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

1.2AOP面向切面的优势

        降低模块之间的耦合度。

        使系统更容易扩展。 更好的代码复⽤。

        ⾮业务代码更加集中,不分散,便于统⼀管理。

        业务代码更加简洁存粹,不参杂其他代码的影响。

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

1.3AOP需要添加的依赖

<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.1简述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获取属性

2.2动态创建的总结:

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

3.1使用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()获取方法名

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

3.2测试类

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 默认小写名字,再调用方法即可

3.3 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>

标签:object,Spring,AOP,Object,import,org,详细分析,public
来源: https://blog.csdn.net/DrLai/article/details/119295952

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有