Spring has many concepts. One of the most basic concepts is bean. What is Spring bean?

Beans are one of the two most central concepts in the Spring framework (the other is section-oriented programming AOP).

A proper understanding of beans is critical to mastering and using the Spring framework effectively.

Unfortunately, there are countless articles on the Internet without a simple and clear explanation.

So what exactly is Spring Bean?

Let’s illustrate this concept in a graphic way.

1 definition

Spring’s official documentation explains beans as follows:

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.

Translation:

In Spring, the objects that make up the backbone of an application and are managed by the Spring IoC container are called beans. A bean is an object instantiated, assembled, and managed by the Spring IoC container.

The concept is simple and we extract key information:

  1. Beans are objects, one or more of which are unlimited
  2. Beans are managed by something in Spring called IoC
  3. Our application is made up of beans

Nos. 1 and 3 are easy to understand. What is IoC?

2 Inversion of control (IoC)

Inversion of Control (IoC)

Inversion of control implements loose coupling between objects through dependency injection (DI).

When the program runs, the dependent object is dynamically generated and injected into the dependent object by the [auxiliary program], and the use relationship between the two is dynamically bound.

The Spring IoC container is one such helper that takes care of object generation and dependency injection, leaving it to us to use later.

In a nutshell: IoC is the process by which an object defines its dependencies without creating them.

Here we can subdivide it into two points.

2.1 Private Properties Save dependencies

Point 1: Store dependent objects with private properties and can only be passed in as constructor arguments,

The constructor can take a factory method, a property of the save class object, or a factory method return value.

Suppose we have a Computer class:

public class Computer { private String cpu; // CPU type private int ram; Public Computer(String CPU, int RAM) {this. CPU = CPU; this.ram = ram; }}Copy the code

We have another Person class that relies on the Computer class, which is IoC compliant:

public class Person { private Computer computer; public Person(Computer computer) { this.computer = computer; }}Copy the code

The practices not in line with IoC are as follows:

Public class Person {private Computer = new Computer("AMD", 3); } public class Person {private Computer Computer; public void init(Computer computer) { this.computer = computer; }Copy the code

2.2 Let Spring control the class building process

Point 2: Instead of new, let Spring control the new process.

In Spring, we don’t really need a new class. Spring does that.

When Spring starts, it instantiates the required classes as objects, and if dependencies are needed, it instantiates the dependencies first and then the current class.

Because dependencies must be passed in through the constructor function, the current class receives and stores all dependent objects when instantiated.

This step is known as dependency injection.

2.3 This is IoC

In Spring, the instantiation of classes, the instantiation of dependencies, and the passing in of dependencies are all controlled by the Spring Bean container.

Instead of instantiating objects in the new way, passing in dependencies through non-constructor methods, and so on.

The actual control has been handed over to the program, not the programmer, so it’s called inversion of control.

3 the Bean?

As for beans, there are several concepts.

  • Concept 1: The Bean container, or Spring IOC container, is used to manage objects and dependencies, as well as the injection of dependencies.
  • Concept 2: A bean is a Java object, a class written according to the bean specification and an object generated by the bean container.
  • Concept 3: Bean specification.

The bean specification is as follows:

  1. All properties are private
  2. Provide a default constructor
  3. Provide getters and setters
  4. Implement the Serializable interface

Reference article:

  1. Spring Dependency Injection
  2. Spring IoC, Spring Bean Example Tutorial
  3. The IoC Container – Spring docs
  4. What is a Spring Bean?
  5. What in the world are Spring beans?
  6. How to understand Bean in Spring?
  7. What is a Java bean concept?
  8. Spring Bean and @bean understanding