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


相关推荐

  • 读书 Android应用开发揭秘[通俗易懂]

    读书 Android应用开发揭秘[通俗易懂]一、Android基于Linux平台,由操作系统、中间件、用户界面和应用软件组成。二、特点:1、开放性2、应用程序无界限3、应用程序在平等的条件下创建4、应用程序可以轻松的嵌入网络5、应用程序可以并行运行。三、Android系统架构分为四层:应用层:java编写运行在虚拟机上的程序。应用框架层:编写应用时使用的API框架。系统运行库层:C/C++库Lin…

    2022年5月29日
  • JAVA面试基础「建议收藏」

    JAVA面试基础「建议收藏」JAVA面试部分重点内容目录JAVA面试部分重点内容五、输入输出流IO流  1.File类的常用方法?  2.说说IO流?  3.字节流的常用方法?  4.说说字符流?  5.说说缓冲流?  6.说说序列化和反序列化?五、输入输出流IO流  1.File类的常用方法?  java.io.File,使用该类的构造函数就可以创建文件对象,将硬盘中的一个具体的文件以Java对象的形式来表示。方法描述publicFile(Stringpathname)根据路径创建对象(是绝

    2022年7月7日
  • Linux系统(Centos)安装tomcat和部署Web项目

    文章结构1.准备工作2.在Linux下安装Tomcat8.03.Linux中配置tomcat的服务器启动和关闭和配置tomcat的开机启动4.给tomcat配置用户名和密码登录5.使用IDEA打包Maven托管的WEB项目6.将JavaWeb项目发布到Tomcat8.0下面并且访问展示1,准备工作①,下载Linux版本的tomcat服务器,这里根据自己的需求下载相应的版本,作者这里…

    2022年4月6日
  • 医咖会SPSS免费教程学习笔记—配对卡方检验

    医咖会SPSS免费教程学习笔记—配对卡方检验配对卡方检验(McNemar’s检验)1.需要满足的假设:(1)观测变量是二分类变量(互斥)(2)分组变量有两类(有三类及以上的用Cochran’sQ检验)2.实操分析—非参数检验—相关样本—勾选“自动比较实测数据和假设数据”—选择上方工具栏中的“字段”—将分组变量拖入“检验字段”中—选择上方工具栏中“设置”—勾选定制检验—选择“麦克尼马尔检验”—运行…

    2022年8月31日
  • P2v, V2v 实践

    P2v, V2v 实践P2V(物理机转虚拟机)p2v,就是physicalmachinetovirtualmachine,物理机转换成虚拟机,物理机有硬件和软件资源两部分,虚拟机同样也有硬件和软件资源,只是硬件是虚拟出来的。p2v是把物理机的软件资源(操作系统,数据等)迁移到虚拟机,虚拟机的物理资源(CPU、内存、磁盘等),根据现场情况分配创建。 p2v,一般会通过转换整个物理磁盘,或者某个分区成某种格式的镜像…

    2022年7月26日

发表回复

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

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