Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Passing instance of Aspect into a pointcut

Hi Alex.

Your question is about Spring AOP rather than native AspectJ. Anyway, AFAIK you cannot inject a Spring bean into a static field, but of course you also cannot access an instance field from a static 'if()' method without some means to fetch a reference to the instance you wish to access. So if you think that an `if()` pointcut looks prettier than a condition at the beginning of an advice method, you are going to have to live with calling 'aspectOf()'.

Kind regards

--
Alexander Kriegisch
https://scrum-master.de
 

Alex Rojkov schrieb am 06.09.2022 05:11 (GMT +02:00):

I have an Aspect that defines @Around advice.
 
@Aspect 
class FooAspect {
  @spring.Autowired Env env; 
 
  @Around(some()) 
  public Object advice(String name) {
     return env.get(name);
  }
}
 
I'd like this Aspect to execute when env is not null and delegate to JoinPoint when it is null. 
 
@Around(some())
public Object advice(String name, ProceedingJointPoint pjp) {
   if (env != null) {
        return env.get(name);
    else 
        return pjp.proceed();
}
 
However, I'd like to avoid cluttering the advice and do something like this instead.
 
@Pointcut("if()") 
public static boolean isActive(FooAspect aspect) {
   return aspect.env != null;
}
 
 @Around(isActive() && some()) 
  public Object advice(String name, ProceedingJointPoint pjp) {
     return env.get(name);
  }
 
Question: is there a way to pass the instance of the advice to the isActive() pointcut? 
 
I'd like to avoid using Aspects.aspectOf() to get hold of the instance. 
 
Thanks,
Alex
 
 
 

Back to the top