针对于SpringBoot 2.0 来讲,AOP 的概念 过于抽象,对初学者不是很友好,并且在实际项目中应用
也是很少,毕竟AOP 的性能很是低下,同样类似于声明式事务,在大型 的项目中,使用spring 的编程式 事务会很多,毕竟性能;
好了,继上一篇blog 入门讲解spring 的AOP 也有一段时间了,现在总结下AOP 的使用以及深入理解,当然,本Blog 不是在讲 AOP 的源码,而是,在简单 demo 中 使用AOP ,一个字,照着敲,敲一遍有点概念,敲两遍基本知道怎么个流程了,绝不是赏花似的 瞄几眼能够比拟的。
日常源码分享:
https://github.com/medoo-Ai/springboot
AOP 实现原理 以及AOP概念可以参考 我的上一篇blog
https://blog.youkuaiyun.com/weixin_42323802/article/details/82468900
1.1、基础创建maven 等就不累述,创建启动类AopApp.java
首先定义一个简单的HelloWorldService ,并创建实现类
public interface HelloService {
void sayHello(String name);
}
@Slf4j
public class HelloServiceImpl implements HelloService {
public void sayHello(String name) {
if (name == null || name.trim() == "") {
log.info("name 不能为空 {}",name);
}
System.out.println(name);
}
}
1.2、自定义拦截器 MyInterceptor.java
可以参考我的这篇 blog
https://blog.youkuaiyun.com/weixin_42323802/article/details/83656729
这里简单实现一个自定义的interceptor
@Slf4j
public class MyInterceptor implements Interceptor {
public boolean before() {
log.info(<