Evgeny Chesnokov
2017-11-28 10:54:49 UTC
Hi all!
I'm having a problem with Guice injection in one particular corner-case. I
use Guice to inject model to the controller instances that are
parameterized in the constructor, hence I'm using #bindMembers() and not
the constructor injection. It seems that if a model callback field in the
controller instance was instantiated before #bindMembers() call, then it
fails to see the injected model classes. Here's a quick sample:
class MyController extends MyAbstractController {
private int param;
@Inject private MyModel model;
private /*could also be final*/ PropertyChangeListener listener =
new PropertyChangeListener() {
@Override public void propertyChange( PropertyChangeEvent evt ) {
model.doStuff(); *// fails with NPE because model is null!*
}
};
public MyController(int param) {
this.param = param;
}
// Injector#injectMembers(myController) gets called by my framework and
then myController#init()
public init() {
model.addPropertyChangeListener("prop", listener); // *does NOT fail*
with NPE during initialization
}
}
So I've been wondering which internal mechanims I keep missing that allow
for this kind of a strange behaviour? Any ideas welcome.
Thanks in advance,
Evgeny.
I'm having a problem with Guice injection in one particular corner-case. I
use Guice to inject model to the controller instances that are
parameterized in the constructor, hence I'm using #bindMembers() and not
the constructor injection. It seems that if a model callback field in the
controller instance was instantiated before #bindMembers() call, then it
fails to see the injected model classes. Here's a quick sample:
class MyController extends MyAbstractController {
private int param;
@Inject private MyModel model;
private /*could also be final*/ PropertyChangeListener listener =
new PropertyChangeListener() {
@Override public void propertyChange( PropertyChangeEvent evt ) {
model.doStuff(); *// fails with NPE because model is null!*
}
};
public MyController(int param) {
this.param = param;
}
// Injector#injectMembers(myController) gets called by my framework and
then myController#init()
public init() {
model.addPropertyChangeListener("prop", listener); // *does NOT fail*
with NPE during initialization
}
}
So I've been wondering which internal mechanims I keep missing that allow
for this kind of a strange behaviour? Any ideas welcome.
Thanks in advance,
Evgeny.
--
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/e66a5a5e-5fad-4605-9ae7-71b2634212d0%40googlegroups.com.
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/e66a5a5e-5fad-4605-9ae7-71b2634212d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.