Without knowing more about this I'm not sure why it doesn't work. My
guesses would be:
1) It is possible that because you are adding both resolvers in the
same configure that somehow the one is not seeing the other, or at the
least is not recognizing the one annotation as an injection annotation.
Are you using the new feature in hk2 (in 2.3.0-b07) where you can mark
annotation as indicating injection points (@InjectionPointIndicator)
(
https://hk2.java.net/2.3.0-b07/apidocs/org/glassfish/hk2/api/InjectionPointIndicator.html).
My suspicion here is that when it is looking at the injection resolver
it does not recognize that annotation as being an injection point
because the other resolver is not yet available.
2) Is the fact that your second resolver is not in the Singleton scope
a problem? (I don't know)
I would try first doing this in two different configure operations (try
getting the ServiceLocator and doing a
ServiceLocatorUtilities.addClasses in SEPARATE calls to make sure the
things are added serially rather than all at once).
Hope this helps!
On 6/6/2014 3:25 PM, Dave Trombley wrote:
>
> Further:
>
> Even if I get the Singleton FieldQueryResolver instance manually
> from the locator after running the binder, and manually inject it,
> the EntityManager field in it remains null!
>
> Object fqrSvc = APPLICATION_LOCATOR.getService(new
> TypeLiteral<InjectionResolver<FieldQuery>>(){}.getType());
> APPLICATION_LOCATOR.inject(fqrSvc);
>
> I'm totally out of options here. =/
>
> -David
>
>
> On Fri, Jun 6, 2014 at 3:09 PM, Dave Trombley <dave.trombley_at_gmail.com
> <mailto:dave.trombley_at_gmail.com>> wrote:
>
>
>
> Hi all -
>
> Could someone tell me if this sort of thing is supported?
> The idea is that a custom annotation @ApplicationContext can
> inject a JPA EntityManager, and I want another custom annotation
> @FieldQuery to use that to construct and inject JPA queries into
> manager objects:
>
> protected void configure() {
> bind(ApplicationInjectionResolver.class)
> .to(new
> TypeLiteral<InjectionResolver<ApplicationContext>>(){})
> bind(FieldQueryResolver.class)
> .to(new
> TypeLiteral<InjectionResolver<FieldQuery>>(){})
> .in(Singleton.class);
> }
>
> -----
>
> public class FieldQueryResolver implements
> InjectionResolver<FieldQuery>{
>
> @ApplicationContext EntityManager entityManager;
>
> @Override
> public Object resolve(Injectee injectee, ServiceHandle<?> root) {
> FieldQuery aFQ =
> injectee.getParent().getAnnotation(FieldQuery.class);
> ...
> }
> -----
> static class ApplicationInjectionResolver implements
> InjectionResolver<ApplicationContext> {
> @Override
> public Object resolve(Injectee injectee, ServiceHandle<?>
> root) {
> Type targetType = injectee.getRequiredType();
> if (targetType instanceof Class) {
> Class targetClass = (Class)targetType;
> switch (targetClass.getCanonicalName()) {
> case "com.chesapeakelogic.ath.Application":
> return APPLICATION;
> case "freemarker.template.Configuration":
> return APPLICATION_FM_ERROR_CFG;
> case "javax.persistence.EntityManager": return
> APPLICATION_ENTITY_MANAGER;
> case "javax.sql.DataSource": return
> APPLICATION_DATA_SOURCE;
> case "org.eclipse.jetty.server.Server": return
> APPLICATION_SERVER;
> }
> }
> ...
> }
>
>
>
> That injection is working fine in other managed classes, but
> not in the @FieldQuery injection resolver... The docs seem to
> indicate that it should in fact work so long as the injection
> resolver isn't trying to invoke itself -- but that second
> resolve() method I pasted isn't getting called at all from the
> HK2-managed FieldQueryResolver instance...
>
> Any ideas? Is this sort of thing supposed to work, or is it
> too crazy?
>
> Also I noticed the ServiceHandle<?> passed as second parameters
> to those are always null. Why should that be? It would be
> useful to get at either the service handle data or the service
> locator from the resolver implementations... any way to do that?
>
> -David
>
>