文章目录
- 一、Spring AOP概述
- 二、采用配置方式使用AOP
- 三、采用注解方式使用AOP
-
- 1、在net.zl包里创建lesson05.aop_annotation子包
- 2、在aop_annotation子包里创建杀龙任务类 - SlayDragonQuest
- 3、在aop_annotation子包里创建勇敢骑士类 - BraveKnight
- 4、在aop_annotation子包里创建注解接口 - Action
- 5、在aop_annotation子包里创建游吟诗人切面 - MinstrelAspect
- 6、在aop_annotation子包里创建Spring配置类 - AopConfig
- 7、在aop_annotation子包里创建测试类 - TestKnight
- 8、运行测试方法testBraveKnight(),查看效果
- 9、课堂练习
- 10、实现注解式拦截
- 四,课堂练习
一、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 {