Red Hat
Jan 11, 2013
by Mike Brock
One common problem that keeps catching people off guard using Errai UI and its hip companion framework, Errai UI Navigation, has been that @Templated and @Page beans have required explicit-scoping in order to work correctly.

It turns out that you almost always want such beans to be of the @Dependent scope. But if you know a little bit about CDI, you might be saying to yourself: well, all non-explicitly scoped beans are of the @Dependent scope! 

Well, yes. This is true. But Errai throws a bit of a curve-ball here. Because while Errai respects this semantic when it comes to static injection, it does not respect it when it comes to dynamic injection / dynamic lookup.

The reason for this is straight-forward. When Errai compiles your CDI code magically down to JavaScript (via the GWT compiler), it prunes all the bean wiring code from non-explicitly scoped beans. It does this as a way of minimizing the amount of dead code that ends up being compiled. 

So, for example, if we respected the CDI specification strictly, we'd end up having to create bean wiring code for things like ArrayList, HashMap, LinkedList, StringBuilder, etc.  Instead, we allow you to @Inject those dependent beans directly as you might in a fully compliant CDI container. But we don't produce the code to allow them to be dynamically produced at runtime.  

So, for Errai, implicit dependent beans are a compile-time feature. Not a runtime feature.

But wait! It turns out there's a common need to dynamically lookup @Dependent beans in Errai applications. Widgets, for instance, are more often than not, of the @Dependent scope. And many widgets you want to instantiate many times, dynamically. Which is a perfect fit for the @Dependent scope. 

So to allow you to overcome the limitation we impose, you can annotate your bean with a @Dependent annotation. This explicit scoping is treated as signal that you want to interact with the bean via the bean manager. And that's why you've had to explicitly scope all your @Templated and @Page beans up until now.

But as it turns out, we've changed the rules. In the absence of an explicit scope, Errai will now consider the use of either of these two annotations to mean the same thing as an explicitly-scoped @Dependent bean.

The good news is that this improvement will be directly available in the next 2.2 release. And in final.

Hopefully this improvement makes your coding experience a little bit more pleasant. It has for us.