美文网首页
day04 Aop学习

day04 Aop学习

作者: 墨寒_3338 | 来源:发表于2019-03-10 16:10 被阅读0次

在运行时,动态地将代码切入到类的指定法、指定位置的编程思想就是AOP(面向切面编程)
AOP常用于编程中的事物,日志等。由代理机制实现

1.AOP核心概念

  • Aspect(切面):将关注点进行模块化
  • Join Point(连接点):在程序执行过程中的某个特定的点,如谋方法调用时或处理异常时
  • Advice(通知):在切面的某个特定的连接点上执行的动作
  • PointCut(切入点):匹配连接点的断言
  • Introduction(引入):声明额外的方法或某个类型的字段
  • Target Object(目标对象):被一个或多个切面所通知的对象
  • AOP Proxy(AOP代理):AOP框架创建的对象,用来实现Aspect Contract包括通知方法执行等功能
  • Weaving(织入):把切面连接到其他的应用程序类型或对象上,并创建一个Advice的对象

2.代理模式实例

建一个接口
public interface Move {
    void move();

}

继承接口的类
public class Tank implements Move{
    @Override
    public void move() {
        System.out.println("Tank is moving");
    }
}

创建代理类
public class Tankproxy implements Move{
    private Move t;
    public Tankproxy(Move t){
        this.t=t;
    }
    @Override
    public void move() {
        System.out.println("Starting");
        t.move();
        System.out.println("stop");
    }
}

main方法输出验证
public class MoveApp {
    public static void main(String[] args) {
        Tank tank=new Tank();
        Move moveProxy=new Tankproxy(tank);
        moveProxy.move();
    }
}

运行结果:

image.png

3.AOP实例

建一个接口
public interface Hello {
    String getHello();
}

继承接口的类
public class HelloImpl implements Hello{

    @Override
    public String getHello() {
        return "Hello,Spring AOP";
    }
}

AOP前置增强类
public class MyBeforeAdvice {
    //定义前置方法
    public void beforeMethod(){
        System.out.println("This is a before method");
    }
}

在xml文件中配置bean和aop
<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

       <bean id="hello" class="com.spring.quickstart.HelloImpl"/>
       <bean id="advice" class="com.spring.quickstart.MyBeforeAdvice"/>
       <aop:config>
           <aop:aspect id="before" ref="advice">
                  <aop:pointcut id="myPointCut" expression="execution(* com.spring.quickstart.*.*(..))"/>
                  <aop:before method="beforeMethod" pointcut-ref="myPointCut"/>
           </aop:aspect>
       </aop:config>
</beans>

main方法类输出验证
public class HelloApp {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("/applicationContext.xml");
        Hello hello=context.getBean(Hello.class);
        System.out.println(hello.getHello());
    }

}

运行结果:

image.png

相关文章

  • day04 Aop学习

    在运行时,动态地将代码切入到类的指定法、指定位置的编程思想就是AOP(面向切面编程)AOP常用于编程中的事物,日志...

  • day04 AOP学习

    在运行时,动态地将代码切入到类的指定法、指定位置的编程思想就是AOP(面向切面编程)AOP常用于编程中的事物,日志...

  • Android中使用AspectJ

    aop学习 深入理解Android之AOP 什么是AOP AOP是Aspect Oriented Programm...

  • Spring Aop

    官方文档 了解AOP之前,建议先学习代理设计模式 什么是AOP AOP(Aspect Oriented Progr...

  • 2019-08-25_Spring AOP切面编程学习笔记

    Spring AOP切面编程学习笔记 1 概述 AOP(Aspect Oriented Programming),...

  • Spring学习手册(11)—— SpringAOP实战

    Spring学习手册(10)—— Spring AOP配置我们学习了XML方式配置Spring AOP的方式。我们...

  • CSS2

    Day04****************************************************...

  • Spring 框架学习(四):AOP

    [TOC] Spring 框架学习(四):AOP 概述 AOP 是 Aspect Oriented Program...

  • day04(数组)

    Day04(java基础知识)学习目标 【学习目标】理解、了解、应用、记忆通过今天的学习,参训学员能够:(解释的时...

  • Spring学习笔记(六、Spring AOP基本概念)

    上一篇:Spring学习笔记(五、Bean装配(下)) 一、AOP概念 1. 什么是AOP? AOP:Aspect...

网友评论

      本文标题:day04 Aop学习

      本文链接:https://www.haomeiwen.com/subject/utvxpqtx.html