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


当前回答

就Spring引导应用程序而言,bean只是一个Java对象,它是在应用程序启动时由Spring框架创建的。

对象的用途可以是任何东西——配置、服务、数据库连接工厂等等——Spring并不关心这些。

大多数bean依赖于其他bean才能工作,例如实体管理器可能需要一个数据库连接。Spring框架能够找出如何将bean自动连接在一起。从应用程序开发人员的角度来看,您只需声明所需的bean,它们就会“神奇地”出现在您的应用程序中,随时可以使用。

其他回答

Spring beans are just object instances that are managed by the Spring IOC container. Spring IOC container carry the Bag of Bean.Bean creation,maintain and deletion are the responsibilities of Spring Container. We can put the bean in to Spring by Wiring and Auto Wiring. Wiring mean we manually configure it into the XML file. Auto Wiring mean we put the annotations in the Java file then Spring automatically scan the root-context where java configuration file, make it and put into the bag of Spring.

Spring beans are classes. Instead of instantiating a class (using new), you get an instance as a bean cast to your class type from the application context, where the bean is what you configured in the application context configuration. This way, the whole application maintains singleton-scope instance throughout the application. All beans are initialized following their configuration order right after the application context is instantiated. Even if you don't get any beans in your application, all beans instances are already created the moment after you created the application context.

就Spring引导应用程序而言,bean只是一个Java对象,它是在应用程序启动时由Spring框架创建的。

对象的用途可以是任何东西——配置、服务、数据库连接工厂等等——Spring并不关心这些。

大多数bean依赖于其他bean才能工作,例如实体管理器可能需要一个数据库连接。Spring框架能够找出如何将bean自动连接在一起。从应用程序开发人员的角度来看,您只需声明所需的bean,它们就会“神奇地”出现在您的应用程序中,随时可以使用。

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是一个轻量级且灵活的框架。

类比:

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