How to @Inject the factory object

18 views
Skip to first unread message

unv...@gmail.com

unread,
Oct 16, 2020, 11:05:25 AM10/16/20
to Bootique User Group
Is it possible to @Inject a "factory" parameter instead of passing it as argument to create () method:

i.e. for this code to be valid and working as expected via BQ's dependecy injection mechanism? Thanks.

public class MyFactory {
    @Setter
    private int intProperty;
    @Setter
    private String stringProperty;
    @Setter
    @Inject
    private Service service;

    public OtherService create() {
        new OtherService(service)
            .setProperty(intProperty);
    }
}

Andrus Adamchik

unread,
Oct 16, 2020, 11:28:16 AM10/16/20
to Bootique User Group
Hi Vadim,

The short answer is no. We avoided that intentionally to separate "configuration"properties" and "DI services" within the factory. The alternative seemed too confusing. 

It generally works well, though I've encountered one scenario where the choice above is kinda problematic - polymorphic factories (factories that subclass each other). Each subclass may need different services to do its work, so we end up passing Injector in the method parameters, which is ugly.

So indeed we should give the factory injection a second thought. This may cut down on boilerplate in a couple of places.... 

Andrus

--
You received this message because you are subscribed to the Google Groups "Bootique User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bootique-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bootique-user/28902cd8-523c-4703-ac11-acb29b82b803n%40googlegroups.com.

unv...@gmail.com

unread,
Oct 16, 2020, 11:44:45 AM10/16/20
to Bootique User Group
That is exactly the use case I have.

I did this and works ok with me: I have the Injector as an argument to the @Provides method and then inside I am calling injector.injectMembers(serviceFactory) while looping through the values of the factories Map.

Reply all
Reply to author
Forward
0 new messages