4

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 convenient and why?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Denys S.
  • 5,880
  • 12
  • 40
  • 63

2 Answers2

3

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.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • thank you very much for your answers! do you imply here that annotations is the preferred way to go, or simply that both are equally good approaches, even for the most complicated scenarios? I kind of like annotaitons, but want to avoid switching as much as possible – fr_andres Nov 09 '17 at 19:40
  • I should have been more specific: with complicated I meant complex dataflow in the app itself (excluding deployment-specific settings), as complicated as it can get without having to override annotations with the xml – fr_andres Nov 09 '17 at 19:53
  • 1
    Go with annotations. That said, I'd like to make sure that you're aware that `javax.faces.bean.*` annotations are deprecated since JSF 2.3. See also https://stackoverflow.com/q/4347374 – BalusC Nov 09 '17 at 19:55
  • Thanks again for the valuable advice, and the heads up! yes, Oracle's EE7 tutorial already goes the CDI way, but I'll be weary of that. Also, in [16.3](https://docs.oracle.com/javaee/7/tutorial/jsf-configure003.htm#CHDGFCJF) they give a further reason for annotations in the context of face flows: they *place the code closer to the rest of the flow code and enable you to modularize the flows* which I presume is a big pro – fr_andres Nov 09 '17 at 23:43
  • 1
    Java EE 8 has been out since barely 2 months: https://javaee.github.io/tutorial/toc.html – BalusC Nov 10 '17 at 08:20
2

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.

Community
  • 1
  • 1
Denys S.
  • 5,880
  • 12
  • 40
  • 63