Balaraj V
2017-04-18 19:40:42 UTC
I'm using Google Guice for dependency injection in my application. I have a
class structure like this,
Example is taken from here
<http://www.journaldev.com/2403/google-guice-dependency-injection-example-tutorial>
This is my interface,
package com.journaldev.di.services;
@ImplementedBy(EmailService.class)public interface MessageService {
boolean sendMessage(String msg, String receipient);}
Which I will implement here
package com.journaldev.di.services;
import javax.inject.Singleton;
//import com.google.inject.Singleton;
@Singletonpublic 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;
}}
If I inject EmailService here.
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);
}}
If suppose my EmailService class looked like this,
package com.journaldev.di.services;
import javax.inject.Singleton;
//import com.google.inject.Singleton;
@Singletonpublic 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;
}}
In order to test the above EmailService code, I need to inject
FancyEmailService than instantiating from the constructor. How do I inject
FancyEmailService into EmailService code? and still be able to inject
EmailService into MyApplication.
I posted the same question in Stackoverflow could not find any answers.
class structure like this,
Example is taken from here
<http://www.journaldev.com/2403/google-guice-dependency-injection-example-tutorial>
This is my interface,
package com.journaldev.di.services;
@ImplementedBy(EmailService.class)public interface MessageService {
boolean sendMessage(String msg, String receipient);}
Which I will implement here
package com.journaldev.di.services;
import javax.inject.Singleton;
//import com.google.inject.Singleton;
@Singletonpublic 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;
}}
If I inject EmailService here.
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);
}}
If suppose my EmailService class looked like this,
package com.journaldev.di.services;
import javax.inject.Singleton;
//import com.google.inject.Singleton;
@Singletonpublic 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;
}}
In order to test the above EmailService code, I need to inject
FancyEmailService than instantiating from the constructor. How do I inject
FancyEmailService into EmailService code? and still be able to inject
EmailService into MyApplication.
I posted the same question in Stackoverflow could not find any answers.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/241faaeb-f5a5-4b93-930c-db977a929899%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/241faaeb-f5a5-4b93-930c-db977a929899%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.