我还没有找到我能够理解的Spring bean的高级定义。我经常在Grails文档和书籍中看到它们,但我认为了解它们是什么将是有益的。那么春豆是什么呢?如何使用它们?它们与依赖注入有关吗?


当前回答

Spring bean只是由Spring容器管理的实例对象,也就是说,它们是由框架创建和连接的,并放入一个“对象袋”(容器)中,您以后可以从那里获取它们。

“连接”部分是依赖注入的全部内容,它的意思是你可以说“我将需要这个东西”,框架将遵循一些规则来为你获得适当的实例。

对于那些不习惯Spring的人来说,我认为Wikipedia Spring的文章有一个很好的描述:

Central to the Spring Framework is its inversion of control container, which provides a consistent means of configuring and managing Java objects using reflection. The container is responsible for managing object lifecycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together. Objects created by the container are also called managed objects or beans. The container can be configured by loading XML files or detecting specific Java annotations on configuration classes. These data sources contain the bean definitions which provide the information required to create the beans. Objects can be obtained by means of either dependency lookup or dependency injection. Dependency lookup is a pattern where a caller asks the container object for an object with a specific name or of a specific type. Dependency injection is a pattern where the container passes objects by name to other objects, via either constructors, properties, or factory methods.

其他回答

Spring核心技术参考文档描述了bean是什么。

根据Spring IoC容器和bean的介绍部分(其中“IoC”表示“控制反转”):

在Spring中,构成应用程序主干并由Spring IoC容器管理的对象称为bean。bean是由Spring IoC容器实例化、组装和管理的对象。否则,bean只是应用程序中的众多对象之一。bean以及它们之间的依赖关系反映在容器使用的配置元数据中。

Bean和作用域在Bean作用域部分中描述:

When you create a bean definition, you create a recipe for creating actual instances of the class defined by that bean definition. The idea that a bean definition is a recipe is important, because it means that, as with a class, you can create many object instances from a single recipe. You can control not only the various dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition but also control the scope of the objects created from a particular bean definition. This approach is powerful and flexible, because you can choose the scope of the objects you create through configuration instead of having to bake in the scope of an object at the Java class level. Beans can be defined to be deployed in one of a number of scopes.

春天有IoC容器,里面装着一袋豆子;创建、维护和删除是Spring Container的职责。 我们可以通过布线和自动布线将bean放到Spring中。 “连线”意味着我们手动将其配置到XML文件中,“自动连线”意味着我们将注释放入Java文件中,然后Spring自动扫描Java配置文件所在的根上下文,将其制作并放入Spring的包中。

这里是详细的URI,您可以从中获得有关bean的更多信息

首先让我们来了解一下春天:

Spring是一个轻量级且灵活的框架。

类比:

Java bean是将许多对象封装到单个对象(bean)中的类。“Bean”这个名称包含了这个标准,它旨在为Java创建可重用的软件组件。

Spring Bean:是一个对象,在Spring容器中创建、管理和销毁。我们可以通过元数据(xml或注释)将对象注入到Spring容器中,这称为控制反转。

类比: 让我们假设农民有一块种种子(或豆子)的农田。 在这里,农民是春天的框架,农田是春天的容器,豆子是春天的豆子,耕种是春天的加工者。

和豆类的生命周期一样,春豆也有自己的生命周期。

img源

下面是Spring中bean生命周期的序列:

Instantiate: First the spring container finds the bean’s definition from the XML file and instantiates the bean. Populate properties: Using the dependency injection, spring populates all of the properties as specified in the bean definition. Set Bean Name: If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method. Set Bean factory: If Bean implements BeanFactoryAware interface, spring passes the beanfactory to setBeanFactory() method. Pre-Initialization: Also called post process of bean. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method. Initialize beans: If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called. Post-Initialization: – If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called. Ready to use: Now the bean is ready to use by the application Destroy: If the bean implements DisposableBean, it will call the destroy() method

For a Java class to be usable as a Java bean, its setter- and getter-method names need to be as per the JavaBean guidelines (also called design patterns) for properties. If such a Java class is instantiable & manageable by the Spring IoC container, it is a Spring bean. To achieve this, the programmer wires the class as a bean definition of a suitable scope by using XML config files or annotations or a mix of both. The programmer can create new Spring beans out of existing Spring beans by wiring further by passing the latter to constructor-arguments of the former either as string-names as <idref> elements or by dependency injection (it can be recursive).

这个答案可以与我的这个SO答案一起阅读,以获得更多的背景信息。

在Spring中,那些构成应用程序主干并由Spring IoC管理的对象 容器被称为bean。bean只是一个实例化、组装等的对象 由Spring IoC容器管理;