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


相关推荐

  • Java 实习生(月薪 3k-5k 水平)应具备哪些知识、能力?给学弟学妹们支招

    我10年前在一家外企做Java实习生的时候,就差不多这个水平((月薪3k-5k),10多年过去了,可以稍微把目标放得大一点,比如说8k-10k,我觉得也不是不可能!在一线城市找一份靠谱的实习工作我觉得还是不难的,怕就怕有些学弟学妹盲目乐观,以为能找一份不错的实习工作,结果碰一鼻子灰,最后心灰意冷,可能就转行了。或者盲目悲观,大四了呀,学历不咋滴呀,整天恍恍惚惚的,无心学习,最后也转行了。注意注意,只要现在立刻马上开始按照我说的学习路线开始,就一点不晚。第一,IntellijIDEA、G

    2022年4月10日
    72
  • textview可复制_android长按点击

    textview可复制_android长按点击有这么一个需求,用户在浏览文本信息时希望长按信息就能弹出复制的选项方便保存或者在别的页面使用这些信息.类似的,就像长按WebView或者EditText的内容就自动弹出复制选项.这里面主要是2个特点:1,用户只能浏览文本信息而不能编辑这些文本信息;2,用户对着文本信息长时间点按可以弹出”复制”选项实现复制;网上有好多种方法可实现,也比较零散,此处做个小结,希望有所帮助.1,通过继承EditT…

    2022年9月29日
    2
  • iec104规约遥测遥信解析笔记「建议收藏」

    iec104规约遥测遥信解析笔记「建议收藏」最近在写104规约的解析暂时做一个笔记,先写一个遥信的后期再补全规约解析表如下:104规约解析 启动字符 1字节 取值 68H APCI应用规约控制信息 APDU应用规约数据单元 …

    2022年6月20日
    82
  • shell的比较运算符

    算术运算符 +-*/%表示加减乘除和取余运算+=-=*=/=同C语言中的含义位操作符>>>=表示位左右移一位操作&&=|

    2021年12月24日
    50
  • cgal使用_cgal库

    cgal使用_cgal库CGAL5.3-手动:在Windows上使用CGAL(使用VisualC++)

    2025年8月13日
    1
  • 基础野:细说无符号整数[通俗易懂]

    基础野:细说无符号整数[通俗易懂]Brief本来只打算理解JS中0.1+0.2==0.30000000000000004的原因,但发现自己对计算机的数字表示和运算十分陌生,于是只好恶补一下。本篇我们一起来探讨一下基础的基础

    2022年8月3日
    5

发表回复

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

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