我在我的应用程序中使用Google guice进行依赖注入。 我有这样的类结构,
示例取自here
这是我的界面,
package com.journaldev.di.services;
@ImplementedBy(EmailService.class)
public interface MessageService {
boolean sendMessage(String msg, String receipient);
}
我将在此实施
package com.journaldev.di.services;
import javax.inject.Singleton;
//import com.google.inject.Singleton;
@Singleton
public class EmailService implements MessageService {
public boolean sendMessage(String msg, String receipient) {
//some fancy code to send email
System.out.println("Email Message sent to "+receipient+" with message="+msg);
return true;
}
}
如果我在这里注入EmailService。
package com.journaldev.di.consumer;
import javax.inject.Inject;
import com.journaldev.di.services.MessageService;
public class MyApplication {
private MessageService service;
@Inject
public void setService(MessageService svc){
this.service=svc;
}
public boolean sendMessage(String msg, String rec){
//some business logic here
return service.sendMessage(msg, rec);
}
}
如果假设我的EmailService类看起来像这样,
package com.journaldev.di.services;
import javax.inject.Singleton;
//import com.google.inject.Singleton;
@Singleton
public class EmailService implements MessageService {
public EmailService(int someValue) {
FancyEmailService fancyEmailService = new FancyEmailService(someValue);
}
public boolean sendMessage(String msg, String receipient) {
fancyEmailService.doSomething();
System.out.println("Email Message sent to "+receipient+" with message="+msg);
return true;
}
}
为了测试上面的EmailService代码,我需要注入FancyEmailService而不是从构造函数实例化。如何将FancyEmailService注入到EmailService代码中?并且仍然能够将EmailService注入MyApplication。
答案 0 :(得分:2)
您需要查看这些内容 - https://github.com/google/guice/wiki/InstanceBindings和https://github.com/google/guice/wiki/ProvidesMethods
public class GuiceTestModule extends AbstractModule {
@Override
protected void configure() {
bind(Integer.class)
.annotatedWith(Names.named("fancy"))
.toInstance(10);
}
@Provides
FancyEmailService fancyEmailServiceProvider(@Named("fancy") int i){
return new FancyEmailService(i);
}
}
My FancyEmailService& EMailService如下所示。
public class FancyEmailService {
private final int i;
public FancyEmailService(int i){
this.i = i;
}
public void doSomething(){
System.out.println("i is injected with " + i);
}
}
@Singleton
public class EmailService implements MessageService {
private FancyEmailService fancyEmailService;
@Inject
public EmailService(@Named("fancy") int someValue){
fancyEmailService = new FancyEmailService(someValue);
}
public boolean sendMessage(String msg, String receipient) {
//some fancy code to send email
System.out.println("Email Message sent to "+receipient+" with message="+msg);
fancyEmailService.doSomething();
return true;
}
}
让我们试试
public class GuiceTest {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new GuiceTestModule());
MyApplication appn = injector.getInstance(MyApplication.class);
appn.sendMessage("msg", "guru");
}
}
输出:
Email Message sent to guru with message=msg
i is injected with 10
也可以使用@Provides
以这种方式编写EmailService@Singleton
public class EmailService implements MessageService {
private FancyEmailService fancyEmailService;
@Inject
public EmailService(FancyEmailService fes){
fancyEmailService = fes;
}
public boolean sendMessage(String msg, String receipient) {
//some fancy code to send email
System.out.println("Email Message sent to "+receipient+" with message="+msg);
fancyEmailService.doSomething();
return true;
}
}