faces-config.xml 中的注释与托管 bean 声明

发布于 2024-10-18 10:46:37 字数 239 浏览 1 评论 0原文

我正在接触 JSF 2.0,并对基于新注释的自动装配(在 faces-config.xml 中没有任何代码的托管 bean 声明)有一定的疑问。

就我而言,注释是伟大而简单的,但问题可能是在一个大系统中用一个 bean 替换另一个 bean 的某种需要,如果使用注释,将导致需要删除某些类(或类似的脏黑客) ),同时它可以很容易地在 faces-cofig.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 convenient and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救赎№ 2024-10-25 10:46:37

但是问题可能会出现在大系统中用一种 Bean 替换另一种 Bean 的特定需求

这根本不应该这样做。 JSF 托管 bean 应该特定于 JSF 视图,并且不能被其他层/API 重用。如果您想在 JSF 和其他不了解 JSF 的层/API 之间共享一些数据,那么您应该将该数据放在它自己的类中,并使其成为 JSF 托管 bean 的属性。

因此,

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

您应该使用

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

and

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

这样您就可以在所有层之间共享 User 而不必担心特定于层的 API。这也称为“数据传输对象”架构模式。

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.

花桑 2024-10-25 10:46:37

正如核心 JavaServer Faces(第三版) 中所述:

在 JSF 2.0 之前,所有 bean 都必须使用 XML 进行配置。如今,你有
注释和 XML 配置之间的选择。 XML 配置相当
冗长,但如果您想在部署时配置 bean,它会很有用。

注释允许快速开发并减少冗余的 xml 编码。一般来说,这很大程度上取决于项目本身。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文