spring aop切面编程@Aspect、@Around注解方式配置

1. 服务类功能代码

package com.demo.service;

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements HelloInterface {

    public void sayHello() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("userHello");
    }
    
}  

目标类需要在Spring Context管理。 

2. 切面类

package com.demo.aop;

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

@Service
@Aspect
public class TimeMonitor {    
    
    @Around("execution(* com.demo.service.UserServiceImpl.sayHello(..))")
    public void monitorAround(ProceedingJoinPoint pjp) throws Throwable {

        System.out.println("method start time:" + System.currentTimeMillis());

        Object re = pjp.proceed();

        System.out.println("method end time:" + System.currentTimeMillis());

    }
}
  • 切面类有两个注释,分别是@Service和@Aspect,第一个注解是使得TimeMonitor类受Spring托管并实例化。@Aspect就是使得这个类具有AOP功能两个注解缺一不可。
  • 类里面只有一个方法,名字叫做monitorAroud,其实就是为了检测函数执行时间的!
  • @Around表示使用的是环切,执行方法调用前,执行该切面方法,通过pjp.proceed();调用目标方法,目标方法执行完成后,继续执行后续方法。完整的处理,还可以增加异常处理。

3. 切面依赖

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

4. 调用方法可以获得目标方法的执行时间

method start time:1480223298250 
method end time:1480223299250

通过切面,可以方便地对方法执行时间、调用日志、异常处理等进行统一处理。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AOP(面向切面编程)中,注解方式是一种常见的实现方式,可以用于声明切面和通知。在Spring框架中使用注解方式实现AOP可以更加简洁和便捷。 要使用AOP注解方式,首先需要在Spring配置文件中启用AspectJ自动代理,通过在配置文件中添加`<aop:aspectj-autoproxy></aop:aspectj-autoproxy>`来实现。 接下来,可以使用以下注解来实现AOP切面编程: 1. `@Aspect`:用于声明一个类为切面类。 2. `@Pointcut`:用于声明一个切入点,切入点定义了何时何地应该执行切面的通知方法。 3. `@Before`:在目标方法执行之前执行通知。 4. `@After`:在目标方法执行之后(无论是否发生异常)执行通知。 5. `@AfterReturning`:在目标方法执行后返回结果时执行通知。 6. `@AfterThrowing`:在目标方法抛出异常时执行通知。 7. `@Around`:在目标方法执行前后执行通知。 下面是一个使用注解方式实现AOP的示例代码: ```java @Aspect public class LoggingAspect { @Pointcut("execution(* com.example.service.*.*(..))") private void serviceMethods() {} @Before("serviceMethods()") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before advice: " + joinPoint.getSignature().getName()); } @After("serviceMethods()") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After advice: " + joinPoint.getSignature().getName()); } @AfterReturning(pointcut = "serviceMethods()", returning = "result") public void afterReturningAdvice(JoinPoint joinPoint, Object result) { System.out.println("After returning advice: " + joinPoint.getSignature().getName() + ", result: " + result); } @AfterThrowing(pointcut = "serviceMethods()", throwing = "exception") public void afterThrowingAdvice(JoinPoint joinPoint, Exception exception) { System.out.println("After throwing advice: " + joinPoint.getSignature().getName() + ", exception: " + exception.getMessage()); } @Around("serviceMethods()") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Before around advice: " + joinPoint.getSignature().getName()); Object result = joinPoint.proceed(); System.out.println("After around advice: " + joinPoint.getSignature().getName()); return result; } } ``` 在上面的示例中,`LoggingAspect`类使用了`@Aspect`注解声明为切面类,然后通过`@Pointcut`注解定义了一个切入点`serviceMethods()`,它匹配了`com.example.service`包下的所有方法。 接着,使用各种不同的注解来实现不同类型的通知,比如`@Before`、`@After`、`@AfterReturning`、`@AfterThrowing`和`@Around`,并在这些通知方法中实现相应的逻辑。 以上就是使用注解方式实现AOP切面编程的简要介绍。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值