3

I'm implementing an AOP-based caching layer similar to Spring Cache, and I'm having trouble getting my advice to execute when the joinpoint is called by another method in its own class. I was initially using Spring AOP's AspectJAutoProxy, and I understand why that doesn't allow for this use case, so I tried switching to AspectJ's load-time weaver, but it doesn't appear to have done anything. Here are all the details:

Spring version: 4.5.2

AspectJ version: 1.8.9

Java agent(s) added to run command. Note that I've tried including each of the two agents separately and together, without any difference in behavior:

-javaagent:/var/app/cops/jars/aspectjweaver-1.8.9.jar -javaagent:/var/app/cops/jars/spring-instrument-4.3.0.RELEASE.jar

Application entrypoint:

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class Application implements ApplicationContextAware {
    @Bean
    public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
        InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
        return loadTimeWeaver;
    }

   // Other unrelated beans
}

Advice signature:

@Around("@annotation(cacheable)")
public Object processCacheable(ProceedingJoinPoint joinPoint, Cacheable cacheable) throws Throwable

Method signature:

@Cacheable(key = "'test-key'")
public Map<String, Object> getDataFromSource()

"processCacheable" gets executed before "getDataFromSource" is called by another class, but still not if called from within the same class. Is there some configuration I'm missing in order to get LTW working correctly?

cb4
  • 6,689
  • 7
  • 45
  • 57
Jared
  • 2,043
  • 5
  • 33
  • 63

1 Answers1

1

My usual disclaimer: I am not a Spring user. So I have next to zero experience with Spring configuration. But having looked at the weaving agent you put on your command line, I cannot find any AspectJ-related stuff in there. It is a really small JAR with only instrumenting classes that seem to rely on other transformers/weavers being deployed and somehow configured in your container.

I suggest you put the actual AspectJ weaving agent on your command line and see what happens. ;-)

Update: Or you if that does not help try both agents on the command line at the same time. I cannot tell you exactly what spring-instrument does, but I think it somehow integrates aspectjweaver (which does the actual aspect weaving) deeper into the Spring framework, even though AspectJ would also work without Spring even knowing of its existence.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Sorry - I should have mentioned in the original post that I've tried all combinations of the aspectjweaver jar and spring-instrument both together and separate. – Jared Sep 12 '16 at 18:39
  • Then I believe you have a configuration problem, e.g. the weaver cannot find the configuration or there is a syntax error within same. – kriegaex Sep 12 '16 at 19:03