栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring——AOP中五大通知功能的执行顺序(完全注解开发)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring——AOP中五大通知功能的执行顺序(完全注解开发)

1.前言

Spring大家肯定都用过的,IOC和AOP是它的两大核心,那么这篇文章主要和大家分享一下如果对一个方法声明了切面、添加了五种通知,它们的执行顺序是怎样的呢?

由于之前学Spring的时候写过太多了xml配置了,所以这篇文章采用完全注解开发的方式来实现。


2.项目源码

首先是pom文件,我们添加所需依赖。

    
      org.springframework
      spring-context
      5.2.5.RELEASE
    

    
      org.springframework
      spring-aspects
      5.2.5.RELEASE
    

    
      junit
      junit
      4.11
      test
    

接下来我们来一个业务接口以及它的实现类。

package com.szh.service;


public interface SomeService {

    String doSome(String name,Integer age);

}

由于我们不写xml配置,所以需要在业务接口实现类上添加 @Service 注解,通过注解将这个类交给Spring IOC容器管理。 

package com.szh.service.impl;

import com.szh.service.SomeService;
import org.springframework.stereotype.Service;

@Service
public class SomeServiceImpl implements SomeService {

    @Override
    public String doSome(String name, Integer age) {
        //int a = 10 / 0; //用于测试异常通知
        System.out.println("业务方法doSome(),创建商品的订单");
        return "姓名:" + name + ",年龄:" + age;
    }

}

然后是我们的切面类,同上面的业务实现类,也需要使用 @Component 注解将这个类交给Spring IOC容器管理。 

package com.szh.handle;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {

    @Before(value = "myPointCut()")
    public void myBefore() {
        System.out.println("前置通知,在目标方法之前先执行的....");
    }

    @AfterReturning(value = "myPointCut()", returning = "obj")
    public void myAfterReturning(Object obj) {
        System.out.println("后置通知,在目标方法之后再执行的(如有异常,则后置通知不会执行).... 目标方法返回值:" + obj);
    }

    @Around(value = "myPointCut()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知中前置通知的功能....");
        Object obj = joinPoint.proceed();
        System.out.println("环绕通知中后置通知的功能....");
        return obj;
    }

    @AfterThrowing(value = "myPointCut()", throwing = "e")
    public void myAfterThrowing(Exception e) {
        System.out.println("异常通知,在目标方法抛出异常时执行的,异常原因是:" + e.getMessage());
    }

    @After(value = "myPointCut()")
    public void myAfter() {
        System.out.println("最终通知,总是会被执行的....");
    }

    @Pointcut(value = "execution(* com.szh.service.impl.*.do*(..))")
    private void myPointCut() {
        //无需代码
    }
}

最后再来个配置类,在这个配置类中开启要替代xml配置文件的相关功能。

package com.szh.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;


@Configuration
//开启包扫描功能,相当于xml中的 
@ComponentScan(basePackages = "com.szh")
//开启aop自动代理功能,相当于xml中的 
@EnableAspectJAutoProxy
public class MyConfig {
}

最后是测试类。

package com.szh;

import com.szh.config.MyConfig;
import com.szh.service.SomeService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class MyTest {

    @Test
    public void testAnnotationAop() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        SomeService service = (SomeService) context.getBean("someServiceImpl");
        String info = service.doSome("张起灵", 20);
        System.out.println(info);
    }
}
2.1 无异常的执行结果

2.2 有异常的执行结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991376.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号