IT评测·应用市场-qidao123.com技术社区

标题: Spring AOP [打印本页]

作者: 河曲智叟    时间: 2024-11-9 18:41
标题: Spring AOP
入门

Spring AOP快速入门:统计各个业务层方法执行耗时
概述
一样平常我们都会想到在方法开始竣事定义一个执行毫秒值计算总耗时,但是这种方法比较繁琐,以是AOP能大大的提高该效率

思路

实现

xml文件

导入依赖:在pomxml中导入AOP的依赖
  1. <dependency>
  2.            <groupId>org.springframework.boot</groupId>
  3.            <artifactId>spring-boot-starter-aop</artifactId>
  4. </dependency>
复制代码
aop类

编写AOP步伐:针对于特定方法根据业务须要进行编程
  1. @Component
  2. @Slf4j
  3. @Aspect // AOP类
  4. public class TimeAspect {
  5.    @Around("execution(* com.itheima.service.*.*(..))") // 切入点表达式
  6.    public Object aopDemp(ProceedingJoinPoint point) throws Throwable {
  7.        long begin = System.currentTimeMillis();
  8.        //调用原始方法运行
  9.        Object result = point.proceed();
  10.        long end = System.currentTimeMillis();
  11.        log.info(point.getSignature() +"方法执行耗时:{}ms",end - begin);
  12.        return result;
  13.    }
  14. }
复制代码
使用场景

aop的使用场景远不止如此
比方


记录操纵日记 ---> 权限控制 ---> 事务管理 .........

代码无侵入 ---> 减少重复代码 ---> 提高开发效率 ---> 维护方便
AOP核心概念



AOP关照范例

实现

  1. @Component
  2. @Slf4j
  3. @Aspect //AOP类
  4. public class MyAspect1 {
  5.    @Before(" execution(* com.itheima.service.impl.DeptServiceImpl.*(..)) ") //切入点
  6.    public void before(){
  7.        log.info("前置通知");
  8.    }
  9.    @Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
  10.    public Object Around(ProceedingJoinPoint point) throws Throwable {
  11.        Object obj = point.proceed();
  12.        log.info("循环通知");
  13.        return obj;
  14.    }
  15.    @After("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
  16.    public void After(){
  17.        log.info("后置通知");
  18.    }
  19.    @AfterReturning("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
  20.    public void AfterReturning(){
  21.        log.info("AfterReturning");
  22.    }
  23.    @AfterThrowing("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
  24.    public void AfterThrowing(){
  25.        log.info("AfterThrowing");
  26.    }
  27. }
复制代码
注意事项


@PointCut



关照顺序

当有多个切面的切入点给都匹配到了目标方法,目标方法运行时,多个关照方法都会被执行
执行顺序

1.差别切面类中,默认按照切面的类名字母排序:

2.用@Order(数字)加在切面类上来控制顺序

切入点表达式

   
  1. @Aspect
  2. @Slf4j
  3. @Component
  4. public class MyAspect6 {
  5.    @Pointcut("execution(public void com.itheima.service.impl.DeptServiceImpl.delete())")
  6.    public void pt(){}
  7.    @Before("pt()")
  8.   public void before(){
  9.        System.out.println("前置通知asdf");
  10.    }
  11. }
复制代码
execution

execution重要根据方法的返回值、包名、类名、方法名、方法参数等信息来匹配,语法为:
  1. execution(访问修饰符?返回值 包名.类名.?方法名(方法参数) throws 异常?)
复制代码

  1. @Pointcut("execution(public void com.itheima.service.impl.DeptServiceImpl.delete())")
  2.   public void before(){
  3.        System.out.println("前置通知asdf");
  4.    }
复制代码


   根据业务须要,可以使用且(&&)、或(||)、非(!)来组合比较复杂的切入点表达式
  @annotation


实现

MyAspect7
  1. public class MyAspect7 {
  2.    @Pointcut("@annotation(com.itheima.aop.tips)")
  3.    public void pt(){}
  4.    @Before("pt()")
  5.    public void before(){
  6.        System.out.println("前置通知asdf");
  7.    }
  8. }
复制代码
tips
创建一个注解
  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.METHOD)
  3. public @interface tips {
  4. }
复制代码
DeptServiceImpl
在该方法下加上本身创建的注解
  1. @Slf4j
  2. @Service
  3. public class DeptServiceImpl implements DeptService {
  4.    @Autowired
  5.    private DeptMapper deptMapper;
  6.    @Override
  7.    @tips
  8.    public List<Dept> list() {
  9.        List<Dept> deptList = deptMapper.list();
  10.        return deptList;
  11.    }
  12.    @tips
  13.    @Override
  14.    public void delete(Integer id) {
  15.        //1. 删除部门
  16.        deptMapper.delete(id);
  17.    }
  18. }
复制代码
连接点



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4