Sponge: Is it right behaviour? Google guice

Created on 1 May 2017  路  3Comments  路  Source: SpongePowered/Sponge

Injector won't inject some provided instances automatically. Is it right behaviour?
E.g.
How to reproduce:

  1. Create main plugin class
  2. Create second class
  3. In annotate variable with Inject(e.g. Logger object)
  4. Add method to log something
  5. In main class, create our second class and execute method, to log something
  6. ????
  7. PROFIT!

Example classes:

@Plugin(id="testplug",name="testplug")
public class Main {
    @Listener
    public void onInit(GamePreInitializationEvent event) {
        new SecondClass().exec();
    }
}

public class SecondClass {
    @Inject
    private Logger logger;

    public void exec() {
        loggler.info("Catched!");
    }
}

Most helpful comment

The solution @dualspiral explained above works, but in fact all you need to do to make it work is to @Inject your SecondClass:

@Plugin(id="testplug",name="testplug")
public class Main {
    @Inject
    private SecondClass secondClass;

    @Listener
    public void onInit(GamePreInitializationEvent event) {
        secondClass.exec();
    }
}

public class SecondClass {
    @Inject
    private Logger logger;

    public void exec() {
        loggler.info("Catched!");
    }
}

All 3 comments

I assume you will get an NPE in your logger.info line. This behaviour is to be expected. Because you don't pass your newly created instance to the guice injector, thats why it is not injected.
Unfortunately I don't know how to fix that/trigger the injection myself, i usually expose the logger via the plugin instance that I pass during instance creation.

Yes, this is the correct behaviour. If you _really_ wanted to use the injector to create your second class, you would need to:

  • Add the field @Inject private Injector injector; to your Main class, so you can get the Injector for your plugin (Sponge does this injection for you on your plugin class _only_).
  • Use injector.getInstance(SecondClass.class) to create your class, or injector.injectMembers(Object) on the object _after_ it is constructed.

However, do consider whether it would just be better to pass the logger instance along, as @ST-DDT suggests. I would recommend reading up on Dependency Injection and Guice in general before making use of injectors, as well as the Sponge Docs page on DI.

If you need more help with this, we should be able to help you on IRC.

The solution @dualspiral explained above works, but in fact all you need to do to make it work is to @Inject your SecondClass:

@Plugin(id="testplug",name="testplug")
public class Main {
    @Inject
    private SecondClass secondClass;

    @Listener
    public void onInit(GamePreInitializationEvent event) {
        secondClass.exec();
    }
}

public class SecondClass {
    @Inject
    private Logger logger;

    public void exec() {
        loggler.info("Catched!");
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

JoeSGT picture JoeSGT  路  5Comments

XakepSDK picture XakepSDK  路  5Comments

nikosgram picture nikosgram  路  3Comments

Rasgnarok picture Rasgnarok  路  4Comments

SquidDev picture SquidDev  路  5Comments