Constructor Injection using Guice

守給你的承諾、 提交于 2019-12-07 03:05:48

问题


I have some sample code which is using factories. I'd like to clean up the code by removing the factories and use Guice instead. I attempted to do this but I hit a small roadblock. I am really new to Guice, so I am hoping someone can help me out here.

Existing client code (Using factories):

public class MailClient {

    public static void main(String[] args) {
        MailConfig config = MailConfigFactory.get();
        config.setHost("smtp.gmail.com");
        Mail mail = MailFactory.get(config);
        mail.send();
    }
}

My attempt to refactor using Guice:

//Replaces existing factories
public class MailModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(Mail.class)
        .to(MailImpl.class);

        bind(MailConfig.class)
        .to(MailConfigImpl.class);
    }
}

public class MailImpl implements Mail {

    private final MailConfig config;

    @Inject
    public MailImpl(MailConfig config) {
        this.config = config;
    }

    public void send() { ... }
}

public class MailClient {

    public static void main(String[] args) {
        MailModule mailModule = new MailModule();
        Injector injector = Guice.createInjector(mailModule);
        MailConfig config = injector.getInstance(MailConfig.class);
        config.setHost("smtp.gmail.com");
        Mail mail = //??
        mail.send();
     }
}

How would I construct an instance of MailImpl using the object config in my revised MailClient? Should I be using Guice in this way?


回答1:


Take a look at AssistedInject. It appears to address this problem.




回答2:


2 solutions are possible: 1) bind the config as a guice object also, including its host parameter. then just inject Mail, in your main method you cna ignore the fact that mail has further dependencies.

2) mail must be configured individually for each send (recipient?). then you have no choice, but create it yourself using MailFactory.




回答3:


You can do everything in MailModule as follows:

public class MailModule extends AbstractModule {
    @Override
    protected void configure() {
       ... // other bindings
    }

    @Provides
    MailConfig getMailConfig( ... ) {
        MailConfig config = new MailConfig( ... );
        config.setHost("smtp.gmail.com");
        config;
    }
}

If you want a singleton MailConfig, add the @Singleton annotation to getMailConfig(), and Bob's your uncle.

Note that arguments to getMailConfig must be bound. When you bind commonly used types like String, be sure to add a binding annotation.



来源:https://stackoverflow.com/questions/2105895/constructor-injection-using-guice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!