首页 > 编程语言 > 详细

【2】Spring-- AOP的简单使用

时间:2021-03-12 15:19:30      阅读:20      评论:0      收藏:0      [点我收藏+]

【2】Spring-- AOP的简单使用

? 面向切面编程(AOP)提供了另一种思考程序结构的方式来补充面向对象编程(OOP)。 OOP 中模块化的单元是,而在 AOP 中模块化是切面。切面使关注点(例如事务 Management)的模块化可以跨越多种类和对象

? Spring 的关键组件之一是 AOP 框架。尽管 Spring IoC 容器不依赖于 AOP(这意味着您不需要使用 AOP),但 AOP 是对 Spring IoC 的补充,以提供功能非常强大的中间件解决方案。

? AOP 要达到的效果是,保证开发者不修改源代码的前提下,去为系统中的业务组件添加某种通用功能。AOP 的本质是由 AOP 框架修改业务组件的多个方法的源代码,AOP 其实就是代理模式的典型应用。

? 按照 AOP 框架修改源代码的时机,可以将其分为两类:

  • 静态 AOP 实现, AOP 框架在编译阶段对程序源代码进行修改,生成了静态的 AOP 代理类(生成的 *.class 文件已经被改掉了,需要使用特定的编译器),比如 AspectJ。
  • 动态 AOP 实现, AOP 框架在运行阶段对动态生成代理对象(在内存中以 JDK 动态代理,或 CGlib 动态地生成 AOP 代理类),如 SpringAOP。

2.1 概念

AOP编程的相关概念很多,主要如下:

  • 通知(Advice): AOP 框架中的增强处理。通知描述了切面何时执行以及如何执行增强处理。
  • 连接点(join point): 连接点表示应用执行过程中能够插入切面的一个点,这个点可以是方法的调用、异常的抛出。在 Spring AOP 中,连接点总是方法的调用。
  • 切点(PointCut): 可以插入增强处理的连接点
  • 切面(Aspect): 切面是通知和切点的结合。
  • 引入(Introduction):引入允许我们向现有的类添加新的方法或者属性。
  • 织入(Weaving): 将增强处理添加到目标对象中,并创建一个被增强的对象,这个过程就是织入。实现织入功能的包有Spring AopAspectJ

技术分享图片

通俗的理解如下:

技术分享图片

  • 连接点:类中的每个方法都可以被称为连接点,上图中方法1(),方法2(),方法3(),方法4(),方法5()都可以被当做连接点,但也只是概念上的连接点,还没有写入到java代码中。
  • 切点:每个概念上的连接点,配置到代码中后,就成为了能够切入增强功能的切入点。在javaConfig类中使用@PointCut声明某个包下的某个类中的某个方法是切入点,可以给这个切点起个名字。
  • 通知:增强的功能是什么,以及在切点位置的方法执行之前还是之后。
  • 切面:切点+通知;
  • 织入:配置好切点,通知之后,将通知嵌入到切点的过程叫做织入,实现该功能的有Spring aop包和AspectJ包

2.2 简单案例

  1. 使用springIOC容器定义好基本的POJOs
package pojo;

public interface Buy {
    void buy();
}
package pojo;

public class Boy implements Buy{
    @Override
    public void buy() {
        System.out.println("男的买了游戏机!!!!");
    }

    public void play(){
        System.out.println("打游戏");
    }
}
package pojo;

public class Girl implements Buy{
    @Override
    public void buy() {
        System.out.println("女孩买了衣服");
    }

    public void guangjie(){
        System.out.println("guangjie");
    }
}

  1. 使用JavaConfig的方式配置文件
package configrution;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import pojo.Boy;
import pojo.Girl;

@Configuration
public class BuyConfigruation {

    @Bean
    public Boy boy(){
        return new Boy();
    }

    @Bean
    public Girl girl(){
        return new Girl();
    }
}
  1. 测试基本功能
import configrution.BuyConfigruation;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import pojo.Boy;
import pojo.Girl;

public class SpringTest {

    @Test
    public void springTest01(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BuyConfigruation.class);
        Boy boy = applicationContext.getBean("boy", Boy.class);
        Girl girl = applicationContext.getBean("girl", Girl.class);
        boy.buy();
        boy.play();
        girl.buy();
        girl.guangjie();
    }
}

结果:

男的买了游戏机!!!!
打游戏
女孩买了衣服
guangjie
  1. 使用JavaConfig的方式配置AOP(AspectJ包导入)
package configrution;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class BuyAspectJ {

    @Pointcut("execution(* *.buy(..))")
    public void point1() {

    }

    @Pointcut("execution(* *.guangjie(..))")
    public void point2(){

    }

    @Pointcut("execution(* *.play(..))")
    public void point3(){

    }


    @Before("point1()")
    public void enhancementMethod(){
        System.out.println("在买之前执行");
    }

    @After("point1()")
    public void enhancementMethodAfter(){
        System.out.println("买完了");
    }

    @Before("point2()")
    public void enhancement(){
        System.out.println("开机");
    }
    @After("point2()")
    public void enhancement1(){
        System.out.println("关机");
    }

    @Before("point3()")
    public void enhancement2(){
        System.out.println("化妆");
    }

    @After("point3()")
    public void enhancement3(){
        System.out.println("zhenlei");
    }

}
  1. 使得这些基于javaconfig的注解的配置方式生效,修改配置文件
package configrution;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import pojo.Boy;
import pojo.Girl;

@Configuration
@ComponentScan(basePackageClasses = {BuyAspectJ.class}) //让这个类的所有注解生效
@EnableAspectJAutoProxy(proxyTargetClass = true)  // 开启AOP
public class BuyConfigruation {

    @Bean
    public Boy boy(){
        return new Boy();
    }

    @Bean
    public Girl girl(){
        return new Girl();
    }
}

结果:

在买之前执行
男的买了游戏机!!!!
买完了
化妆
打游戏
zhenlei
在买之前执行
女孩买了衣服
买完了
开机
guangjie
关机

2.3 总结

AOP,

  • 首先定义切入点@Pointcut
  • 然后定义通知@Before @After @Around。。。。。,
  • 让注解生效:@ComponentScan(basePackageClasses = {BuyAspectJ.class}) //让这个类的所有注解生效
  • 开启AOP:@EnableAspectJAutoProxy(proxyTargetClass = true)

【2】Spring-- AOP的简单使用

原文:https://www.cnblogs.com/mjoe/p/14523221.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!