In this post of Spring, We will try to give you answer to one of the common question “What is a Spring Bean?"
在这篇关于Spring,我们将尝试回答一个常见的问题“什么是 Spring Bean”
01:Introduction (简介)
Spring Bean is the key concept or backbone of the Spring Framework. Spring Bean is the object whose life-cycle managed by the Spring IoC. It is important to understand it before we work with the Spring Framework. In simple words Spring Bean is the core building block for any Spring application. My goal is to give a clear answer to a basic question “What is a Spring Bean?“. Let’s try to get an answer to this simple question.
SpringBean 是 SpringFramework 的关键概念或骨干。Spring Bean 是由 Spring IoC 管理其生命周期的对象。在使用 Spring 框架之前,了解它非常重要。简而言之,SpringBean 是任何 Spring 应用程序的核心构建块。我的目标是对一个基本问题给出一个明确的答案“什么是Spring Bean?”?”。让我们试着找到这个简单问题的答案。
02:Spring Bean Definition (SpringBean的定义)
This is the standard definition of Spring Bean from Spring documentation:
这是 Spring 文档中 Spring Bean 的标准定义:
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 otherwise managed by a Spring IoC container. Otherwise, a bean is one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
在 Spring 中,构成应用程序主干并由 Spring IoC 容器管理的对象称为 bean。Bean 是一个由 Spring IoC 容器实例化、组装和管理的对象。否则,bean 就是应用程序中的许多对象之一。Bean 以及它们之间的依赖关系反映在容器使用的配置元数据中。
This provides a complete definition of the Spring Bean, lets cover some important point in this definition to get a clear answer to our question.
这提供了一个完整的 SpringBean 定义,让我们讨论一下这个定义中的一些重要点,以得到对我们的问题的一个明确的答案。
03:Spring IOC Containers (Spring IOC 容器)
The most important concept while working on the Spring beans is the Ioc Container. Spring IoC container is the central management system of the Spring Framework. It is responsible to create, configure and manage the life cycle of these objects. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata provided in the form of either XML configurations or annotations.It allows you to express the objects that compose your application and the rich interdependencies between such objects. Let’s look at the high level workflow of the Spring Ioc container.
在处理 Spring bean 时,最重要的概念是 Ioc 容器。Spring IoC 容器是 Spring 框架的中央管理系统。它负责创建、配置和管理这些对象的生命周期。容器通过读取以 XML 配置或注释形式提供的配置元数据来获取关于要实例化、配置和组装哪些对象的指令。它允许您表达组成应用程序的对象以及这些对象之间丰富的相互依赖关系。让我们看看 Spring Ioc 容器的高级工作流。
For a better understanding, let’s take the following example:
为了更好的理解,让我们看下面的例子:
-
Customer for our online shop. (我们网上商店的顾客)
-
Orders placed by Customers on our online shop. (客户在我们的网上商店下的订单)
public class Customer{ private String firstName; private String lastName; private List orders; //getter and setter}public class Order{ private String code; private double total; private double tax; private Product product; private Customer owner; //getter and setter
}
If we are working on an application with no support for the Spring Framework, we might do the following to create an instance of both Customer and Order class:
如果我们正在开发一个不支持 Spring 框架的应用程序,我们可以执行以下操作来创建 Customer 和 Order 类的实例:
Customer customer = new Customer(); // pass any constructor argumentscustomer.setFirstName("FirstName");// fill other data in customer mmodel// Create order instance by getting all orders for a customerList orders = get_All_Orders_For_Customer(Customer);
customer.setOrders(orders);
This code will work fine with no issue, however there are few points which can make this code complex and error prone.
这段代码可以很好地工作,没有任何问题,但是有一些地方会使这段代码变得复杂和容易出错。
Developer need to create each instance and fill out dependencies (orders in our case).
开发人员需要创建每个实例并填写依赖项(在我们的例子中是订单)
For a large application with a hundred of classes, this is difficult and error prone also if we need to change in any class, we need to make sure that all dependencies are working correctly.
对于一个有100个类的大型应用程序来说,如果我们需要在任何类中进行更改,那么这都很困难,而且容易出错,我们需要确保所有的依赖项都能正常工作
We also need to take care of the sequence To handle all these cross dependencies in a large application, Spring Ioc container prove its power and flexibility. As stated above, we need to pass the class information and its dependencies to Spring container using as metadata and Spring Ioc container will manage the bean life cycle for us.
为了在大型应用程序中处理所有这些交叉依赖,Spring Ioc容器证明了它的能力和灵活性。如上所述,我们需要将类信息及其依赖项作为元数据传递给Spring容器,Spring Ioc容器将为我们管理bean的生命周期。
It will also take care of any dependencies while instating the bean (if bean B needs bean A, it will first create bean A)
它还将在实例化 bean 时处理任何依赖关系(如果 bean B 需要 bean A,它将首先创建 bean A)
04:Bean Configuration (Bean 配置)
Spring provide the following options for bean configuration:
Spring 为 bean 配置提供了以下选项:
-
Bean configuration using Java configurations. (使用 Java 配置的 Bean 配置)
-
XML declaration (XML 声明)
4.1 Java Configuration ( Java 配置)
@Component
public class Customer{
private String firstName;
private String lastName;
private List orders;
//getter and setter
}
@Component
public class Order{
private String code;
private double total;
private double tax;
private Product product;
private Customer owner;
//getter and setter
}
@Configuration
@ComponentScan(basePackages = { "com.javadevjournal"})
public class AppConfig{
@Bean
public Customer getCustomer() {
return new Customer();
}
}
05:Bean Dependencies(Bean 依赖)
One of the main feature of Spring managed beans are the dependency management. When Spring creates a bean which define dependency to another bean, the Spring Ioc container will create that bean first.It will make sure that all dependencies are in place before it create the bean. This dependency graph creation and making sure that the beans are getting created in right order is one of the most powerful feature of the Spring Ioc container and it also take away the complexity from our code. Below, I summarize the bean life cycle under Spring IoC.
Spring 托管 bean 的主要特性之一是依赖项管理。当 Spring 创建一个 bean 来定义对另一个 bean 的依赖关系时,Spring Ioc 容器将首先创建这个 bean。创建依赖关系图并确保按照正确的顺序创建 bean 是 Spring Ioc 容器最强大的特性之一,它还减少了代码的复杂性。下面,我总结了 Spring IoC 下的 bean 生命周期。
Summary (摘要)
In this post, we tried to give an answer to a basic question “What is a Spring Bean?”. We discuss few important points about Spring beans and how the life cycle of the Spring beans managed by IoC container. We briefly talk about the IoC benefits and flexibility.
在这篇文章中,我们试图回答一个基本问题“什么是Spring Bean?”.我们讨论了关于 Spring bean 的一些重要问题,以及 IoC 容器如何管理 Spring bean 的生命周期。我们简要地讨论了 IoC 的好处和灵活性。
关注我,一起提升认知!