什么是AOP

什么是AOP,第1张

什么是AOP

一,why  AOP?

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

总结: 把核心业务代码和非核心业务代码进行分离,从而降低核心业务代码和非核心代码的耦合度。

举例说明:使用动态代理可以完成核心业务代码和非核心业务代码的分离

1.创建接口

public interface ArithmeticCalculator {

    
    public int add(int a,int b);

    
    public int sub(int a,int b);

    
    public int mul(int a,int b);

    
    public int div(int a,int b);
}

2.接口的实现类

public class ArithmeticCalculatorImpl implements ArithmeticCalculator{

    public int add(int a, int b) {
        int result=a+b;
        return result;
    }

    public int sub(int a, int b) {
        int result=a-b;
        return result;
    }

    public int mul(int a, int b) {
        int result=a*b;
        return result;
    }

    public int div(int a, int b) {
        int result=a/b;
        return result;
    }
}

3.创建代理类

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

public class LogProxy {//日志代理类

     private ArithmeticCalculator target;//被代理的对象
     public LogProxy(ArithmeticCalculator target){
          this.target=target;
     }

     public ArithmeticCalculator getProxyInstance(){

         //ClassLoader loader,
         //Class[] interfaces,
         //InvocationHandler h
         ClassLoader loader=target.getClass().getClassLoader();
         Class[] interfaces={ArithmeticCalculator.class};   //被代理的类实现了哪些接口
         InvocationHandler h=new InvocationHandler() {
             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                 System.out.println("The method "+method.getName()+" beigin with:"+Arrays.asList(args));
                 Object result = method.invoke(target, args);
                 System.out.println("The method "+method.getName()+" end with:"+result);
                 return result;
             }
         };
         ArithmeticCalculator instance =(ArithmeticCalculator) Proxy.newProxyInstance(loader,interfaces,h);//获取代理类对象
         return instance;
     }

}

4.测试

public class Test {
    public static void main(String[] args) {
          //创建被代理的对象。---张三
          ArithmeticCalculator target=new ArithmeticCalculatorImpl();
          //获取代理对象----李四
         LogProxy logProxy=new LogProxy(target);
         ArithmeticCalculator proxy=logProxy.getProxyInstance();
         //使用代理对象调用相应的方法
        int add = proxy.add(10, 5);
        System.out.println("结果:"+add);

        int sub = proxy.sub(10, 5);
        System.out.println("结果:"+sub);
    }
}

如上所示,这种写法很难理解?  没关系  spring框架帮你抽取了aop模式

使用AOP的步骤

(1)引入依赖--jar包   该版本号需要和spring其他框架版本号一致

        
            org.springframework
            spring-aspects
            5.2.9.RELEASE
        

(2)创建一个日志切面

@Component  //@Service @Controller一样
@Aspect  //表示该类为切面类
public class LogAspect {
    


    //该方法在指定表达式之前执行
    @Before(value = "execution(* com.lj.aop.after.*.*(..))") 
    public void before(){
        System.out.println("日志开始");
    }


    //该方法在指定表达式之后执行
    @After(value = "execution(* com.lj.aop.after.*.*(..))")
    public void after(){
        System.out.println("日志结束");
    }

}

(3)spring.xml文件配置




    
    

    
    

(4)测试

public class Test {
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("spring.xml");
        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) 			               app.getBean("arithmeticCalculator");
        double add = arithmeticCalculator.fun(10, 5,6);
        System.out.println("结果:"+add);
    }
}

以上是基于注解开启切面编程

还有一种基于xml文件配置



    
    
  
    


    
    
        
    

    
    

    
        
            
            
            
            
        
    
    
    
        
        
    


二,AOP常用注解

@Before: 前置通知, 在方法执行之前执行

@After: 后置通知, 在方法执行之后执行

@AfterRunning: 返回通知, 在方法返回结果之后执行

@AfterThrowing: 异常通知, 在方法抛出异常之后

总结:

1.AOP: 什么是AOP?

切面编程,将核心业务代码与非核心业务代码分离,降低耦合性

2.AOP在实际项目的应用:

事物 日志

3.AOP的实现原理:

动态代理。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5677178.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存