Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [sisu-users] watching injected components

ProvisionListener#onProvision javadoc is quite unambiguous regarding
@Singleton components


    Types bound in Singleton scope will only be provisioned once.


ProvisionListener is not invoked on injection of seed components of
custom scopes either.

One of usecases is to print debug information similar to what is printed
for mojo parameters/values when Maven runs with -X, only for injected
components, i.e. before mojo execution starts, print injected field
names or constructor parameter indexes and their corresponding values.
There are more interesting usecases, but I want to make this one work first.

--
Regards,
Igor


On 2014-03-01, 16:11, Stuart McCulloch wrote:
On 1 Mar 2014, at 21:00, Igor Fedorenko <igor@xxxxxxxxxxxxxx> wrote:

I played with ProvisionListener earlier toda and it did not do what
I need.

From what I understood, ProvisionListener is invoked on component
provisioning, i.e. instance creation, not component injection.
Going back to my example, if ComponentA is @Singleton,
ProvisionListener will be called only once for the ComponentA
instance even if it is injected in multiple ComponentB instances.

I believe it is called per-injection request (aka provisioning) not
per-construction, but I might be mis-remembering. It’s definitely
more than per-construction, but that might be because it’s reporting
each link as it follows the dependency chain. There is no other API
that lets you intercept the injection of a specific member, but if
you describe your use-case in more detail you might not need this at
all because there may be alternative solutions.

I also looked at InjectionListener, which is invoked on each
ComponentA injection, but I could not find a way to locate target
ComponentA instance.

Have I misunderstood the API?

-- Regards, Igor

On 2014-03-01, 15:37, Stuart McCulloch wrote:
On 1 Mar 2014, at 19:26, Igor Fedorenko <igor@xxxxxxxxxxxxxx> wrote:

Say I have a couple of components, ComponentA and ComponentB

   @Named
   class ComponentA {}

   @Named
   class ComponentB {
      @Inject
      ComponentA a;
   }

Does sisu (or guice for that matter) provide a way to observe injection
of ComponentA instances into ComponentB instances?

To give some context, I develop a maven extension that needs to track
what objects are injected in Mojo instances and it needs to have
per-Mojo breakdown of all injected components.

The API that comes closest is probably the Guice ProvisionListener API:

	https://code.google.com/p/google-guice/source/browse/core/src/com/google/inject/spi/ProvisionListener.java

This lets you intercept the provisioning of any objects initiated by Guice and also provides the dependency chain that led to the object being provisioned - you’ll need to drill down the chain into the Dependency then the InjectionPoint if you want to know the exact declaring type and member (constructor, method, field etc.)

You can bind a listener for all bindings as follows:

	bindListener(Matchers.any(), myProvisionListener);  // you can narrow down the matching if you know what bindings you’re interested in

	https://code.google.com/p/google-guice/source/browse/core/src/com/google/inject/Binder.java#379

See also https://code.google.com/p/google-guice/issues/detail?id=78#c16

--
Regards,
Igor
_______________________________________________
sisu-users mailing list
sisu-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/sisu-users

_______________________________________________
sisu-users mailing list
sisu-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/sisu-users

_______________________________________________
sisu-users mailing list
sisu-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/sisu-users

_______________________________________________
sisu-users mailing list
sisu-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/sisu-users



Back to the top