Java – Guice runtime dependency parameter re injection

About Guice I'm still learning it, but I can understand the basics

This question has been asked on the Internet several times, but there has never been a specific answer (I can't find it)

Suppose my situation is like a picture (a similar example is on the Internet)

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);
    }
}

It's all great But the problem is – I can somehow re inject the object instance into the container (injector) for use on classes that depend on this dependency

So, let's add an interface, the class personimpl

The new class source is:

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 problem is - I can inject this particular instance into the added object in some way This is a simple example, but we can assume that there are 10 levels of classes under this class

The solution I found is not very flexible It's like:

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

Then bind to the concrete instance That's not very energetic Basically, every time I need different Runtime / dynamic parameters, I have to recreate the injector

The provider < T > is good and factorymodulebuilder is helpful, but how can I inject objects?

Is there a more dynamic solution to this problem?

thank you.

Solution

Mpierce – agreed I'll try to explain the way I imagine the problem (if I'm wrong, you can correct me)

Originally derived from the "service locator" model, the idea that it can manage more than services is at least optimistic

We can split applications into service and data classes, or you can say that we have application and infrastructure code - "dependency injection", which is a great book

Therefore, basically, dependency injection and dependency injection frameworks are usually great Code used to solve infrastructure or "service" problems

Any dynamic (runtime) parameters injected into the container / injector basically force you to end the object graph

For example, we have the following designs:

Emailmessage is a runtime parameter It can "inject" email services other than container / injector, but it will end the object graph If we want to request an emaildispatcher, after we inject the emailmessage into the emailservice (I repeat, outside the injector), we can no longer get the emaildispatcher from the injector

You can then redesign the model to "fit" the container / injector concept of dynamic parameters

But then again, you force design. Suddenly, emaildispatcher has too many responsibilities It can be used in an environment where you don't have many infrastructure classes

When you have a design similar to yours in the third example image, you cannot use the injector / container to obtain the nextservice3 instance (nor lower than the level of emaildispatcher)

The problem is - if you have any dynamic (runtime) parameters, you can only use dependency injection on the upper class of the class that needs dynamic parameters. You can forget the lower class

Yo

correct?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>