java bean有什么好处,使用Java Bean有什么好处?

java bean有什么好处,使用Java Bean有什么好处?IbelieveIunderstandwhatJavaBeansare:Javaclass(es)whichcontainano-argconstructor,areserializable,andexposetheirfieldswithgettersandsetters.DoesaJavaBeanhavetoexposeallof…

大家好,又见面了,我是你们的朋友全栈君。

java bean有什么好处,使用Java Bean有什么好处?

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.

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 include constructors with arguments as well as a no-arg constructor?

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.

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).

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?

解决方案

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:

Entity Beans

Stateful Session Beans

Stateless Session Beans

Some details now follow…

Entity Beans

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.

Stateful Session Beans

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.

Stateless Session Beans

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.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/156626.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 青蛙过河谁先过_python knn算法实现

    青蛙过河谁先过_python knn算法实现一只青蛙想要过河。 假定河流被等分为若干个单元格,并且在每一个单元格内都有可能放有一块石子(也有可能没有)。 青蛙可以跳上石子,但是不可以跳入水中。给你石子的位置列表 stones(用单元格序号 升序 表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一块石子上)。开始时, 青蛙默认已站在第一块石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格 1 跳至单元格 2 )。如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k – 1、k 或 k + 1 个单位。 另请注意

    2022年8月9日
    4
  • wing是什么_分段计价的数学题

    wing是什么_分段计价的数学题给定一个由 n 行数字组成的数字梯形如下图所示。梯形的第一行有 m 个数字。从梯形的顶部的 m 个数字开始,在每个数字处可以沿左下或右下方向移动,形成一条从梯形的顶至底的路径。规则 1:从梯形的顶至底的 m 条路径互不相交。规则 2:从梯形的顶至底的 m 条路径仅在数字结点处相交。规则 3:从梯形的顶至底的 m 条路径允许在数字结点相交或边相交。对于给定的数字梯形,分别按照规则 1,规则 2,和规则 3 计算出从梯形的顶至底的 m 条路径,使这 m 条路径经过的数字总和最大。输入格式第 1

    2022年8月11日
    6
  • 测试 windows live writer

    测试 windows live writer

    2021年5月8日
    117
  • linux关闭端口进程命令,linux关闭端口命令

    linux关闭端口进程命令,linux关闭端口命令在linux中我们可以通过命令来启动和关闭端口,那么具体是哪个命令呢?下面由学习啦小编为大家整理了linux关闭端口的相关命令,希望对大家有所帮助。linux关闭端口命令前提:首先你必须知道,端口不是独立存在的,它是依附于进程的。某个进程开启,那么它对应的端口就开启了,进程关闭,则该端口也就关闭了。下次若某个进程再次开启,则相应的端口也再次开启。而不要纯粹的理解为关闭掉某个端口,不过可以禁用某个端…

    2022年7月20日
    46
  • opc服务器网站,OPC 服务器[通俗易懂]

    opc服务器网站,OPC 服务器[通俗易懂]OPC服务器OPC服务器,是指按照OPC基金组织规定的OPC规范群开发的软件驱动。OPC服务器作为中间媒介负责从数据源读取数据再跟另外一端的客户端通信。在OPC客户端/服务器的结构图中,通信的发起端是,也只能是OPC客户端。客户端和服务器的对话是双向的,也就是说,客户端既可以从服务器读出也可以向服务器写入。TOPC基金会定义了四种不同类型的OPC服务器。他们分别是:OPC数据访问服务器…

    2022年6月20日
    31
  • 目前主流的Eclipse版本是哪个版本?面向哪个版本的JDK?_JDK版本

    目前主流的Eclipse版本是哪个版本?面向哪个版本的JDK?_JDK版本版本代号平台版本主要版本发行日期SR1发行日期SR2发行日期需要的JDK最低版本Callisto3.22006年6月26日N/AN/AJDK1.4Europa3.32007年6月27日2007年9月28日2008年2月29日JDK1.5Ganymede3.42008年6月25日2008年9月24日2009年2月25日JDK1.5Galileo3.52009年6月24日2009年9月25日2010年…

    2022年9月23日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号