JavaBean SpringBean是对象还是类
首先先说结论:
Bean可以理解为对象
这几天在学习Spring源码的时候,观察到底层反复的对Bean的操作,于是就去网上搜索Bean到底是什么。有说类的,有说是对象的,还有说既是类也是对象的。给我搞得很懵。其中主要迷惑的点如下:
什么是JavaBean
-
当你搜索什么是JavaBean的时候,一定会搜到如下结果:
JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool. Practically, they are classes written in the Java programming language conforming to a particular convention(JavaBeans是可重用的Java软件组件,可以在构建器工具中进行可视化操作。实际上,它们是用符合特定约定的Java编程语言编写的类)
从上文来看,你可能感觉JavaBean是一个类,其实这段话后面还有一段:
They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods.(JavaBean是可序列化的Java对象,具有空构造函数,并允许使用getter和setter方法访问属性)
In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behavior. These conventions make it possible to have tools that can use, reuse, replace, and connect JavaBeans.(为了充当JavaBean类,对象类必须遵守关于方法命名、构造和行为的某些约定。这些约定使得拥有可以使用、重用、替换和连接JavaBeans的工具成为可能。)
综合上面的描述可以得出,一个JavaBean其实本质上是一个Java对象,为了使得这个Java对象满足要求,在编写其对应的类(JavaBean Class)的时候,该类需要满足几点要求:可序列化、空参构造器、getter和setter方法。
-
下面是我搜索时得到的资料,以佐证我对JavaBean的理解
what-is-a-javabean
difference-between-dto-vo-pojo-javabeans
B站视频-什么是JavaBean
强烈推荐看一下这个视频,只有四分钟,解开了我很大的疑惑。
什么是SpringBean
-
这个就很简单,无可争议了,上官网解释
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and managed by a Spring IoC container.(bean是由SpringIOC容器实例化、组装和管理的对象。) Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
在这里清楚的写到SpringBean,就是有Spring IOC容器管理的对象!!!对象!!!对象!!!!
这样就统一起来,用一句话解释如下:
一个JavaBean是一个遵循了一定协议(可序列化、空参构造器、getter和setter方法)的Java对象、
SpringBean就是由Spring IOC容器管理的对象。
以上为我的粗浅见解,欢迎留言讨论!!!