1.导入aspectjweaver是spring的切入点表达式需要用的包
<dependency>
<!--spring 包-->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<!--aspectjweaver是spring的切入点表达式需要用的包-->
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
2.创建业务逻辑层接口类和实现类,的增,删,改方法
3.写通知类中的具体方法 :Logger
//记录日志的工具类(也叫通知类),它里面提供了公共的代码
public class Logger {
public void beforePrintlog(){
//用于打印日志:计划让其在切入点方法执行前执行,也就是前置通知
//切入点方法就是业务层的方法
System.out.println("logger类中 前置通知 方法开始记录日志了。。。");
}
public void afterReturningPrintlog(){
//后置通知
System.out.println("logger类中 后置通知 方法开始记录日志了。。。");
}
public void afterThrowingPrintlog(){
//异常通知
System.out.println("logger类中 异常通知 方法开始记录日志了。。。");
}
public void afterPrintlog(){
//最终通知
System.out.println("logger类中 最终通知 方法开始记录日志了。。。");
}
}
4.配置通知,IOC,AOC,bean.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--自己理解后的简单解释:
1.accountService 类(账户)想增强,他会的技能,并且要使用技能的时候,
都能有人监督并给出通知和建议(生成通知)。
2.就请了个可以 监督的通知类,它有通知的技能
3.通知就告诉账户,在监督账户使用技能的时候,
通知会在,账户使用具体某个技能的时候准备, 前置,后置,异常,最终
的一些相关的摄像机在进行监督。
-->
<!--配置spring的IOC ,把service 对象配置进来-->
<bean id = "accountService" class="wuwei.service.impl.AccountService"></bean>
<!--spring中 基于xml ,AOP配置步骤
1.把通知Bean也交给spring来管理
2.使用aop:config标签表明开始AOP配置
3.使用aop:aspect标签表明配置切面
id属性:是给切面提供一个唯一标识
ref属性:是指通知类bean的id
4.在aop:aspect标签的内部使用对应的标签来配置通知的类型
我们现在示例是让pringLog方法在切入点方法执行前,所以是前置通知。
aop:before:表示配置前置通知
method属性:用于指定Logger类中哪个方法是前置通知
pointcut属性:用于指定切入点表达式,该表达式的含义是指对业务层