Discussion:
Help with @Inject, for some reason it doesn't work.
p***@mailarchiva.ru
2018-05-24 13:49:28 UTC
Permalink
I have a class i wan't inject
For legacy code support it is like this.
@Singleton
public class Applications extends AbstractModule {
private Applications() {}
private static class ApplicationsLoader {
static final Applications INSTANCE = new Applications();
}
@Override protected void configure() {}
@Provides @Singleton
public Applications get() {
return ApplicationsLoader.INSTANCE;
}
public static Applications getApps() {
return ApplicationsLoader.INSTANCE;
}
}
On application startup , I create injector.
public void startup() throws Exception {
initLogging();
Injector injector = Guice.createInjector(Stage.PRODUCTION,
Applications.getApps());
}
but some while after, when I need Applications.class i always get a null
public class Blob {
@Inject private Applications applications;
.....
}
Any suggestions how to share Applications.class singleton?
injector.getInstance(Applications.class) working fine.

Regards,

Valentin.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/d02fdaa7-59e9-4b4e-8f47-4a43a04d53db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2018-05-24 14:15:23 UTC
Permalink
The question is:

How are you creating the instance of Blob?

Most likely the instance of Blob was not created by guice. This happens
when you call "new". In order to have guice create the instance you must
do either one of the following:
- ask the injector for an instance -- injector.getInstance(Blob.class)
- have Blob injected into another class.

To avoid such situations it is always a possibility to use constructor
injection instead of field injection:

public class Blob {
    private final Applications applications;

    @Inject
    Blobb(Applications applications) {
        this.applications = applications;
    }

.....
}

Now the compile will force you to pass an instance of Applications when
you call new Blob.
Guice will happily pass all arguments to the constructor annotate with
@Inject.
Post by p***@mailarchiva.ru
I have a class i wan't inject
For legacy code support it is like this.
@Singleton
public class Applications extends AbstractModule {
    private Applications() {}
    private static class ApplicationsLoader {
        static final Applications INSTANCE = new Applications();
    }
    public Applications get() {
        return ApplicationsLoader.INSTANCE;
    }
   public static Applications getApps() {
        return ApplicationsLoader.INSTANCE;
    }
}
On application startup , I create injector.
public void startup() throws Exception {
initLogging();
Injector injector = Guice.createInjector(Stage.PRODUCTION,
Applications.getApps());
}
but some while after, when I need Applications.class i always get a null
public class Blob {
.....
}
Any suggestions how to share Applications.class singleton?
injector.getInstance(Applications.class) working fine.
Regards,
Valentin.
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/d02fdaa7-59e9-4b4e-8f47-4a43a04d53db%40googlegroups.com
<https://groups.google.com/d/msgid/google-guice/d02fdaa7-59e9-4b4e-8f47-4a43a04d53db%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/6e966ba5-10cf-59a1-dad4-c604df112767%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
Valentin Popov
2018-05-24 14:21:26 UTC
Permalink
Thanks!

Got it.
Post by Stephan Classen
How are you creating the instance of Blob?
- ask the injector for an instance -- injector.getInstance(Blob.class)
- have Blob injected into another class.
public class Blob {
private final Applications applications;
@Inject
Blobb(Applications applications) {
this.applications = applications;
}
.....
}
Now the compile will force you to pass an instance of Applications when you call new Blob.
Post by p***@mailarchiva.ru
I have a class i wan't inject
For legacy code support it is like this.
@Singleton
public class Applications extends AbstractModule {
private Applications() {}
private static class ApplicationsLoader {
static final Applications INSTANCE = new Applications();
}
@Override protected void configure() {}
@Provides @Singleton
public Applications get() {
return ApplicationsLoader.INSTANCE;
}
public static Applications getApps() {
return ApplicationsLoader.INSTANCE;
}
}
On application startup , I create injector.
public void startup() throws Exception {
initLogging();
Injector injector = Guice.createInjector(Stage.PRODUCTION, Applications.getApps());
}
but some while after, when I need Applications.class i always get a null
public class Blob {
@Inject private Applications applications;
.....
}
Any suggestions how to share Applications.class singleton?
injector.getInstance(Applications.class) working fine.
Regards,
Valentin.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
Visit this group at https://groups.google.com/group/google-guice <https://groups.google.com/group/google-guice>.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/d02fdaa7-59e9-4b4e-8f47-4a43a04d53db%40googlegroups.com <https://groups.google.com/d/msgid/google-guice/d02fdaa7-59e9-4b4e-8f47-4a43a04d53db%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
Visit this group at https://groups.google.com/group/google-guice <https://groups.google.com/group/google-guice>.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/6e966ba5-10cf-59a1-dad4-c604df112767%40gmx.ch <https://groups.google.com/d/msgid/google-guice/6e966ba5-10cf-59a1-dad4-c604df112767%40gmx.ch?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
С УважеМОеЌ,
ВалеМтОМ ППпПв
------------------------
http://mailarchiva.ru
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/754B2FD7-36CD-4CCC-B7CC-A93AED0DEBA4%40mailarchiva.ru.
For more options, visit https://groups.google.com/d/optout.
Loading...