Spring框架学习笔记05

一、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;

/**
 * 功能:杀龙任务类
 * 作者:吴钊
 * 日期:2021年03月24日
 */
@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;

/**
 * 功能:勇敢骑士类
 * 作者:吴钊
 * 日期:2021年03月24日
 */
@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;

/**
 * 功能:游吟诗人类
 * 作者:吴钊
 * 日期:2021年03月24日
 */
@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">
            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值