一、采用配置方式使用AOP
1.1 创建所需子包
在cn.kox.spring包里创建day05.aop_xml子包
1.2 创建杀龙任务类
在aop_xml子包里创建杀龙任务类 - SlayDragonQuest
package cn.kox.spring.day05.aop_xml;
import org.springframework.stereotype.Component;
/*
1.4 创建游吟诗人类
在aop_xml子包里创建游吟诗人类 - Minstrel
package cn.kox.spring.day05.aop_xml;
import org.springframework.stereotype.Component;
@Component
public class Minstrel {
/**
* 骑士出发前唱赞歌
*/
public void singBeforeQuest() {
System.out.println(“啦啦啦,骑士出发了~”);
}
/**
* 骑士凯旋时唱赞歌
*/
public void singAfterQuest() {
System.out.println("真棒啊!骑士完成了任务~");
}
}
1.5 创建Spring配置文件
在resources里创建aop_xml目录,在里面创建spring-config.xml配置文件
1.6 添加AOP相关依赖
在pom.xml文件里添加AOP相关依赖
org.springframework
spring-aop
5.3.25
aspectj
aspectjrt
1.5.4
org.aspectj
aspectjweaver
1.9.19
runtime
1.7 创建测试类 - TestKnight
在test/java里创建cn.kox.spring.day05.aop_xml包,在包里创建TestKnight类
package cn.kox.spring.day05.aop_xml;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestKnight {
private ClassPathXmlApplicationContext context; // 基于类路径XML配置文件的应用容器
@Before
public void init() {
// 基于Spring配置文件创建应用容器
context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");
}
@Test
public void testBraveKnight() {
// 根据名称从应用容器里获取勇敢骑士对象
BraveKnight braveKnight = (BraveKnight) context.getBean("RobinHood");
// 勇敢骑士执行任务
braveKnight.embarkOnQuest();
}
@After
public void destroy() {
// 关闭应用容器
context.close();
}
}
1.8 运行测试方法,查看结果
运行testBraveKinght()方法
1.9 练习
1.9.1 创建救美任务类与救美骑士类
创建RescueDamselQuest类和RescueDamselKnight类
RescueDamselQuest类
package cn.kox.spring.day05.aop_xml;
import org.springframework.stereotype.Component;
/*
RescueDamselKnight类
package cn.kox.spring.day05.aop_xml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*
救美骑士类
*/
@Component(“Galahad”)
public class RescueDamselKnight {
@Value(“格拉海德”)
private String name;
@Autowired
private RescueDamselQuest rescueDamselQuest;
public void embarkOnQuest() {
System.out.print(“救美骑士[” + name + “]”);
rescueDamselQuest.embark();
}
}
1.9.2 在测试类里增加测试方法
测试方法 - testRescueDamselKnight()
package cn.kox.spring.day05.aop_xml;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestKnight {
private ClassPathXmlApplicationContext context; // 基于类路径XML配置文件的应用容器
@Before
public void init() {
// 基于Spring配置文件创建应用容器
context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");
}
@Test
public void testBraveKnight() {
// 根据名称从应用容器里获取勇敢骑士对象
BraveKnight braveKnight = (BraveKnight) context.getBean("RobinHood");
// 勇敢骑士执行任务
braveKnight.embarkOnQuest();
}
@Test
public void testRescueDamselKnight() {
RescueDamselKnight rescueDamselKnight = (RescueDamselKnight) context.getBean("Galahad");
rescueDamselKnight.embarkOnQuest();
}
@After
public void destroy() {
// 关闭应用容器
context.close();
}
}
1.9.3 运行测试类,查看结果