Help

Some people have wondered why CDI requires an explicit @Named annotation before a bean can be accessed using EL? Why not just give all beans names by default?

Well, the reason is that EL has a single flat namespace. There's no notion of qualified names in EL. Experience in the JSF community is that avoiding duplicate names is harder than it looks, especially when the application includes multiple modules maintained by different teams. Remember that not all CDI beans are intended to form part of the user interface layer. A Java EE 6 application might include modules defining an independent business layer, whose beans should always be accessed via typesafe dependency injection instead of non-typesafe EL names. Beans in these modules should not have EL names, in order to avoid polluting the namespace of the UI layer.

Still, the combination @Named @RequestScoped is going to be super-common in JSF applications. So CDI includes a built-in stereotype that encapsulates these defaults. The @Model stereotype refers to the M role in the MVC pattern. We recommend that all JSF user-interface-oriented beans be annotated @Model to both save a little typing (one annotation instead of two), and, more importantly, to make clear their architectural role in the system.

Indeed, even when you have a UI model bean that's not @RequestScoped, I recommend annotating it @ConversationScoped @Model rather than @Named @ConversationScoped. The two options have the same functional effect, but the first combination communicates more information to someone reading your code later, and makes it easy to quickly search for all UI model beans.

(Of course, if, for whatever reason, you don't like @Model, you can always define your own stereotype to fulfil a similar purpose. The CDI container doesn't treat @Model in any special way.)

8 comments:
 
18. Jan 2011, 01:21 CET | Link
anonymouscoward

A big global hashmap... Nobody told you that global variables are evil?

ReplyQuote
 
18. Jan 2011, 06:49 CET | Link
A big global hashmap... Nobody told you that global variables are evil?

Rmmm ... you're aware that Unified EL originally came out of the JSP spec, right? If you're trying to blame us for the design of Unified EL, you're giving us waaay too much credit...

 
18. Jan 2011, 07:44 CET | Link

Semi-related but are there any plans to split off EL to a JSR of its own?

 
20. Jan 2011, 09:37 CET | Link

As name-based injection is discouraged, I think it's perfectly logical that beans don't have names by default. Using a bean in EL is a side-effect. I don't think anyone should be tempted to refer to DAO beans in the view.

 
20. Jan 2011, 21:01 CET | Link
Walter

What's the consensus on @ViewScoped?

 
20. Feb 2011, 15:02 CET | Link
for whatever reason, you don't like @Model, you can always define your own stereotype to fulfil a similar purpose.

You're absolutely right, but using the standard annotation will communicate more clearly to someone reading the code what the intention is. Personally I associate the term model more with (JPA) entities, but maybe that's my mistake.

I did notice that a request scoped named bean is often denoted by the term backing bean, so maybe @Backing of @BackingBean would have been a better choice?

 
22. Feb 2011, 13:59 CET | Link
Flavio Oliva | denon82(AT)gmail.com
I did notice that a request scoped named bean is often denoted by the term 'backing bean', so maybe @Backing of @BackingBean would have been a better choice?

Nice point! @BackingBean would be a nice interface name but @Model is fine too. Acctually, it really doesn't matter the name of the stereotype, as long as you understand its meaning you are alright.

 
25. Mar 2013, 10:20 CET | Link

Great post. CDI is still evolving, it will take some time.

Post Comment