JSF 2.2: Dependency Injection

#JSF 2.2: Dependency Injection#

CDI 1.0 was part of Java EE 6, it is the standard Dependency Injection specification for Java EE platform. But unfortunately, JSF 2.0/2.1 which was also part of Java EE 6 did not adopt CDI as its Dependency Injection/IOC container, but invented its own IOC container.

In the package javax.faces.bean, there are several annotations provided.

You can annotate your JSF backend bean with annotation @ManagedBean and put it in a reasonable scope( @ApplicationScoped, @RequestScoped, @SessionScoped, @ViewScoped, @NoneSopced).

<pre> @ManagedBean(name="foo") @RequestScoped public class FooBean{ } </pre>

The attribute name specified in the @ManagedBean can be accessed in EL in facelets.

You can also inject other beans via @ManagedProperty.

When you are using Java EE 6, you may be confused about the JSF IOC and CDI, we need two types of IOC mechanism in projects?

CDI provides a complete list of scope definition for Java EE platform, including @ApplicationScoped, @RequestScoped, @SessionScoped, @ConversationScoped, which can be found in the package javax.enterprise.context. They are very similar with JSF defined, the difference is JSF has a specific @ViewScoped, and CDI provides a @ConversationScoped which is designated for JSF.

Some third party projects such as JBoss Seam 3, Apache MyFaces CODI etc are born and try to fill the gap between CDI and JSF. In JBoss Seam 3, you can use CDI beans/scopes freely as JSF backend beans. Apache MyFaces CODI provides more, it mapped all JSF scopes to CDI scopes internally which allow you use JSF scopes but it is processed by CDI container, and redesigned a new powerful Conversation which support conversation group, multi conversation, nested conversation etc, and provides some extra scopes, such as @ViewAccessScoped, Window scope etc.

But develops always dream JSF specification can reuse the effort from CDI export group. This became true in Java EE 7 finally after a 4-years long waiting.

In JSF 2.2, all JSF scopes defined in JSF 2.0(in the package javax.faces.bean) should be abandoned, you can use CDI beans as JSF backend beans freely. JSF 2.2 also provides two new CDI compatible scopes, @ViewScoped and @FlowScoped.

NOTE, the new @ViewScoped is located in the package javax.faces.view, it does not replace the old one.

@FlowScoped is designated for another new feature shipped with JSF 2.2, the workflow support, we will discuss it in further post.

Almost all JSF artifacts support @Inject now, but unfortunately, there are some exceptions, such as JSF converter and validator still do not support @Inject, which are canceled in the last milestone before JSF 2.2 was finalized. Maybe we have to wait another 4 years?

转载于:https://my.oschina.net/hantsy/blog/145330

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值