背景:有时候代码执行,为了避免错误的发生,我们需要一些额外的措施。类中的某些方法譬如:银行类的转账方法执行前,可以先记录日志,如果转账失败收款方没有收到钱,我们就根据日志把钱返到付款方账户。在这里,记录日志就是必须要做的操作,银行类的其它方法执行前通常也需要记录日志。
记录日志其实可看作是对转账等方法的增强,事实上,我们就可以通过动态代理对方法增强实现这种功能。
spring的AOP正是基于动态代理来解决上述问题的。
工程
导包
使用spring的aop需要下面这个jar包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
业务层接口及其实现类
public interface IAccountService {
//模拟保存账户
void saveAccount();
//模拟更新账户
void updateAccount(int i);
//模拟删除账户
int deleteAccount();
}
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println("执行类保存");
}
public void updateAccount(int i) {
System.out.println("执行了更新"+i);
}
public int deleteAccount() {
System.out.println("执行了删除");
return 0;
}
}
通知类
我们想让每个方法执行之前都进行日志记录,我们需要新建一个工具类,它具有一个输出日志的方法。该类就是AOP中的通知类。
/**
* 用于记录日志的工具类,它里面提供类公共的代码
*/
public class Logger {
public void printLog(){
System.out.println("Logger类中的printLog方法开始记录日志了-----");
}
}
配置AOP(bean.xml)
首先配置AOP的版本信息,要有xmlns:aop,这和之前的不一样。
1、把通知Bean也交给spring来管理
2、使用aop:config标签表明开始AOP的配置
3、使用aop:aspect标签表明配置切面
- id属性:是给切面提供一个唯一标识
- ref属性:是指定通知类bean的Id。
4、在aop:aspect标签的内部使用对应标签来配置通知的类型我们现在示例是让printLog方法在切入点方法执行之前之前:所以是前置通知
- aop:before:表示配置前置通知
- method属性:用于指定Logger类中哪个方法是前置通知
- pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
<!-- 配置Logger类 -->
<bean id="logger" class="com.itheima.utils.Logger"></bean>
<!--配置AOP-->
<aop:config>
<!--配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!-- 配置通知的类型,并且建立通知方法和切入点方法的关联-->
<aop:before method="printLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:before>
</aop:aspect>
</aop:config>
</beans>
切入点表达式的写法:
关键字:execution(表达式)
表达式:
访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)标准的表达式写法:
public void com.itheima.service.impl.AccountServiceImpl.saveAccount()
测试类
public class AOPTest {
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
IAccountService as = (IAccountService)ac.getBean("accountService");
//3.执行方法
as.saveAccount();
as.updateAccount(1);
as.deleteAccount();
}
}
输出结果:
补充
通知类型:主要分为前置通知、后置通知、异常通知、最终通知、环绕通知
以下是对Bean.xml的更新
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
<!--配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!-- 配置前置通知:在切入点方法执行之前执行
<aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>-->
<!-- 配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个
<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->
<!-- 配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->
<!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行
<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>-->
<!-- 配置环绕通知 详细的注释请看Logger类中-->
<aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>
</aop:aspect>
</aop:config>
以下是对日志类的更新
public class Logger {
public void beforeprintLog(){
System.out.println("前置通知:Logger类中的beforeprintLog方法开始记录日志了-----");
}
public void afterReturningPrintLog(){
System.out.println("后置通知:Logger类中的afterReturningPrintLog方法开始记录日志了-----");
}
public void afterThrowingprintLog(){
System.out.println("异常通知:Logger类中的afterThrowingprintLog方法开始记录日志了-----");
}
public void afterprintLog(){
System.out.println("最终通知:Logger类中的afterprintLog方法开始记录日志了-----");
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。