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.
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:
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!
Should the @AfterInitialization annotation work with a bean tagged as @Dependent?
I have one, and it won't trigger the method.
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.