跳至主要内容

JSF 2.2: Embrace CDI?

JSF 2.2: Embrace CDI?

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

Dependency injection in JSF 2.0

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).
@ManagedBean(name="foo")
@RequestScoped
public class FooBean{

}
The attribute name specified in the @ManagedBean can be accessed via EL in facelets views.
You can inject it in other beans via @ManagedProperty.
@ManagedBean(name="bar")
@RequestScoped
public class BarBean{
    @ManagedPropertiy("foo")
    FooBean foo;
}
In JSF 2.0, it also supports to annotate the fields with @Resource and @EJB annotations to inject Java EE resources and EJB.
@ManagedBean(name="bar")
@RequestScoped
public class BarBean{
    @Resource("")
    DataSource ds;
    
    @EJB
    HelloEJB hello;
}

CDI 1.0 and JSF 2.0

When you are using Java EE 6, you maybe have confused about the JSF IOC and CDI, we need two types of IOC mechanism in projects?
CDI provides a complete list of scopes for Java EE platform, including @ApplicationScoped, @RequestScoped, @SessionScoped, @ConversationScoped, which are provided in the package javax.enterprise.context. As you see, they are very similar with JSF scopes. The difference is JSF has a specific @ViewScoped and there is no equivalent concept in CDI, and CDI provides a @ConversationScoped which is also designated for JSF in CDI 1.0.
But due to some unknown reason, JSF EG refused to reuse CDI in Java EE 6. It was explained the JSF 2.0 features had been frozen and would not be changed, even there were some months left before Java EE 6 was released. The most reasonable explanation is that it was the result of political fight between the Java EE giants, such as Oracle, RedHat etc.
Luckily, CDI beans can be accessed through EL in faceslet views directly. And 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 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.

CDI 1.1 and JSF 2.2

In Java EE 7, the JSF specific ManagedBean specification is discontinued.
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 introduces two new CDI compatible scopes, @ViewScoped and @FlowScoped.
NOTE, the new @ViewScoped is located in the package javax.faces.view, it is a new annotation.
@FlowScoped is designated for another new feature shipped with JSF 2.2, the Faces Flow support.
Almost all JSF artifacts support @Inject now. For example, the DebugPhaseListener provided in the sample codes is a custom PhaseListener which is use for tracking the JSF request lifecycle.
public class DebugPhaseListener implements PhaseListener{
    
    @Inject Logger log;

    @Override
    public void afterPhase(PhaseEvent event) {
       log.info(" afer phase @"+ event.getPhaseId());
    }

    @Override
    public void beforePhase(PhaseEvent event) {
       log.info( " before phase @"+ event.getPhaseId());
    }

    @Override
    public PhaseId getPhaseId() {
       return PhaseId.ANY_PHASE;
    }
    
}
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.
And many features I expected are not provided in JSF 2.2, such as bridge the JSF system events to CDI events, expose the JSF FacesContext related resources as CDI beans and allow you inject them directly instead of programmatic code.
We have to wait another 4 years?

评论

此博客中的热门博文

Create a restful application with AngularJS and Zend 2 framework

Create a restful application with AngularJS and Zend 2 framework This example application uses AngularJS/Bootstrap as frontend and Zend2 Framework as REST API producer. The backend code This backend code reuses the database scheme and codes of the official Zend Tutorial, and REST API support is also from the Zend community. Getting Started with Zend Framework 2 Getting Started with REST and Zend Framework 2 Zend2 provides a   AbstractRestfulController   for RESR API producing. class AlbumController extends AbstractRestfulController { public function getList() { $results = $this->getAlbumTable()->fetchAll(); $data = array(); foreach ($results as $result) { $data[] = $result; } return new JsonModel(array( 'data' => $data) ); } public function get($id) { $album = $this->getAlbumTable()->getAlbum($id); return new JsonModel(array("data" =>

JPA 2.1: Attribute Converter

JPA 2.1: Attribute Converter If you are using Hibernate, and want a customized type is supported in your Entity class, you could have to write a custom Hibernate Type. JPA 2.1 brings a new feature named attribute converter, which can help you convert your custom class type to JPA supported type. Create an Entity Reuse the   Post   entity class as example. @Entity @Table(name="POSTS") public class Post implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="ID") private Long id; @Column(name="TITLE") private String title; @Column(name="BODY") private String body; @Temporal(javax.persistence.TemporalType.DATE) @Column(name="CREATED") private Date created; @Column(name="TAGS") private List<String> tags=new ArrayList<>(); } Create an attribute convert

Auditing with Hibernate Envers

Auditing with Hibernate Envers The approaches provided in JPA lifecyle hook and Spring Data auditing only track the creation and last modification info of an Entity, but all the modification history are not tracked. Hibernate Envers fills the blank table. Since Hibernate 3.5, Envers is part of Hibernate core project. Configuration Configure Hibernate Envers in your project is very simple, just need to add   hibernate-envers   as project dependency. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-envers</artifactId> </dependency> Done. No need extra Event listeners configuration as the early version. Basic Usage Hibernate Envers provides a simple   @Audited   annotation, you can place it on an Entity class or property of an Entity. @Audited private String description; If   @Audited   annotation is placed on a property, this property can be tracked. @Entity @Audited public class Signup implements Serializa