Spring框架学习笔记05:Spring AOP基础

一、Spring AOP概述

1、AOP含义

AOP: Aspect-Oriented Programming (面向切面编程)

2、AOP作用

Spring的AOP作用在于解耦。AOP让一组类共享相同的行为(比如事务管理、日志管理、安全管理)。OOP(Object-Oriented Programming)只能通过继承类或实现接口来增加代码的耦合度,而且类继承是单根继承(不允许一子多父),阻碍了将更多的行为添加到一组类上,此时AOP可以弥补OOP的不足。

3、AOP与OOP

AOP(Aspect-Oriented Programming)—— 横向的关系
OOP(Object-Oriented Programming)—— 纵向的关系

4、AOP使用方式

Spring里有两种方式使用AOP:(1)配置方式;(2)注解方式

二、采用配置方式使用AOP

1、在net.wz.spring包里创建lesson05.aop_xml子包

在这里插入图片描述

2、在aop_xml子包里创建杀龙任务类 - SlayDragonQuest

在这里插入图片描述

package net.wz.spring.lesson05.aop_xml;

import org.springframework.stereotype.Component;


@Component
public class SlayDragonQuest {
    public void embark() {
        System.out.println("执行杀龙任务。");
    }
}

3、在aop_xml子包里创建勇敢骑士类 - BraveKnight

在这里插入图片描述

package net.wz.spring.lesson05.aop_xml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BraveKnight {
    @Autowired
    private SlayDragonQuest slayDragonQuest;

    public void embarkOnQuest() {
        slayDragonQuest.embark();
    }
}

4、在aop_xml子包里创建游吟诗人类 - Minstrel

在这里插入图片描述

package net.wz.spring.lesson05.aop_xml;

import org.springframework.stereotype.Component;


@Component
public class Minstrel {
    public void singBeforeQuest() {
        System.out.println("啦啦啦,骑士出发了!");
    }

    public void singAfterQuest() {
        System.out.println("真棒啊!骑士完成了任务!");
    }
}

5、创建Spring配置文件

在resources里创建aop_xml目录,在里面创建spring-config.xml配置文件

<?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:context="http://www.springframework.org/schema/context"
       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/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--组件扫描-->
    <context:component-scan base-package="net.wz.spring.lesson05.aop_xml" />

    <!--AOP配置-->
    <aop:config>
        <!--定义切面-->
        <aop:aspect ref="minstrel">
            <!--定义切点-->
            <aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))" />
            <!--声明前置通知-->
            <aop:before method="singBeforeQuest" pointcut-ref="embark"/>
            <!--声明后置通知-->
            <aop:after method="singAfterQuest" pointcut-ref="embark" />
        </aop:aspect>
    </aop:config>

</beans>

在这里插入图片描述

(1)切点

在使用Spring框架配置AOP时,不管是通过XML配置文件还是注解方式,都需要定义pointcut(切点)。

(2)切点表达式

定义切点表达式execution (* net.hw.spring….(…))

(3)切换函数

execution()是最常用的切点函数,整个表达式可以分为五个部分。

execution():表达式主体。
第一个*号:表示返回类型,号表示所有的类型。
包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,net.hw.spring包、子孙包下所有类的方法。
第二个
号:表示类名,*号表示所有的类。
*(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

6、创建测试类- TestKnight

在这里插入图片描述

package net.wz.spring.lesson05.aop_xml;

import net.wz.spring.lesson05.aop_xml.BraveKnight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestKnight {
    private ClassPathXmlApplicationContext context;

    @Before
    public void init() {
        // 基于Spring配置文件创建应用容器
        context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");
    }

    @Test
    public void testBraveKnight() {
        // 根据名称从应用容器里获取勇敢骑士对象
        BraveKnight braveKnight = (BraveKnight) context.getBean("braveKnight");
        // 勇敢骑士执行任务
        braveKnight.embarkOnQuest();
    }

    @After
    public void destroy() {
        // 关闭应用容器
        context.close();
    }
}

7、在pom.xml文件里添加AOP相关依赖

<!--Spring AOP-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--AspectJ支持-->
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
            <scope>runtime</scope>
        </dependency>

8、运行测试方法testBraveKnight(),查看结果

在这里插入图片描述

9、课堂练习

(1)增加拯救少女任务类与拯救少女骑士类

在这里插入图片描述

(2)在测试程序里增加对拯救少女骑士的测试方法 - testDamselRescuingKnight()

三、采用注解方式使用AOP

1、在net.wz.spring包里创建lesson05.aop_annotation子包

在这里插入图片描述

2、在aop_annotation子包里创建杀龙任务类 - SlayDragonQuest

在这里插入图片描述

package net.wz.spring.lesson05.aop_annotation;

import org.springframework.stereotype.Component;


@Component
public class SlayDragonQuest {
    public void embark() {
        System.out.println("执行杀龙任务。");
    }
}

3、在aop_annotation子包里创建勇敢骑士类 - BraveKnight

在这里插入图片描述

package net.wz.spring.lesson05.aop_annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component("mike")
public class BraveKnight {
    @Autowired
    private SlayDragonQuest slayDragonQuest;

    public void embarkOnQuest() {
        slayDragonQuest.embark();
    }
}

4、在aop_annotation子包里创建注解接口 - Action

在这里插入图片描述

package net.wz.spring.lesson05.aop_annotation;

import java.lang.annotation.*;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
    String name();
}

(1)@Target({ElementType.TYPE}) 注解

ElementType 这个枚举类型的常量提供了一个简单的分类:注解可能出现在Java程序中的语法位置(这些常量与元注解类型(@Target)一起指定在何处写入注解的合法位置)

(2)@Retention({RetentionPolicy.Runtime}) 注解

RetentionPolicy这个枚举类型的常量描述保留注解的各种策略,它们与元注解(@Retention)一起指定注释要保留多长时间

(3)@Documented注解

Documented注解表明这个注解是由 javadoc记录的,在默认情况下也有类似的记录工具。 如果一个类型声明被注解了文档化,它的注解成为公共API的一部分。

5、在aop_annotation子包里创建游吟诗人切面 - MinstrelAspect

在这里插入图片描述

package net.wz.spring.lesson05.aop_annotation;

import org.aspectj.lang.JoinPoint;
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;


@Aspect // 声明切面
@Component // 声明为Spring管理的Bean
public class MinstrelAspect {
    // 注解声明切点
    @Pointcut("execution(* *.embarkOnQuest(..))")
    public void embark() {
    }

    // 注解声明前置通知
    @Before("embark()")
    public void singBeforeQuest(JoinPoint joinPoint) {
        System.out.println("啦啦啦,骑士出发了!");
    }

    // 注解声明后置通知
    @After("embark()")
    public void singAfterQuest(JoinPoint joinPoint) {
        System.out.println("真棒啊!骑士完成了任务!");
    }
}

6、在aop_annotation子包里创建Spring配置类 - AopConfig

在这里插入图片描述

package net.wz.spring.lesson05.aop_annotation;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;



@Configuration // 标明是Spring配置类
@ComponentScan("net.wz.spring.lesson05.aop_annotation") // 组件扫描
@EnableAspectJAutoProxy // 开启Spring对ApectJ的支持
public class AopConfig {
}

7、在aop_annotation子包里创建测试类 - TestKnight

在这里插入图片描述

package net.wz.spring.lesson05.aop_annotation;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestKnight {
    private AnnotationConfigApplicationContext context;

    @Before
    public void init() {
        // 基于Spring配置类创建应用容器
        context = new AnnotationConfigApplicationContext(AopConfig.class);
    }

    @Test
    public void testBraveKnight() {
        // 根据名称从应用容器里获取勇敢骑士对象
        BraveKnight braveKnight = (BraveKnight) context.getBean("mike");
        // 勇敢骑士执行任务
        braveKnight.embarkOnQuest();
    }

    @After
    public void destroy() {
        // 关闭应用容器
        context.close();
    }
}

8、运行测试方法testBraveKnight(),查看效果

在这里插入图片描述

9、课堂练习

(1)增加拯救少女任务类与拯救少女骑士类

在这里插入图片描述

package net.wz.spring.lesson05.aop_annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 功能:拯救少女骑士类
 * 作者:吴钊
 * 日期:2021年03月29日
 */

@Component
public class DamselRescuingKnight {
    @Autowired
    private RescueDamselQuest rescueDamselQuest;

    public DamselRescuingKnight(RescueDamselQuest rescueDamselQuest) {
        this.rescueDamselQuest = rescueDamselQuest;
    }

    public void embarkOnQuest() {
        rescueDamselQuest.embark();
    }
}

package net.wz.spring.lesson05.aop_annotation;
import org.springframework.stereotype.Component;


@Component
public class RescueDamselQuest {
    public void embark()  {
        System.out.println("执行救美任务。");
    }
}

(2)在测试程序里增加对拯救少女骑士的测试方法 - testDamselRescuingKnight()

在这里插入图片描述

在这里插入图片描述

四、实现注解式拦截

(1)修改勇敢骑士类,给embarkOnQuest()添加自定义注解Action

package net.wz.spring.lesson05.aop_annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component("mike")
public class BraveKnight {
    @Autowired
    private SlayDragonQuest slayDragonQuest;

    @Action(name = "注解式拦截")
    public void embarkOnQuest() {
        slayDragonQuest.embark();
    }
}

(2)修改游吟诗人切面类 - MinstrelAspect

package net.wz.spring.lesson05.aop_annotation;

import org.aspectj.lang.JoinPoint;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


@Aspect // 声明切面
@Component // 声明为Spring管理的Bean
public class MinstrelAspect {
    // 注解声明切点
    @Pointcut("@annotation(net.wz.spring.lesson05.aop_annotation.Action)")
    public void embark() {
    }

    // 注解声明前置通知
    @Before("embark()")
    public void singBeforeQuest(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println(action.name() + "拦截了" + method.getName() + "方法。");
        System.out.println("啦啦啦,骑士出发了!");
    }

    // 注解声明后置通知
    @After("embark()")
    public void singAfterQuest(JoinPoint joinPoint) {
        System.out.println("真棒啊!骑士完成了任务!");
    }
}

(3)运行测试方法testBraveKnight(),查看效果

在这里插入图片描述

(4)修改拯救少女骑士类,给embarkOnQuest()添加自定义注解Action

package net.wz.spring.lesson05.aop_annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



@Component
public class DamselRescuingKnight {
    @Autowired
    private RescueDamselQuest rescueDamselQuest;

    public DamselRescuingKnight(RescueDamselQuest rescueDamselQuest) {
        this.rescueDamselQuest = rescueDamselQuest;
    }

    @Action( name = "注解式拦截")
    public void embarkOnQuest() {
        rescueDamselQuest.embark();
    }
}

(5)运行测试方法testDamselRescuingKnight(),查看效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值