小编给大家分享一下关于Spring中AOP的案例分析,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!
切面:Aspect
切面=切入点+通知。在老的spring版本中通常用xml配置,现在通常是一个类带上@Aspect注解。切面负责将 横切逻辑(通知) 编织 到指定的连接点中。
目标对象:Target
将要被增强的对象。
连接点:JoinPoint
可以被拦截到的程序执行点,在spring中就是类中的方法。
切入点:PointCut
需要执行拦截的方法,也就是具体实施了横切逻辑的方法。切入点的规则在spring中通过AspectJ pointcut expression language来描述。
切入点与连接点的区别:连接点是所有可以被"切"的点;切入点是真正要切的点。
通知:Advice
针对切入点的横切逻辑,包含“around”、“before”和“after”等不同类型的通知。
通知的作用点如其命名:
还有一些异常处理的通知,这里不一一举例
织入:Weaving
将切面和目标对象连接起来,创建代理对象的过程。spring中用的是动态代理。假如目标对象有接口,使用jdk动态代理;否则使用cglib动态代理。
说了这么多概念,看看代码实现可能会使读者理解的更深刻一些,这里简单写一个通过注解增强方法的AOP-Demo。
首先是切面类:
package com.example.demo.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @author Fcb
* @date 2020/6/20
* @description 切面类=切入点+通知
*/
@Aspect
@Component
public class LogAspect {
//这个方法定义了切入点
@Pointcut("@annotation(com.example.demo.aop.anno.MyLog)")
public void pointCut() {}
//这个方法定义了具体的通知
@After("pointCut()")
public void recordRequestParam(JoinPoint joinPoint) {
for (Object s : joinPoint.getArgs()) {
//打印所有参数,实际中就是记录日志了
System.out.println("after advice : " + s);
}
}
//这个方法定义了具体的通知
@Before("pointCut()")
public void startRecord(JoinPoint joinPoint) {
for (Object s : joinPoint.getArgs()) {
//打印所有参数
System.out.println("before advice : " + s);
}
}
//这个方法定义了具体的通知
@Around("pointCut()")
public Object aroundRecord(ProceedingJoinPoint pjp) throws Throwable {
for (Object s : pjp.getArgs()) {
//打印所有参数
System.out.println("around advice : " + s);
}
return pjp.proceed();
}
}
注解:
package com.example.demo.aop.anno;
import java.lang.annotation.*;
/**
* @author Fcb
* @date 2020/6/20
* @description
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyLog {
}
目标类:
package com.example.demo.aop.target;
import com.example.demo.aop.anno.MyLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Fcb
* @date 2020/6/20
* @description
*/
@RestController
public class MockController {
@RequestMapping("/hello")
@MyLog
public String helloAop(@RequestParam String key) {
System.out.println("do something...");
return "hello world";
}
}
最后是测试类:
package com.example.demo.aop.target;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author Fcb
* @date 2020/6/20
* @description
*/
@SpringBootTest
class MockControllerTest {
@Autowired
MockController mockController;
@Test
void helloAop() {
mockController.helloAop("aop");
}
}
控制台结果:
around advice : aop
before advice : aop
do something...
after advice : aop
看完了这篇文章,相信你对关于Spring中AOP的案例分析有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。