Summary of how SpringBoot registers components with containers

Keywords: Java Spring Spring Boot

Catalogue of Series Articles

Spring Boot is a completely new open source framework based on Spring. It has all the best features of Spring, and is simpler to use, more versatile, and more stable and robust in performance. Spring Boot provides a number of out-of-the-box dependent modules, such as spring-boot-starter-redis, spring-boot-starter-data-mongodb, and spring-boot-starter-data-elastic search. These dependent modules provide a large number of auto-configurations for Spring Boot applications, enabling Spring Boot applications to run with only a very small number of configurations or even zero configurations, freeing developers from Spring's Configuration Hell and focusing more on the development of business logic, i.e., conventions are greater than configurations.


Objects, classes, packages, modules, components, containers, frames, all of which share one common feature: accommodation.


  stay java In the world,Objects are static and dynamic attributes that correspond to things through attributes and methods.


  An abstract concept used to describe objects of the same type.

Relationships between objects and classes:

  Classes are abstractions of a group of objects that share common attribute names and behaviors, while objects are real examples of classes.


  Components are also abstract concepts,It can be understood that a combination of classes that conform to a certain specification constitutes a component. He can provide certain functions.
  J2EE For what servlet,jsp, javabean,ejb Are components. But in fact they are all classes with their special rules.

Relationships between components and classes: Components consist of a combination of classes that conform to a specification.


 Containers are also called component containers,Component container is a special kind of component that can contain other components. We can put components in component containers.
 Conversely, if a component is not a component container, it cannot contain other components.
 Component containers are also components, so one component container can be placed in another. 
 The emergence of component containers complicates things. We can put components in component containers,You can also place component containers in another component container, which results in a hierarchical component structure.
 We can think of ordinary components as eggs and component containers as baskets.
 that,Eggs can be placed in small baskets, and small baskets and other eggs can be placed in large baskets. So there can be eggs in the basket, and there can be other baskets.

At the request of blogger, this article mainly describes the near infrared spectral qualitative analysis modeling method of SVM.

1. Four ways to register components

The registration of components is the implementation and reflection of classes. Only the components in the container can have the powerful functions provided by SpringBoot. There are four common ways to register components.

  • 1. Package Scan + Component Label (@Component, @Service, @Controller, @Repository, mostly self-written classes)
  • 2, @Bean [components in imported third-party packages]
  • 3. @Import [Quickly import a component into a container]
  •      1,Import(Class name),This component is automatically registered in the container. id The default is the full name of the component
  •      2,ImportSelector: Returns an array of the full class names of the components that need to be imported
  •      3,ImportBeanDefinitionRegistrar: Manual Registration bean
  • 4. Use FactoryBean provided by Spring
  •      1,Factory is acquired by default bean call getObject Created Objects
  •      2,To get to bean Itself, need to give id Add one before&Identification
  • @Conditional({Condition}): Register bean s in containers according to certain criteria

2. Package Scan + Component Label Notes

1. Configure the class to indicate the scope of the annotation scan

//Tell Spring that this is a configuration class
//Annotate the scope scanned, similar to context:component-scan of the spring configuration file

public class MainConfig {


2. Notes

//Identify the presentation layer
public class BookController {

3. @bean import (the most common method)

For a create person class

public class User {

    private String name;
    private Integer age;

    private Pet pet;

    public User(String name,Integer age){ = name;
        this.age = age;

Register with @bean

@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //Tell SpringBoot that this is a configuration class==configuration file
//@ConditionalOnBean(name = "tom")
@ConditionalOnMissingBean(name = "tom")
//1. Turn on Car Configuration Binding
//2. Automatically register this Car component into the container
public class MyConfig {

     * Full:Externally, regardless of how many times this component registration method is invoked in the configuration class to obtain a single instance object from a previously registered container
     * @return

    @Bean //Add a component to the container. Use the method name as the id of the component. The return type is the component type. The return value is the instance of the component in the container
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        //user components depend on Pet components
        return zhangsan;

    public Pet tomcatPet(){
        return new Pet("tomcat");


Check to see if registration was successful

        ConfigurableApplicationContext run =, args);

        boolean user01 = run.containsBean("user01");
        System.out.println("In container user01 Components:"+user01);

Note that 1:@Bean adds components to the container, defaulting to the method name as the ID of the component, the return type is the component type, and the return value is the instance of the component in the container. If you don't want to default, set the ID of the component in @Bean ("xxxx").
Note that 2:@Configuration(proxyBeanMethods = false or true)//This property does not determine whether the components in the container are singular or multiple. If he is true, he is a singleton. If false, but not many. When he is true, we always get the same object in the container, even if we call the method to create the object. But when there are multiple instances, if we call the method to create the object, that's not the same. But if we use the getBeans method to get it, it's still singular, and we get the same object. Default to true.

4. @Conditional({Condition}) Registration

Typically used for component dependencies, components that depend on @ConditionalOnBean(name = "tom") are registered only if a component registered exists.

 * 4,@Import({User.class, DBHelper.class})
 *      The name of the default component, which automatically creates these two types of components in the container, is the full class name

@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //Tell SpringBoot that this is a configuration class==configuration file
public class MyConfig {

Posted by The Midnighter on Wed, 01 Dec 2021 20:37:33 -0800