dev@glassfish.java.net

Debugging dependency injection in GlassFish

From: Ken Cavanaugh <kencavanaugh_at_me.com>
Date: Tue, 04 Jan 2011 00:17:51 -0800

I've made a few changes in the FOLB code in glassfish-naming, and now some
of the surefire tests for naming are failing. The scenario is that
SerialInitContextFactory
needs to call GlassFishORBHelper.getORB(). GlassFishORBHelper is annotated
with @Service and implements PostConstruct and ORBLocator.
The PostConstruct method calls

orbFactory = habitat.getByContract( GlassFishORBFactory.class ) ;

where habitat was obtained from @Inject on a field.
The tests are failing because orbFactory is null.

ORBFactory is an @Contract interface, which is implemented
by GlassFishORBFactoryImpl, which is a class annotated with @Service.
GlassFishORBFactoryImpl also implements PostConstruct.

So how do I debug this and figure out why getByContract returned null?
I haven't changed any of the annotations, so presumably something else
is failing,
but what?

A quick look into getByContract seems to indicate that the byContract
MultiList
does not contain GlassFishORBFactory, but why?

Another possible clue is that a breakpoint in the
GlassFIshORBFactoryImpl.postConstruct method is never
executed. So it seems that the GlassFishORBFactoryImpl class is perhaps
ignored
by hk2, even though it has the @Service annotation.
GlassFishORBFactoryImpl has no
static initializer, and a default constructor, so I can't see why there
should be a problem
instantiating this class.

Thanks,

Ken.