大二下-企业级信息系统设计与开发笔记08(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.zl包里创建lesson05.aop_xml子包

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

package net.zl.lesson05.aop_xml;

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

/**
 * 功能:勇敢骑士类
 * 作者:周璐
 * 日期:2021年03月24日
 */
@Component
public class BraveKnight {
   
    @Autowired
    private SlayDragonQuest slayDragonQuest;

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

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

在这里插入图片描述

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.zl.lesson05.aop_xml" />

    <!--AOP配置-->
    <aop:config>
        <!--定义切面-->
        <aop:aspect ref="minstrel">
            <!--定义切点-->
            <aop:pointcut id="embark" expression="execution(* net.zl.lesson05..  *.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.zl.lesson05… .(…))

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

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

6、创建测试类- TestKnight

在这里插入图片描述

package net.zl.spring.lesson05.aop_xml;

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

/**
 * 功能:测试骑士类
 * 作者:周璐
 * 日期:2021年03月24日
 */
public class TestKnight {
   
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值