0

Via Reflection:

Class c = Class.forName("mypackage.DiodeService");
Method m = c.getDeclaredMethod("blink");
Object t = c.newInstance();
Object o = m.invoke(t);

Method from DiodeService is invoked:

@ValueGreaterThan
public void blink(){
    log.info("Diode service: blink()");
}

On the annotation @ValueGreaterThan is listening aspect:

@Around(value = "mypackage.CommonJoinPointConfig.valueGreaterThanAspect())") public void around(ProceedingJoinPoint joinPoint) throws Throwable {
    log.info("Value greater than aspect {}", joinPoint); }

BUT: The around advice is never intercepted, when calling method by "invoke".

Around advice is intercepted normally, when calling by diodeService.blink()

Please, any ideas?

svacijan
  • 3
  • 3

1 Answers1

2

The Spring AOP is based on runtime weaving due to the proxy-based nature of the Spring framework. This means the target bean is a Spring-managed bean. The bean is turned into a proxy during Spring runtime.

In the case where the DiodeService is a Spring proxy (managed by Spring), the Spring AOP will work normally as intended, i.e., the advice will be intercepted for any calls to blink.

The advice around blink will never be intercepted if DiodeService is instantiated directly and not via Spring. This is the case when you are instantiating DiodeService by calling newInstance,

Object t = c.newInstance()

Indra Basak
  • 7,124
  • 1
  • 26
  • 45