day04 Aop学习

在运行时,动态地将代码切入到类的指定法、指定位置的编程思想就是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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在运行时,动态地将代码切入到类的指定法、指定位置的编程思想就是AOP(面向切面编程)AOP常用于编程中的事物,日志...
    山下_26阅读 93评论 0 1
  • 概述 Spring是什么? Spring是一个开源框架,为了解决企业应用开发的复杂性而创建的,但是现在已经不止于企...
    琅筑阅读 1,207评论 2 8
  • 基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹...
    永顺阅读 8,365评论 5 114
  • IoC 容器 Bean 的作用域 自定义作用域实现 org.springframework.beans.facto...
    Hsinwong阅读 2,511评论 0 7
  • 团队开发框架实战—面向切面的编程 AOP 引言 软件开发的目标是要对世界的部分元素或者信息流建立模型,实现软件系统...
    Bobby0322阅读 4,186评论 4 49