Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7402585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:46:43+00:00 2026-05-29T04:46:43+00:00

Suppose I have some Message class like the following. (This is a made-up class

  • 0

Suppose I have some Message class like the following. (This is a made-up class for simplicity.)

public class Message {
  private String text;

  public Message(String text) {
    this.text = text;
  }

  public void send(Person recipient) {
    // I think I should be Guice-injecting the sender.
    MessageSender sender = new EmailBasedMessageSender();
    sender.send(recipient, this.text);
  }
}

Since I have different MessageSender implementations, and might want to unit test this sending ability, I think I should be injecting the MessageSender in Message‘s send() method. But how do I do this?

All the Guice examples I’ve seen and that I understand seem to do the injection in the constructor:

public class Message {
  private String text;
  private MessageSender sender;

  // ??? I don't know what to do here, since the `text` argument shouldn't be injected.
  @Inject
  public Message(String text, MessageSender sender) {
    this.text = text;
    this.sender = sender;
  }

  public void send(Person recipient) {
    this.sender.send(recipient, this.text);
  }
}

public class MessageSenderModule extends AbstractModule {
  @Override 
  protected void configure() {
    bind(MessageSender.class).to(EmailBasedMessageSender.class);
  }
}

But my Message class takes in a text argument in its constructor, which I don’t want to inject. So what am I supposed to do instead?

(Note: I’m a complete Google Guice noob. I think I understand dependency injection, but I don’t understand how to actually implement it with Guice.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:46:44+00:00Added an answer on May 29, 2026 at 4:46 am

    You could use assisted injection to provide the text through a factory, along with the message sender instantiated by Guice:

    public class Message {
      private String text;
      private MessageSender sender;
    
      @Inject
      public Message(@Assisted String text, MessageSender sender) {
        this.text = text;
        this.sender = sender;
      }
    
      public void send(Person recipient) {
        this.sender.send(recipient, this.text);
      }
    }
    

    Factory:

    public interface MessageFactory{
        Message buildMessage(String text);
    }
    

    Module:

    public class MessageSenderModule extends AbstractModule {
      @Override 
      protected void configure() {
        bind(MessageSender.class).to(EmailBasedMessageSender.class);
        FactoryModuleBuilder factoryModuleBuilder = new FactoryModuleBuilder();
        install(factoryModuleBuilder.build(MessageFactory.class));
      }
    }
    

    usage:

    @Inject MessageFactory messageFactory;
    
    void test(Recipient recipient){
      Message message = messageFactory.buildMessage("hey there");
      message.send(recipient);
    }
    

    Assisted Injection Wiki

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class that processes some data: class SomeClass { public: void
Suppose a construct like this: class Interface { public: template <typename T> virtual void
suppose I have this string: some striinnngggg <a href=something/some_number>linkk</a> soooo <a href=someotherthing/not_number>asdfsadf</a> I want
Suppose you have a Java class which defines a copyFile(String, String) method: public class
Suppose I have some class which has a property actor_ of type Actor .
Propose to consider the following problem. Suppose we have some composite object. Are there
Suppose I have a following constructor in C++ class: MyClass::MyClass() { char* buffer =
Suppose I have an interface for a service: public interface IFooService { void DoSomething();
I have a class that looks like this: class SearchService include Mongoid::Document key :name,
I have the following JAX-RS service. @Path(config) public class ConfigurationResponder { @GET @Produces({application/json}) @Consumes({application/json})

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.