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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2020vue面试题及答案_人际关系面试题及答案

    2020vue面试题及答案_人际关系面试题及答案1、虚拟DOM中key的作用:key是虚拟DOM对象的标识,当状态中的数据发生变化时,Vue会根据【新数据】生成【新的虚拟DOM】,随后Vue进行【新虚拟DOM】的差异比较,比较规则如下:2、key的对比规则:1、旧虚拟DOM中找到了与新虚拟DOM相同的key:若虚拟DOM中内容没变,直接使用之前的真实DOM若虚拟DOM中内容变了,则生成新的真实DOM,随后替换掉页面中之前的真实DOM………

    2025年8月31日
    4
  • sqlite如何安装_安装路由器的详细步骤

    sqlite如何安装_安装路由器的详细步骤1.安装sudoapt-getinstallsqlite32.可执行程序sqlite3在/bin目录下,使用sqlite3+空格+数据库文件,可打开数据库3.使用.database查看已连接的数据库会话4.使用.tables查看数据库中的表5.执行SQL进行增删查改Linux下的SQLite数据库查看工具:SQLiteBrowser安装:sudoaptinstallsqlitebrowser-ywindows下的SQLite数据库查看工具:SQLi…

    2025年9月7日
    4
  • JavaScript getElementById()方法介绍

    JavaScript getElementById()方法介绍getElementById()根据元素的id属性获取一个元素节点对象getElementById()可以根据元素的id属性获取一个元素节点对象,该方法所有浏览器均可使用返回带有指定ID的元素,如下图:我们可以通过innerHTML属性(获取或者设置元素的内容(包含HTML标签解析)),如下图:我们可以通过innerText属性(获取或者设置元素的内容…

    2022年7月15日
    18
  • python判断文件后缀_Python 判断文件后缀是否被篡改

    python判断文件后缀_Python 判断文件后缀是否被篡改自己用Python写了个对文件后缀判断的脚本,目前支持的文件类型还不是很多,还有待完善。支持MicrosoftOffice(.pptx.docx.xlsx)Pyhton版本为3.6#!usr/bin/envpython#-*-coding:UTF-8-*-#@Time:2018/7/1015:16#@Author:gumguiimportstructimportos,sy…

    2025年11月22日
    2
  • 计算机清理垃圾代码,你也可以写代码系列,一键清除系统垃圾文件的代码(超简单)-清除垃圾文件…

    计算机清理垃圾代码,你也可以写代码系列,一键清除系统垃圾文件的代码(超简单)-清除垃圾文件…电脑使用久了,系统或者软件就会产生大量的日志文件、临时文件等垃圾文件。这些垃圾文件日积月累,不仅会大量占用磁盘空间,也会严重拖慢系统运行速度。所以定时清理垃圾文件十分有必要。我们可以手动删除,也可以借助本文提供的批处理自动删除。1,创建一个清除垃圾的.bat文件(1)在桌面上单击鼠标右键,选择“新建”选择“文本文档”(2)将新建的文件改名为“垃圾文件清除.bat”(注意.txt后缀要记得删掉)…

    2022年6月18日
    120
  • 阿里云MQTT服务器搭建与测试(全图文,非常详细)「建议收藏」

    阿里云MQTT服务器搭建与测试(全图文,非常详细)「建议收藏」阿里云MQTT服务器搭建与测试一、MQTT概念二、阿里云MQTT服务器搭建1阿里云平台注册及认证2添加平台2创建产品与设备获取MQTT连接相关信息三、MQTT.fx测试1MQTT.fx下载及安装2配置登录信息3从MQTT.fx上报数据到阿里云服务器4阿里云下发数据到MQTT.fx一、MQTT概念MQTT(MessageQueuingTelemetryTransport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的”轻量级”通讯协议,该

    2022年6月9日
    34

发表回复

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

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