-
1. Re: Errai CDI Injection Question
jfuerth Jan 16, 2014 6:50 PM (in response to mprk)Hi Matt,
CDI initializes objects in three phases (this is true on the server as well as in Errai's CDI implementation):
- Object is created via constructor invocation (the @Inject constructor if there is one; otherwise the no-args constructor)
- @Inject fields are populated
- @Inject setter methods are called (in no guaranteed order)
So you will always observe @Inject fields as null from the object's constructor.
Try moving your logging statement to a @PostConstruct method, and you should see a non-null Event object in the mapChanged field. PostConstruct methods are guaranteed to be called after all three phases of injection have completed.
-Jonathan
-
2. Re: Errai CDI Injection Question
csa Jan 16, 2014 7:27 PM (in response to mprk)Hi Matt,
You also need to make sure that MapView is itself a managed bean (i.e. by annotating it with @ApplicationScoped or @Dependent) and that you retrieve an instance of it using the bean manager (as opposed to just calling new MapView()).
You can get a managed instance of MapView either by having it injected into another bean or by using the client-side bean manager for programmatic look-up:
Client-Side Bean Manager - Errai - Project Documentation Editor
Cheers,
Christian
-
3. Re: Errai CDI Injection Question
mprk Jan 16, 2014 8:18 PM (in response to csa)Jonathan - Tried your suggestion and created a PostConstruct method to check the object. The PostConstruct method was never called.
Christian - Your suggestion fixed the issue.
Thanks each of you for the help!
-
4. Re: Errai CDI Injection Question
mprk Jan 20, 2014 10:16 AM (in response to csa)Should the @AfterInitialization annotation work with a bean tagged as @Dependent?
I have one, and it won't trigger the method.
-
5. Re: Errai CDI Injection Question
csa Jan 20, 2014 3:36 PM (in response to mprk)Yes, it will if your @Dependent bean is injected into another @ApplicationScoped/@Singleton bean or @EntryPoint. Otherwise there will likely not be an instance around when the bus has finished initializing and Errai doesn't create new instances of @Dependent scoped beans to handle events.