我有一段代码是
@Aspect
public class PointcutDefinition {
@Pointcut("within(com.byteslounge.web..*)")
public void webLayer() {
}
@Pointcut("within(com.byteslounge.service..*)")
public void serviceLayer() {
}
@Pointcut("within(com.byteslounge.dao..*)")
public void dataAccessLayer() {
}
}
和LoggingAspect.java:
@Aspect
public class AccountLoggingAspect {
@Before(value = "com.byteslounge.spring.aop.PointcutDefinition.serviceLayer() && "
+ "args(account,..)")
public void beforeAccountMethodExecution(JoinPoint jp, Account account) {
System.out.println("Before method: " + jp.getSignature().getName() + ". Class: " + jp.getTarget().getClass().getSimpleName());
System.out.println("Logging account access. Account: " + account.getAccountNumber());
}
}
和以下服务类别:
@Service
public class ExampleService {
@Auditable(AuditDestination.DATABASE)
public void updateAccountBalance(Account account) {
System.out.println("Inside updateAccountBalance(). Account: " + account.getAccountNumber() );
}
}
beforeAccountMethodExecution()
在updateAccountBalance(Account account)
之前执行。但是我希望仅当我从特定程序包而不是从其他任何地方调用updateAccountBalance(Account account)
时才进行此方法拦截调用。简单来说,我希望aop仅在从“ xyz”包中调用updateAccountBalance
时起作用。有出路吗?