Java 编程中的 Bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4592079/
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-10-30 07:07:00  来源:igfitidea点击:

Beans in java programming

java

提问by abson

What are beans in java programming?Is having an understanding of Struts vital for learning Spring?

java编程中的bean是什么?了解Struts对学习Spring很重要吗?

回答by javamonkey79

That depends.

那要看。

You could be talking about Spring beans, Enterprise Java Beans or some other variant.

您可能在谈论 Spring bean、Enterprise Java Beans 或其他一些变体。

The general answer is that beans are some type of generic object (or POJO perhaps) that hold information - almost think of them like their own data type. The distinction is that they typically don't have much in the way of behaviors eg, they only have: simple fields, getters, setters.

一般的答案是 bean 是某种类型的通用对象(或者可能是 POJO),它们保存信息——几乎可以将它们视为它们自己的数据类型。区别在于它们通常没有太多的行为方式,例如,它们只有:简单的字段、getter、setter。

回答by Darin Dimitrov

From Wikipedia:

来自维基百科

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. 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.

JavaBeans 是可重用的 Java 软件组件,可以在构建器工具中进行可视化操作。实际上,它们是用符合特定约定的 Java 编程语言编写的类。它们用于将许多对象封装到单个对象(bean)中,以便它们可以作为单个 bean 对象而不是多个单独的对象传递。JavaBean 是可序列化的 Java 对象,具有空构造函数,并允许使用 getter 和 setter 方法访问属性。

回答by Buhake Sindi

Java Bean is a class that conforms to the following convention:

Java Bean 是一个符合以下约定的类:

  • The class must have a public default constructor (no-argument). This allows easy instantiation within editing and activation frameworks.
  • The class properties must be accessible using get, set, is (used for boolean properties instead of get) and other methods (so-called accessor methods and mutator methods), following a standard naming-convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties.
  • The class should be serializable. This allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion independent of the VM and of the platform.
  • 该类必须具有公共默认构造函数(无参数)。这允许在编辑和激活框架内轻松实例化。
  • 类属性必须可以使用 get、set、is(用于布尔属性而不是 get)和其他方法(所谓的访问器方法和 mutator 方法),遵循标准命名约定。这允许在框架内轻松自动检查和更新 bean 状态,其中许多包括用于各种类型属性的自定义编辑器。
  • 该类应该是可序列化的。这允许应用程序和框架以独立于 VM 和平台的方式可靠地保存、存储和恢复 bean 的状态。

Source: Wikipedia.

资料来源:维基百科

回答by Bozho

A "bean" can be everything, depending on context. Thus it is roughly equivalent to an "object". (Note that it can also be viewed as equivalent to "class", although I think it's more about instances than definitions. Hence "bean class" is more often used)

根据上下文,“bean”可以是一切。因此它大致相当于一个“对象”。(请注意,它也可以被视为等同于“类”,尽管我认为它更多地是关于实例而不是定义。因此更常使用“bean 类”)

  • a spring bean is any spring-managed object
  • a javabean is an object of class the conforms to the javabean spec (properties, getters and setters)
  • enterprise java bean is a container-managed object
  • spring bean 是任何 spring 管理的对象
  • javabean 是符合 javabean 规范(属性、getter 和 setter)的类的对象
  • 企业 java bean 是一个容器管理的对象

As for your second question - no, struts and spring are rather separate technologies and neither of them requires knowing the other.

至于你的第二个问题 - 不,struts 和 spring 是相当独立的技术,它们都不需要了解对方。

回答by Mahesh

Is having an understanding of Struts vital for learning Spring? No, both follow the MVC pattern so knowing struts will help you to know spring..but otherwise there are lot of differences..in how the two work.

了解 Struts 对学习 Spring 至关重要吗?不,两者都遵循 MVC 模式,因此了解 struts 将帮助您了解 spring..但除此之外,两者的工作方式存在很多差异。