搜索 K
Appearance
博客正在加载中...
Appearance
现在我们使用注解来改造上一篇博客的案例
修改 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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>主要是增加了第 5,10,11 行
然后我们配置扫描的包,以及开启开启 AOP 的配置:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置Spring创建容器时要扫描的包 -->
<context:component-scan base-package="com.peterjxl"/>
<!-- 配置Spring开启注解AOP的支持 -->
<aop:aspectj-autoproxy/>
</beans>然后我们删除关于 service 和 logger 的 bean 配置。
然后是在类上配置注解:
@Service("accountService")
public class AccountServiceImpl implements IAccountService {然后我们在 logger 类中配置
package com.peterjxl.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* 用于记录日志的工具类,它里面提供了公共的代码
*/
@Component("logger")
@Aspect //表示当前类是一个切面类
public class Logger {
@Pointcut("execution(* com.peterjxl.service.impl.*.*(..))")
private void pt1() {}
/**
* 用于打印日志,计划让其在切入点方法执行之前执行(切入点方法就是业务层方法)
*/
public void printLog() {
System.out.println("Logger类中的printLog方法开始记录日志了。。。");
}
/**
* 前置通知
*/
@Before("pt1()")
public void beforePrintLog() {
System.out.println("前置通知 Logger类中的beforePrintLog方法开始记录日志了。。。");
}
/**
* 后置通知
*/
@AfterReturning("pt1()")
public void afterReturningPrintLog() {
System.out.println("后置通知 Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
}
/**
* 异常通知
*/
@AfterThrowing("pt1()")
public void afterThrowingPrintLog() {
System.out.println("异常通知 Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
}
/**
* 最终通知
*/
@After("pt1()")
public void afterPrintLog() {
System.out.println("最终通知 Logger类中的afterPrintLog方法开始记录日志了。。。");
}
/**
* 环绕通知
*/
// @Around("pt1()")
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try{
Object[] args = pjp.getArgs();//得到方法执行所需的参数
System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");
rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");
return rtValue;
}catch (Throwable t){
System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
throw new RuntimeException(t);
}finally {
System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
}
}
}
注意,我们先测试其他类型的通知,环绕类型的注解已经注释掉了,因为不能即有其他类型的通知,又有环绕通知,会冲突。
此时我们测试下 saveAccount 方法,发现运行结果是这样的:
前置通知 Logger类中的beforePrintLog方法开始记录日志了。。。
执行了保存
最终通知 Logger类中的afterPrintLog方法开始记录日志了。。。
后置通知 Logger类中的afterReturningPrintLog方法开始记录日志了。。。可以看到先调用了最终通知,然后才是后置通知,这是 Spring 的机制问题。所以我们需要慎重选择是否要用注解。而如果我们自己写环绕通知的话,就没有这个问题
注释掉其他通知类型的注解,然后取消注释环绕通知的注解,运行结果:
Logger类中的aroundPringLog方法开始记录日志了。。。前置
执行了保存
Logger类中的aroundPringLog方法开始记录日志了。。。后置
Logger类中的aroundPringLog方法开始记录日志了。。。最终所以用注解的话,推荐用环绕通知
除此之外,我们也可以不用配置文件,只需使用 @EnableAspectJAutoProxy 即可:
package com.peterjxl.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.peterjxl")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}本项目已将源码上传到 GitHub 和 Gitee 上。并且创建了分支 demo12,读者可以通过切换分支来查看本文的示例代码。