首页 > 其他分享 >spring aop记录

spring aop记录

时间:2024-10-01 12:49:32浏览次数:6  
标签:joinPoint 记录 spring aop System org import public out

 

使用:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("Before executing: " + joinPoint.getSignature());
    }

    @After("execution(* com.example.service.*.*(..))")
    public void logAfter(JoinPoint joinPoint) {
        System.out.println("After executing: " + joinPoint.getSignature());
    }
}
  • execution(* com.example.service.*.*(..)):匹配 com.example.service 包下的所有类的所有方法。
  • execution(public * *(..)):匹配所有公共方法。

 

import org.springframework.stereotype.Service;

@Service
public class UserService {

    public void addUser(String name) {
        System.out.println("Adding user: " + name);
    }

    public void deleteUser(String name) {
        System.out.println("Deleting user: " + name);
    }
}

 

 

 

aop实现原理即是动态代理技术

 

标签:joinPoint,记录,spring,aop,System,org,import,public,out
From: https://www.cnblogs.com/towboa/p/18442822

相关文章