使用Java Bean有什么好处? [英] What is the advantage of using Java Beans?

查看:110
本文介绍了使用Java Bean有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我理解Java Beans是什么:包含no-arg构造函数的Java类是可序列化的,并用getter和setter公开它们的字段。

I believe I understand what Java Beans are: Java class(es) which contain a no-arg constructor, are serializable, and expose their fields with getters and setters.


  1. Java Bean是否必须公开其所有字段才能获得bean资格?如果不是,它是否甚至必须暴露任何

  1. Does a Java Bean have to expose all of its fields in order to qualify as a bean? If no, does it even have to expose any?

May Java Beans包含带参数和无参数的构造函数构造函数?

May Java Beans include constructors with arguments as well as a no-arg constructor?

Java Bean的目的是什么,除了符合某种编码风格?似乎有很多关于'豆豆'或'豆豆'的讨论,但我不知道为什么它们是有利的,特别是。

What is the purpose of Java Beans, other than to conform to a certain coding style? It seems there is a lot of talk about 'beans this' or 'beans that', but I don't know why they are advantageous, specifically.

我完全可以制作no-arg构造函数。可能有很多原因,如果无参数构造函数帮助编译器进行一些优化,我也不会感到惊讶。我也可以理解让你的类可序列化。即使该类从未被序列化,它可以,并且回溯可能会令人讨厌(或者在黑盒子库中不可能)。

I can totally get making the no-arg constructor. There can be a slew of reasons for it, and I wouldn't be surprised if a no-arg constructor helps the compiler do some optimizations, either. I can also understand making your class serializable. Even if the class is never serialized, it could be, and going back to do it retroactively could be annoying (or impossible in a black-boxed library).

但最令人好奇的是要求所有字段都可以通过getter和setter访问。当我需要它时,我会在我自己的工作中使用它们,但Java Beans需要它们(可能所有这些,取决于我对#1的回答)似乎很奇怪。如果它是反射的问题,那么反射是否也不容易得到字段?如果这不仅仅是设置值是一个问题,如果方法存在,反射是否不能在字段上使用getter / setter?

But most curious is the requirement to have fields all accessible via getters and setters. I do use them in my own work when I have need of them, but it seems odd that Java Beans requires them (possibly all of them, depending on my answer to #1). If it's an issue with reflection, couldn't the reflection get the fields just as easily? If it's an issue with doing more than simply setting the value, couldn't the reflection use a getter/setter over a field if the method exists?

推荐答案

JavaBean本身并不十分有趣,它只是一个符合上面列出的标准的Java类。但是,符合此标准是Java EE框架构建的支柱之一,它出现在很多地方。我怀疑当你听说JavaBeans可以做的所有伟大事情时,Enterprise JavaBeans(EJB)中提到了什么。仅供参考,下面列出了几种不同类型的EJB:

A JavaBean on its own is not terribly interesting, it's just a Java class that conforms to some standards that you listed above. However, conformance with this standard is one of the pillars on which the Java EE framework is built and it comes up in quite a few places. I suspect that when you hear about all of the great things that JavaBeans can do, what's being referred to in Enterprise JavaBeans (EJBs). FYI, there are a few different types of EJB listed below:


  1. 实体Bean

  2. 有状态会话豆类

  3. 无状态会话豆

现在有一些细节......

Some details now follow...

实体Bean

您可能希望向/从基础数据库读取/写入对象。您可以使用JDBC / SQL来执行此操作,但您也可以使用持久性框架。 Java EE规范包含一个持久性规范,您可以将该类声明为实体bean,并让Java自动生成数据库表和逻辑,以在数据库中的条目和程序中的对象之间进行映射。最初,持久性需要使用Application Server(例如Glassfish,JBoss,Geronimo等),但AFAIK,您可以在没有服务器组件的桌面应用程序中使用它。实际的实现由Eclipselink,Toplink,Hibernate等低级库提供,但Java API抽象出它们之间的任何差异。

You might want to read/write objects to/from an underlying database. You could use JDBC/SQL to do this but you could also use a persistance framework. The Java EE spec includes a spec for persistance whereby you declare your class to be an "entity bean" and have Java automatically generate database tables and logic to map between entries in your database and objects in your program. Originally, persistance was something that required the use of an Application Server (such as Glassfish, JBoss, Geronimo etc.) but AFAIK, you can do use it in desktop apps with no server component. The actual implementation is provided by a lower level library such as Eclipselink, Toplink, Hibernate etc. but the Java API abstracts away any differences between them.

有状态会话Bean

想象一下,您想要创建一个存在于单独JVM上的Java类实例。 JVM可能在同一台物理计算机上运行,​​但同样可能在通过网络进行通信的不同计算机上运行。使用Java EE应用程序服务器,您可以创建一个可以由应用程序服务器的客户端实例化的类。这些客户端可以实例化一个类似于普通对象的类,但是在对象上调用的任何方法都会在服务器上执行,并将结果传递回调用者。它基本上是面向对象的远程过程调用形式。

Imagine that you want to create an instance of a Java class which exists on separate JVM. The JVMs might be running on the same physical machine but equally, may be on separate machines communicating over a network. Using a Java EE application server, you can create a class which can be instantiated by clients of the app server. These clients can instantiate a class which will act just like a normal object but any methods that are invoked on the object get executed on the server with the results being passed back to the caller. It's basically an object oriented form of remote procedure calls.

无状态会话Bean

这是有状态会话bean的一个小变化。对于有状态bean,如果服务器有1000个客户端,那么它可能必须创建1000个bean实例并记住哪个实例属于哪个客户端。使用无状态bean,服务器会创建一个bean池,并且无需记住哪个客户端拥有哪个bean。当客户端调用方法时,服务器从池中选择一个bean并使用它,并在完成时将其返回到池中。当您希望服务器记住有关每个客户端的详细信息时,可以使用有状态会话bean,当您不需要记住客户端特定的详细信息时,您将使用无状态Bean。请注意,无状态bean可能具有状态,只是该状态对客户端不感兴趣。

This is a minor variation on stateful session beans. With stateful beans, if the server has 1000 clients then it will potentially have to create 1000 instances of the bean and remember which instance belongs to which client. With stateless beans, the server creates a pool of beans and doesn't bother to remember which client owns which bean. When a client invokes a method, the server picks a bean from the pool and uses it, returning it to the pool on completion. You use stateful session beans when you want the server to remember details about each client, you will use stateless beans when you don't need to remember client specific details. Note that the stateless beans may well have state, it's just that this state won't be of interest to the client.

这篇关于使用Java Bean有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆