faces-config.xml 中的注释与托管 bean 声明
我正在接触 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这根本不应该这样做。 JSF 托管 bean 应该特定于 JSF 视图,并且不能被其他层/API 重用。如果您想在 JSF 和其他不了解 JSF 的层/API 之间共享一些数据,那么您应该将该数据放在它自己的类中,并使其成为 JSF 托管 bean 的属性。
因此,
您应该使用
and
这样您就可以在所有层之间共享
User
而不必担心特定于层的 API。这也称为“数据传输对象”架构模式。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
you should rather have
and
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.正如核心 JavaServer Faces(第三版) 中所述:
注释允许快速开发并减少冗余的 xml 编码。一般来说,这很大程度上取决于项目本身。
As was said in Core JavaServer Faces (Third Edition):
Annotations allow rapid development and reduce redundant xml coding. Generally, it greatly depends on the project itself.