一、采用配置使用AOP
1、创建杀龙任务类与勇敢骑士类
- BraveKnight
- SlayDragonQuest
2、创建游吟诗人类 - Minstrel
package net.zxj.spring.lesson5.aop_xml;
import org.springframework.stereotype.Component;
@Component
public class Minstrel {
public void singBeforeQuest() {
System.out.println("啦啦啦,骑士出发了!");
}
public void singAfterQuest() {
System.out.println("真棒啊!骑士完成了任务!");
}
}
创建Spring配置文件
<?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.zxj.spring.lesson5.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>
execution()是最常用的切点函数,整个表达式可以分为五个部分。
execution():表达式主体。
- 第一个*号:表示返回类型,*号表示所有的类型。
- 包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,net.zxj.spring包、子孙包下所有类的方法。
- 第二个*号:表示类名,*号表示所有的类。
- *(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。
3、创建测试类- TestKnight
package net.zxj.spring.lesson5.aop_xml;
import net.zxj.spring.lesson5.aop_xml.BraveKnight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 功能:测试骑士类
* 作者:华卫
* 日期:2020年09月29日
*/
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();
}
}
- 在pom.xml文件里添加AOP相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.4</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>
- 运行结果:
二、扩展练习
1、增加拯救少女任务类与拯救少女骑士类
2、在测试方法里增加对拯救少女骑士的测试方法 - testDamselRescuingKnight()
- 运行testDamselRescuingKnight()
三、采用注解方式使用AOP
(1) 创建 aop_annotation包并复制aop_xml的类进去
(2) 在aop_annotation子包里创建注解接口 - Action
(3) 修改游吟诗人切面类 - MinstrelAspect
package net.zxj.spring.lesson5.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;
/**
* 功能:游吟诗人切面
* 作者:华卫
* 日期:2020年09月29日
*/
@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("真棒啊!骑士完成了任务!");
}
}
(4) 创建Spring配置类 - AopConfig
package net.zxj.spring.lesson5.aop_annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration // 标明是Spring配置类
@ComponentScan("net.zxj.spring.lesson5.aop_annotation") // 组件扫描
@EnableAspectJAutoProxy // 开启Spring对ApectJ的支持
public class AopConfig {
}
(5) 在aop_annotation子包里创建测试类 - TestKnight
- 运行testBraveKnight():