Java Play 2.5.x JavaCache - How to get the current cache instance?

122 views Asked by At

I've followed the example at https://www.playframework.com/documentation/2.5.x/JavaCache and I can successfully access the cache, but I only seem to be able to do this from a controller method. Specifically:

@Inject
public OverviewController(CacheApi cache) { this.lookupCache = cache; }

From the controller actions, I can then interrogate the cache as needed via lookupCache.

My problem is that I need this functionality a few layers deeper in the application, and I'd really like to prevent passing a reference to lookupCache through several other constructors.

The documentation seems to suggest the capability of adding this injection on any "component", which would give me what I want, but if I try the same injection on some other class:

public class DropDownCache {
    private CacheApi lookupCache;
    @Inject
    public DropDownCache(CacheApi cache) { this.lookupCache = cache; }
}

...then any time I instantiate DropDownCache, I have to supply the CacheApi instance. This tells me that the controller is magically being instantiated with this reference, I just don't know how to access it.

Does anyone have any pointers on where the controllers get their instance of CacheApi from?

0

There are 0 answers