“This is my 23rd day of the November Gwen Challenge.The final text challenge in 2021”.

preface

SpringBoot simplifies a lot of configuration files and instead uses annotations to do what was previously done through configuration files. The operation is very convenient, but also hidden some internal implementation details, in the use of time can not blindly, should know how to configure in the past Spring projects, so that we can deepen our understanding of SpringBoot

An overview of the

Adding this annotation to a class indicates that the class is a configuration class

  • Add configuration classes to the Spring project

In a traditional Spring project, we would like to introduce a configuration class, usually by defining a bean.xml and then adding components to the container via bean annotations to be injected into the Spring container.


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
       
    <bean id="config01" class="com.example.helloworlddemo.config.User">
        <property name="name" value="zhangsan"/>
        <property name="age" value="18"/>
    </bean>
    
</beans>
Copy the code
  • Add a configuration class to the SpringBoot project

In the SpringBoot project, we only need to create a configuration class and assign @configration to this class. In this case, this class is the same as bean.xml. Add components via the @bean annotation. The class is automatically detected when the project starts, and is then loaded and injected into the container, bypassing the steps of manually configuring injection and scanning the configuration file.

@Configuration // Tell SpringBoot that this is a configuration class = bean.xml
public class MyConfig {

    / * * *@BeanEquivalent to the <bean></bean> tag. * Method name as component ID * return value type Component type * Return value instance object of component in container, singleton */
    @Bean
    public User config01(a) {
        return new User("Zhang"."18"); }}Copy the code
  • Verify that the injection is successful

We can print out all the component names in the container

public static void main(String[] args) {
    // Return the IOC container object
    ConfigurableApplicationContext run = SpringApplication.run(HelloworldDemoApplication.class, args);
    // View all the components in the container
    String[] beanDefinitionNames = run.getBeanDefinitionNames();
    for(String name : beanDefinitionNames) { System.out.println(name); }}Copy the code

As you can see, what we just defined ourselves has been injected into the container

And the component instance is singleton, the same no matter how many times it is fetched.

    // Get the component
    User config01 = run.getBean("config01", User.class);
    User config02 = run.getBean("config01", User.class);
    System.out.println(config01);
    System.out.println(config02);
Copy the code

Changes in SpringBoot2.x

Compared to version 1.0, the @Configuration annotation in version 2.0 adds the new attribute proxyBeanMethods().

This property sets how to get the component instance object in the configuration component. It is simply understood as the difference between singleton and multi-instance. The default is true singleton.

  • @Configuration(proxyBeanMethods = true)

When we set it to true, We get to the configuration of the object (MyConfig) is actually a proxy object (com) example. Helloworlddemo. Config. MyConfig $$$$97588 a67 @ 632 aa1a3 EnhancerBySpringCGLIB), Every time we call this object to get an instance of a component, we check it from the container first. If there is one, we take it from the container. If there is none, we create a new one. The purpose of this is to ensure that there must be instances in the container that we need to solve the problem of component dependency. If you think about it, if one component depends on another component, if A component depends on B, when we get A there’s no B in the container then we’re going to have A null pointer problem. In short, when true, SpringBoot checks the container each time to see if the desired instance exists.

  • @Configuration(proxyBeanMethods = false)

When we set to false, return is the original object (com) example. Helloworlddemo. Config. MyConfig @ c1a4620), at this time every time we call for instance method will return a new instance object, And SpringBoot skips instance checking, so projects can start and load faster in this way, so we can set false to speed up project loading when our configuration class is just to return a component instance.

conclusion

  1. Used in configuration classes@BeanAnnotations register components to containers on methods, which are singleton by default
  2. Configuration classes are themselves components
  3. proxyBeanMethodsAgent:beanThe method of
    • FullMode:(proxyBeanMethods = true)

    In this mode, each is guaranteed@BeanMethod, the components returned by repeated calls are singleton, taken directly from the container, and created if not.

    • LiteMode:(proxyBeanMethods = false)

    In this mode: every@BeanMethod returns a newly created instance object each time it is called

Components that have dependencies must be usedFullMode default. Other Default Yes NoLitemodel