开发者

Annotations vs managed beans declarations in faces-config.xml

I'm getting my hands on JSF 2.0 and have certain doubt about new annotation based auto-wiring (declaration of managed beans without any code in faces-config.xml).

As far as I am annotations are great and easy, but problem may come to a certain need of substituting one bean with another in a big system which, if annotations were used, will result in a need to delete certain classes (or similar dirty hack), while it could be easily fixed in faces-cofig.xml.

Please, share your experience on the matter. What should be considered more con开发者_StackOverflow社区venient and why?


but problem may come to a certain need of substituting one bean with another in a big system

This should simply not be done. A JSF managed bean should be specific to JSF view(s) and not be reused by other layers/APIs. If you want to share some data between JSF and other layers/APIs which are not aware about JSF, then you should rather put that data in its own class and make it a property of a JSF managed bean.

So, instead of

@ManagedBean
@SessionScoped
public class User {
    private Long id;
    private String username;
    private String password;
    // ...
}

you should rather have

@ManagedBean
@SessionScoped
public class UserManager {
    private User user;
    // ...
}

and

public class User {
    private Long id;
    private String username;
    private String password;
    // ...
}

This way you can just share User between all layers without worrying about layer-specific API's. This is also known as "Data Transfer Object" architectural pattern.


As was said in Core JavaServer Faces (Third Edition):

Before JSF 2.0, all beans had to be configured with XML. Nowadays, you have the choice between annotations and XML configuration. The XML configuration is rather verbose, but it can be useful if you want to configure beans at deployment time.

Annotations allow rapid development and reduce redundant xml coding. Generally, it greatly depends on the project itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜