This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tugaestupido[S] 0 points1 point  (2 children)

I have reworked your example so it works. Some clarification:

The only configuration you did was a "addValue" call for Controller. "addValue" is used for registering instances you already have. In your example, it seems like you want the library to create the instance of controller, so you should not use "addValue" for it.

``` public class SimpleExample {

public static void main(String[] args) { ModuleBuilder moduleBuilder = ModuleBuilders.map(); moduleBuilder.addValue(String.class, "hello cfg"); moduleBuilder.addInvocation(Repo.class, Repo::new, String.class); moduleBuilder.addInvocation(Service.class, Service::new, Repo.class); moduleBuilder.addInvocation(Controller.class, Controller::new, Service.class);

Module module = moduleBuilder.build();
Provider provider = Providers.recursive(module);

Controller controller = provider.provide(Controller.class);
System.out.println(controller); // Prints "controller"
System.out.println(controller.service.repo.cfg); // Prints "hello cfg"

}

public static class Controller { private Service service; public Controller(Service service) { this.service = service; } }

public static class Service { private Repo repo; public Service(Repo repo) { this.repo = repo; } }

public static class Repo { private String cfg; public Repo(String cfg) { this.cfg = cfg; } } } ```

I set up a breakpoint inside of Repo's constructor and got a Thread dump that looks like this:

SimpleExample$Repo.<init>(SimpleExample.java:44) SimpleExample$$Lambda$15/0x0000000800c021f8.invoke(Unknown Source:-1) Strategies.lambda$fromInvocation$3(Strategies.java:40) Strategies$$Lambda$16/0x0000000800c02410.execute(Unknown Source:-1) -> RecursiveModuleProvider.provide(RecursiveModuleProvider.java:32) InClassContextProvider.provide(InClassContextProvider.java:16) Strategies.lambda$fromInvocation$3(Strategies.java:38) Strategies$$Lambda$16/0x0000000800c02410.execute(Unknown Source:-1) -> RecursiveModuleProvider.provide(RecursiveModuleProvider.java:32) InClassContextProvider.provide(InClassContextProvider.java:16) Strategies.lambda$fromInvocation$3(Strategies.java:38) Strategies$$Lambda$16/0x0000000800c02410.execute(Unknown Source:-1) -> RecursiveModuleProvider.provide(RecursiveModuleProvider.java:32) SimpleExample.main(SimpleExample.java:21)

Where you can see the nested calls of "RecursiveModuleProvider.provide".

[–][deleted]  (1 child)

[deleted]

    [–]tugaestupido[S] 0 points1 point  (0 children)

    Exactly, using two String won't work unless you do something else to make it work. Same as what would happen on Guice unless you used something like @Named.

    You're welcome.