java – Spring 5 @Cacheable is not working, Even though interceptor call is on the stack

java – Spring 5 @Cacheable is not working, Even though interceptor call is on the stack

So the problem was that I was calling the @Cacheable function too early, before the context was initialized. Once I called it after, its working fine.

I added:

 implements Runnable, ApplicationListener<ContextRefreshedEvent>  {

and

@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {

String name = arg0.getApplicationContext().getDisplayName();

if (name.contains(Root)) {
    // ANY CODE That requires @Cacheable is called HERE!.. 
    // Will not work if ran before this, such as inside @PostConstruct

    }
}

The Root check is kind of a hack to only call when the Root event is done, otherwise it gets called twice. I dont know how else to differentiate between the events.

java – Spring 5 @Cacheable is not working, Even though interceptor call is on the stack

Leave a Reply

Your email address will not be published. Required fields are marked *