javaee / hk2

A light-weight and dynamic dependency injection framework
https://javaee.github.io/hk2
Other
112 stars 83 forks source link

ContextAware Service Injection #364

Closed buko closed 6 years ago

buko commented 7 years ago

In an application design where there are a tree of ServiceLocator/Contexts eg:

A -- B --- C

An InjectionResolver or a JustInTimeInjectionResolver that is bound as a Singleton in the root ServiceLocator (A) and is visible to a leaf (C) should be aware of the C context when it is called up to resolve an injection point for a service in C.

One simple way to fix this: provide an Injectee.getServiceLocator() method which returned the ServiceLocator of the Injectee. Then InjectionResolvers bound in A could examine C's context when doing service injection for services in C.

buko commented 7 years ago

This looks easy to do since it seems primarily it's the ServiceLocator that ever creates Injectees.

I'm not sure if my explanation above makes sense so here's a simpler explanation: I have an InjectionResolver in a parent ServiceLocator. It's being asked to inject one service of type B into another service S that's bound in the child ServiceLocator. The problem is I need access to S' ServiceLocator so that I can consider candidates in that ServiceLocator in addition to mine.

A workaround is to bind the InjectionResolver into every ServiceLocator. But it seems a Injectee.getServiceLocator method is a lot cleaner solution.

glassfishrobot commented 6 years ago

Closing this as this issue is migrated to https://github.com/eclipse-ee4j/glassfish-hk2/issues/365