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)
上一篇 2022年7月7日 下午5:36
下一篇 2022年7月7日 下午5:36


相关推荐

  • Idea激活码最新教程2022.2.4版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2022.2.4版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2022 2 4 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2022 2 4 成功激活

    2025年5月25日
    6
  • nanobanana教程:Nano Banana完整指南AI生成指南|Complete Nano Banana Guide风格创作

    nanobanana教程:Nano Banana完整指南AI生成指南|Complete Nano Banana Guide风格创作

    2026年3月15日
    2
  • 五大常用算法之五:分支限界法

    一、基本描述类似于回溯法,也是一种在问题的解空间树T上搜索问题解的算法。但在一般情况下,分支限界法与回溯法的求解目标不同。回溯法的求解目标是找出T中满足约束条件的所有解,而分支限界法的求解目标则是找

    2021年12月25日
    39
  • linux格式化硬盘fat32,linux格式化硬盘教程 linux格式化硬盘教程是什么

    linux格式化硬盘fat32,linux格式化硬盘教程 linux格式化硬盘教程是什么1 查看硬盘 点击电脑左下角的箭头位置标志 然后选择启动终端 不同的桌面环境方法进入终端不太一样 这个根据自己使用的桌面而定 2 在终端上面输入命令 sudofdisk l 回车 3 然后找到 dev sda dev sdb 这样子的英文 这里电脑上面的硬盘 其中 sda 则是第一个硬盘 sdb 是第二个硬盘 4 格式化硬盘分区 在终端输入命令 其中 sudomkfs t 理解为固定格式即可 ntf

    2026年3月18日
    2
  • keil5使用技巧

    keil5使用技巧文章目录前言1、关闭其他文件2、keil注释如何不乱码4、每段程序后都要空行5、添加头文件6、开启和关闭工程列表框7、找到库函数总结前言1、关闭其他文件2、keil注释如何不乱码4、每段程序后都要空行5、添加头文件6、开启和关闭工程列表框7、找到库函数方法就是打开一个.h文件拖到最后→看到如下字样的,就是库函数了/**@defgroupGPIO_Exported_Functions@{*/例如:找EXTI的库函数打开exti.h文件,拖到最后,这些就是EXTI

    2022年5月23日
    64
  • prefetch 和preload_preload和prefetch

    prefetch 和preload_preload和prefetch1 preload 和 prefetch 的出现为我们提供了可以更加细粒度地控制浏览器加载资源的方法 2 preload link 标签的 preload 是一种声明式的资源获取请求方式 用于提前加载一些需要的依赖 并且不会影响页面的 onload 事件 使用方式如下 css 加载后立即生效其中 rel 属性值为 preload as 属性用于规定资源的类型 并根据资源类型设置 Accep 请求头 以便能够使用正常

    2026年3月20日
    1

发表回复

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

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