1.aop execution 所有dao层下面的包,对应表达式:

execution(* com.yourcompany.yourproject.dao..*.*(..))

import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.aspectj.lang.JoinPoint;
 
@Aspect
@Component
public class DaoAspect {
private static final Logger logger = LoggerFactory.getLogger(DaoAspect.class);
 
@Pointcut("execution(* com.yourcompany.yourproject.dao..*.*(..))")
public void daoPackage() {}
 
@Before("daoPackage()")
public void logBefore(JoinPoint joinPoint) {
logger.info("Before method: {}", joinPoint.getSignature().toShortString());
}
}
2.aop execution 所有dao层下面的包所有类中find方法,对应表达式:
execution(* com.yourpackage.dao..*.find*(..))
 
@Aspect
@Component
public class FindMethodLoggingAspect {
 
// 定义切点,匹配所有DAO层包下的类的find方法
@Pointcut("execution(* com.yourpackage.dao..*.find*(..))")
public void findMethodExecution() {}
 
// 定义前置通知,例如记录日志
@Before("findMethodExecution()")
public void beforeFindMethod(JoinPoint joinPoint) {
System.out.println("Before find method: " + joinPoint.getSignature().getName());
}
 
// 定义后置通知,例如记录日志或性能监控等
@After("findMethodExecution()")
public void afterFindMethod(JoinPoint joinPoint) {
System.out.println("After find method: " + joinPoint.getSignature().getName());
}
}