[MGNLCACHE-69] Implement javax.inject.Provider which injects a @Named cache Created: 13/Aug/14 Updated: 18/Jun/15 Resolved: 09/Sep/14 |
|
| Status: | Closed |
| Project: | Cache Modules |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Task | Priority: | Neutral |
| Reporter: | Magnolia International | Assignee: | Magnolia International |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Template: |
|
||||||||
| Acceptance criteria: |
Empty
|
||||||||
| Task DoR: |
Empty
|
||||||||
| Description |
|
With public class FooBar { private final Cache cache; @Inject public FooBar(CacheModule cacheModule) { // or keep a ref to module and retrieve the cache when needed this.cache = cacheModule.getCacheFactory().getCache("foo-cache"); } } It'd be nice if one could do this instead: public class FooBar { private final Cache cache; @Inject public FooBar(@Named("foo-cache") Cache cache) { this.cache = cache; } } Another approach (or additional) would let one inject an unnamed Cache, and the name would, by convention, be the class name of the injectee. package zim.zam; public class FooBar { private final Cache cache; @Inject public FooBar(Cache cache) { assert cache.getName().equals("zim.zam.FooBar"); this.cache = cache; } } |
| Comments |
| Comment by Magnolia International [ 08/Sep/14 ] |
|
I thought Mycila could help with this, but I've failed miserably. Couldn't find a way for a Provider to know about they key it's being used for, or more generally the "context" in which it's being used. (we can't pre-bind named caches, since we don't know their names. http://code.mycila.com/guice/#1-customizes-injection-annotations |
| Comment by Magnolia International [ 08/Sep/14 ] |
|
Maybe Guice's multi injection might be of inspiration (and/or useful for other purposes too) |
| Comment by Magnolia International [ 09/Sep/14 ] |
|
There are possible ways to do this with Guice, but absolutely none that feels like it wouldn't be a giant hack. Guice's main target is systems where you know all the bindings when a container is started. This isn't the case which caches (although by writing this I wonder how much of a hack it'd be to create a container per Cache). The other problem is that I couldn't find a way to somehow pass the injectee, or information about it, to a Provider. (i.e to use the annotation or its value to create/get the cache) GuiceyFruit and Mycila seem to have what it takes to do it, but it smells so bad, and I couldn't figure it out. Their stuff seems based mostly on Guice listeners, and not applicable to constructor injection. |