Spring5(3)-面向切面编程

本文详细介绍了Spring的面向切面编程(AOP),包括AOP的基本概念、手动实现AOP框架的几个版本,重点讲解了Spring支持的AOP实现,如通知类型、AspectJ框架的使用、切入点表达式等,并给出了前置、后置、环绕通知的开发步骤和应用实例。
摘要由CSDN通过智能技术生成

目录

一.概念

二.手写AOP框架

三.Spring支持的AOP的实现

1.Spring的AOP的类型 

2.AOP的常用术语

3.AspectJ框架

(1) Aspect的通知类型

(2)AspectJ的切入点表达式

(3)使用AspectJ的环境

4.前置通知

(1)前置通知流程分析

(2)前置通知切面方法开发

5.后置通知

(1)后置通知流程分析

(2)后置通知切面方法开发

6.环绕通知

(1)环绕通知执行流程分析

(2)环绕通知切面方法开发

7.最终通知

8.为一个方法添加多种通知


一.概念

AOP(Aspect Orient Programming)。

切面:公共的,通用的,重复的功能称为切面,面向切面编程就是将切面提取出来,单独开发,在需要调用的方法中通过动态代理方式进行织入。

二.手写AOP框架

版本1:


//图书购买业务和事务切面耦合在一起
public class BookServiceImpl {
    public void buy(){
        try{
            System.out.println("事务开启");
            System.out.println("图书购买业务功能实现");
            System.out.println("事务提交");
        }catch(Exception e){
            System.out.println("事务回滚");
        }
    }
}

 版本2:

public class BookServiceImpl {
    //todo 在父类中只有业务
    public void buy(){
        System.out.println("图书购买业务功能实现");
    }
}
//todo 子类是代理类,将父类的图书购买功能添加事务的切面
public class SubBookServiceImpl extends BookServiceImpl{
    @Override
    public void buy() {
        try{
            //todo 事务的切面
            System.out.println("事务开启");
            //todo 主业务实现
            super.buy();
            //todo 事务切面
            System.out.println("事务提交");
        }catch(Exception e){
            System.out.println("事务回滚");
        }
    }
}

将业务和切面分开,实现了相同的功能

版本3:

静态代理,实现业务灵活切换,切面的功能在代理中体现。

public interface Service {
    //todo 业务功能
    void buy();
}
//todo 业务功能的具体实现
public class BookServiceImpl implements Service{
    @Override
    public void buy() {
        System.out.println("图书购买业务实现");
    }
}
public class FoodServiceImpl implements Service{
    @Override
    public void buy() {
        System.out.println("购买食品业务");
    }
/*todo 静态代理已经实现了目标对象的灵活切换,如图书购买业务,食品购买业务*/
public class Agent implements Service{
    //todo 设计成员变量为接口,为了灵活切换目标对象
    public Service target;
    //todo 使用构造方法传入目标对象
    public Agent(Service target){
        this.target = target;
    }
    @Override
    public void buy() {
        try{
            //切面功能
            System.out.println("事务开启");
            //业务功能
            target.buy();
            //切面功能
            System.out.println("事务开启");
        }catch(Exception e){
            System.out.println("事务回滚");
        }
    }
}
public class Test {
    @org.junit.Test
    public void test(){
        Agent bookAgent = new Agent(new BookServiceImpl());
        bookAgent.buy();
        Agent foodAgent = new Agent(new FoodServiceImpl());
        foodAgent.buy();
    }
}

版本4:

在静态代理中,会发现代理实现的切面功能是死的只能实现事务功能,不能灵活调用其他的切面功能比如日志权限验证功能。

有了接口使业务的功能实现更加灵活。

 代理要实现切面接口而不是将切面接口的对象传进来。

要是传对象的话,业务接口和切面接口耦合在代理对象中,业务有变化或切面有变化,代理都得改变。

public interface AOP {
    //default 实现类没必要实现该接口中所有的方法
    default  void before(){};

    default void after(){};

    default void exception(){};
}
public class LogAOP implements AOP{
    @Override
    public void before() {
        System.out.println("日志输出");
    }
}
public class TransactionAOP implements AOP{
    @Override
    public void before() {
        System.out.println("事务开启");
    }

    @Override
    public void after() {
        System.out.println("事务提交");
    }

    @Override
    public void exception() {
        System.out.println("事务回滚");
    }
}
/*todo 静态代理已经实现了目标对象的灵活切换,如图书购买业务,食品购买业务*/
public class Agent implements Service,AOP{
    //todo 设计成员变量为接口,为了灵活切换目标对象
    public Service target;
    public AOP aop;
    //todo 使用构造方法传入目标对象
    public Agent(Service target,AOP aop){
        this.target = target;
        this.aop = aop;
    }
    @Override
    public void buy() {
        try{
            //切面功能
            aop.before();
            //业务功能
            target.buy();
            //切面功能
            aop.after();
        }catch(Exception e){
            aop.exception();
        }
    }
}
public class Test {
    @org.junit.Test
    public void test(){
        pro4.Agent bookAgent = new pro4.Agent(new Boo
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值