JavaBean 和 Spring bean 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21866571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:09:13  来源:igfitidea点击:

Difference between JavaBean and Spring bean

javaspringspring-mvcjavabeansspring-bean

提问by Arpit Shah

I am new to Spring MVC and have a little idea of the usage of java beans in Java.
What is the basic difference between a Java bean and Spring bean?

我是 Spring MVC 的新手,对 Java 中 java bean 的使用有一点了解。
Java bean 和 Spring bean 之间的基本区别是什么?

采纳答案by keya

JavaBeans:

At a basic level, JavaBeans are simply Java classes which adhere to certain coding conventions. Specifically, classes that

  • have publicdefault (no argument) constructors
  • allow access to their properties using accessor (getter and setter) methods
  • implement java.io.Serializable

JavaBeans:

在基本层面上,JavaBean 只是遵循某些编码约定的 Java 类。具体来说,类

  • 具有public默认(无参数)构造函数
  • 允许使用访问器(getter 和 setter)方法访问它们的属性
  • 实施 java.io.Serializable

Spring Beans:

A Spring bean is basically an object managed by Spring. More specifically, it is an object that is instantiated, configured and otherwise managed by a Spring Frameworkcontainer. Spring beans are defined in Spring configuration files (or, more recently, with annotations), instantiated by Spring containers, and then injected into applications.

春豆:

Spring bean 基本上是一个由 Spring 管理的对象。更具体地说,它是一个由Spring Framework容器实例化、配置和管理的对象。Spring bean 在 Spring 配置文件中定义(或者,最近,使用注解),由 Spring 容器实例化,然后注入到应用程序中。

Note that Spring beans need not always be JavaBeans. Spring beans might not implement the java.io.Serializableinterface, can have arguments in their constructors, etc.

请注意,Spring bean 不必总是 JavaBean。Spring bean 可能不实现java.io.Serializable接口,可以在其构造函数中包含参数等。

This is the very basic difference between JavaBeans and Spring beans.

这是 JavaBeans 和 Spring bean 之间非常基本的区别。

For more information, refer to the source of the above text, Shaun Abram's article JavaBeans vs Spring beans vs POJOs.

更多信息可以参考上面文字的出处,Shaun Abram 的文章JavaBeans vs Spring beans vs POJOs

回答by raghu

Spring Bean:

春豆:

A class which is developed as part of a spring application. Its life cycle which is managed by Spring Container is called as Spring Bean.

作为 spring 应用程序的一部分开发的类。它的生命周期由 Spring Container 管理,称为 Spring Bean。

回答by Hamid hamid

Java bean is a class that should follow following conventions:

Java bean 是一个应该遵循以下约定的类:

1.Must implement Serializable. 2.It should have a public no-arg constructor. 3.All properties in java bean must be private with public getters and setter methods.

1.必须实现Serializable。2.它应该有一个公共的无参数构造函数。3.java bean 中的所有属性必须是私有的,具有公共的 getter 和 setter 方法。

Spring beans are the objects that form the backbone of your application and are managed by the Spring IoC container .

Spring bean 是构成应用程序主干的对象,由 Spring IoC 容器管理。