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

1.什么是AOP

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

AOP面向切面的优势

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

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

AOP需要添加的依赖


      org.springframework
      spring-aop
      5.3.9
    
    
      org.springframework
      spring-aspects
      5.3.9
    
    
      org.springframework
      spring-context
      5.3.9
    

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



    
    
    
    
    

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

你可能感兴趣的:(Spring框架AOP面向切面编程原理全面分析)