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 6642305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:54:53+00:00 2026-05-25T23:54:53+00:00

A question about Guice. I’m still learning it, but I can understand the fundamentals.

  • 0

A question about Guice. I’m still learning it, but I can understand the fundamentals.

This question was already asked a couple of times on the net, but never with a concrete answer(none that I could find).

Say I have a situation like on the picture(a similar example was somewere on the net).

enter image description here

public class Dog {}

public class Walk implements Walkable {
    private final Dog dog;
    private final boolean leash;

    @Inject
    public Walk(Dog dog, @Assisted boolean leash) {
        this.dog = dog;
        this.leash = leash;
    }

    public void go() {
    }
}

public interface Walkable {
    void go();
}

public interface WalkFactory {
    Walk create(boolean leash);
}

public class AssistedMain {
    public static void main(String[] args) {
        Injector i = Guice.createInjector(new AbstractModule() {
            protected void configure() {

                install(new FactoryModuleBuilder().
                        implement(Walkable.class, Walk.class).
                        build(WalkFactory.class));
            }
        });

        Walk walk = i.getInstance(WalkFactory.class).create(true);
    }
}

That’s all great. But the question is – can I, somehow, reinject that object instance to the “container”(injector) to be used on the classes that rely on this dependency.

So, lets add a interface Person, class PersonImpl.

enter image description here

The new classes source are:

public interface Person {
    void walkDog();
}

public class PersonImpl implements Person {
    private Walkable walkable;

    @Inject
    public PersonImpl(Walkable walkable) {
        this.walkable = walkable;
    }

    public void setWalkable(Walkable walkable) {
        this.walkable = walkable;
    }

    public void walkDog() {
        walkable.go();
    }
}

So, the question is – am I, somehow able to actually inject this particular instance into the added object. This is a simple example, but we can presume there are 10 levels of classes below this one.

The solution I found is not very flexible. Something like:

Injector i = Guice.createInjector(new SimpleModule(false, dog));

And then bind to concrete instance. That’s not very dynamic. Basically, every time I need a different runtime/dynamic parameter I have to recreate the injector.

The Provider<T> is nice, the FactoryModuleBuilder helps, but how can I inject the objects back?

Are there more dynamic solutions to this problem?

Thanks.

  • 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-25T23:54:54+00:00Added an answer on May 25, 2026 at 11:54 pm

    MPierce – agreed. Ill try to explain the way i visualized the problem(you can correct me if im wrong).

    Being originaly derived from a “service locator” pattern, the idea that it can manage more than services is optimistic to say the least.

    We could split the application into Service and Data classes, or you could say that we have application and infrastructure code – “Dependency Injection”, a great book.

    So, basicly, dependecy injection, and dependency injection frameworks in general are great. For solving infrastructure, or “service” code.

    Any dynamic(runtime) parameters being injected into the Container/Injector are basicly forcing you to end the object graph.

    For example, we have the folowing design:

    enter image description here

    EmailMessage is a runtime parameter. It can be “injected” into email service outside the Container/Injector, but it ends the object graph. If we want to request EmailDispatcher, after we injected the EmailMessage into EmailService(which is, I repeat, done outside injector), we could no longer fetch EmailDispatcher from the injector.

    Then, you could redesign your model so it “fits” into the Container/Injector concept of dynamic parameters.

    enter image description here

    But then again, you forced the design, and suddenly, EmailDispatcher has too many responsibilites. It could be used in such a context, where you dont have many infrastructure classes.

    enter image description here

    And when you have a design like you have in the third example picture, you cannot use the Injector/Container to fetch you a NextService3 instance(nor any below the level of EmailDispatcher).

    The problem being – if you have any dynamic(runtime) parameters, you can only use dependency injection for classes above the class that requires a dynamic parameter, you can forget the classes below.

    Phew.

    Correct?

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

Sidebar

Related Questions

I understand that this question has been raised many times already. But I can't
I have been learning the Definitive Guide of JavaScript.i got a question about the
I recently asked a question about using Fluent NHibernate with .NET 4 - I
Depending on your interpretation this may or may not be a rhetorical question, but
This question is about variable naming style in objective c and cocoa. I just
Question about this helper http://codeigniter.com/user_guide/helpers/download_helper.html If, for example, program.exe weights 4 GB, will it
my question is about the ray wenderlich's book Learning Cocos2D: A Hands-On Guide to
This question is about the Jini technology. In Head First Java book, the authors
EDIT: I have made a clean, new project, but still can't get it working.
This is a question of curiosity about accepted coding practices. I'm (primarily) a Java

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.