Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [sisu-users] injectMembers()

Note you can always create injectors (either sibling injectors or child injectors) at a later stage on the fly - using the shared locator to glue things together.

ie. while you can't add bindings to an existing injector, you can create and tear down injectors to support plugins coming and going (like in Maven)

On Tue, 18 Feb 2020 at 20:39, Anton Tanasenko <atg.sleepless@xxxxxxxxx> wrote:
Hey Stuart,

Yea I was talking about arbitrary components which are not mentioned in modules.

Took a quick look at LocatorWiring again and realized those bindings are created using a binder, which is used to create the injector, so cannot be used at a later stage. Makes perfect sense now.
What I wanted was something that satisfied missing dependencies using BeanLocator on the fly, omitting the binding phase.


вт, 18 февр. 2020 г. в 18:15, Stuart McCulloch <mcculls@xxxxxxxxx>:
Hi Anton,

Yes, you can inject dynamic lists into components using `injector#injectMembers()` - to do this you would install a WireModule around the application modules before creating the injector. You'd then need to share the same bean locator instance across any child/sibling injectors so components in them can be discovered.

Note this assumes that either the component being injected via `injector#injectMembers()` is mentioned in the application modules or the type of the dynamic list is bound somewhere using `requireBinding` so that the WireModule knows to register a binding for that particular dynamic list backed by the locator. This is because Guice doesn't let you create complex bindings on the fly (it only supports very simple just-in-time bindings.)

For example, assuming MyComponent exposes a setter to inject the dynamic list:

    // where sharedBeanLocator is shared with other injectors using "bind(BeanLocator.class).toInstance(sharedBeanLocator);" in their configuration

    MyComponent component = new MyComponentImpl();
    Guice.createInjector(new WireModule(new AbstractModule() {
        protected void configure() {
            bind(MyComponent.class);
            bind(BeanLocator.class).toProvider(() -> sharedBeanLocator);
            // ^ provider so we can discover bindings in other injectors using this locator without publishing our bindings
        }
    })).injectMembers(component);

This is just a quick example - if you can provide more details about your use-case I can come up with more detailed suggestions.

--
Cheers, Stuart

On Thu, 6 Feb 2020 at 23:13, Anton Tanasenko <atg.sleepless@xxxxxxxxx> wrote:
Hi,

Is it possible to benefit from LocatorWiring/BeanLocator within `injector#injectMembers()` execution in a fashion similar to injector creation (courtesy of WireModule)? 

Instances for members injection are factory-created. Existing bindings are injected properly, but I would like to get, for example, (effectively deferred) List bindings that come as part of dynamic child injectors working in the same way.

--
Regards,
Anton.
_______________________________________________
sisu-users mailing list
sisu-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/sisu-users
_______________________________________________
sisu-users mailing list
sisu-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/sisu-users


--
Regards,
Anton.
_______________________________________________
sisu-users mailing list
sisu-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/sisu-users

Back to the top