springboot实现aop环绕通知

1、依赖,引入此依赖后,springboot会自动开启自动代理,相当于@EnableAspectJAutoProxy,故不需要再手动添加这个注解。

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
   </dependency>

2、创建切面类 (声明切面类:Aspect。交由ioc管理:Component)return的proceed为方法执行后的返回数据。


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {

    @Around("execution(* com.ljs.springbootplace.service.impl.MyServiceImpl.getUserNameById(..))")
    public Object roundAsp(ProceedingJoinPoint pj){
        System.out.println("前");

        Object proceed = null;
        try {
            Object[] args = pj.getArgs();
            System.out.println("原参数为:"+args[0]);
            args[0]=1;
            System.out.println("修改后入参参数为:"+ pj.getArgs()[0]);
            //如果需要重新设置参数,则调用proceed的有参方法,否则直接调用无参方法即可
            proceed = pj.proceed(args);
        } catch (Throwable throwable) {
            System.out.println("异常");
            throwable.printStackTrace();
        }

        System.out.println("后");

        return proceed;
    }
}

3、使用springboot单元测试进行测试

import com.ljs.springbootplace.service.MyService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringbootplaceApplication.class)
public class SpringbootplaceApplicationTests {

    @Autowired
    private MyService myService;
    
    @Test
    public void test() {
        String userNameById = myService.getUserNameById_testAspect(1);//测试aop
        System.out.println(userNameById);
    }

}

4、测试结果:(原参数1对应返回“yang”,修改为2后,返回“wang”)

注:如需要获取切面执行的方法的名称,类,包等数据,可以参考:环绕通知中,获取执行的方法、所属类名、包名等数据的方式

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,使用环绕通知可以实现对方法的拦截和增强。环绕通知AOP的一种类型,它可以在目标方法的执行前后进行额外的操作。 要使用环绕通知,首先需要在Spring Boot项目中引入`spring-boot-starter-aop`依赖。然后,定义一个切面类,并在该类上使用`@Aspect`注解进行标记。 接下来,在切面类中定义一个环绕通知方法,使用`@Around`注解进行标记。环绕通知方法的定义如下: ```java @Aspect @Component public class MyAspect { @Around("execution(* com.example.demo.service.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { // 在方法执行之前做一些操作 System.out.println("Before method execution"); // 执行目标方法 Object result = joinPoint.proceed(); // 在方法执行之后做一些操作 System.out.println("After method execution"); return result; } } ``` 在上述代码中,`@Around`注解的参数是一个切点表达式,用于指定需要拦截的方法。在`aroundAdvice`方法中,我们可以在目标方法执行之前和之后执行自定义的操作。 需要注意的是,环绕通知方法的参数类型是`ProceedingJoinPoint`,它提供了对目标方法的访问和控制。通过调用`joinPoint.proceed()`方法,可以执行目标方法。 最后,确保切面类被Spring Boot扫描到,可以使用`@ComponentScan`注解或在启动类上使用`@SpringBootApplication`注解。这样,切面类就会被自动装配到Spring Boot应用中。 希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值