Spring basic usage 4 - three ways to create beans

        Preface: In most cases, the Spring container calls the constructor directly through the new keyword to create a Bean instance, and the class attribute specifies the implementation class of the Bean instance. Therefore, the <bean../> element must specify the class attribute of the Bean instance, but this is not the only way to instantiate a Bean. This article introduces three ways to instantiate a Bean.

This article focuses on the following issues:

  • Call the constructor to create a Bean;
  • Call the static factory method to create a Bean;
  • Call the instance factory method to create a bean.

1. Use the constructor to create a bean

        There are two possible situations for using the constructor to create a bean. One is not to use structural injection, and Spring calls the no-parameter constructor of the bean class at the bottom to create an instance. The other is to use the constructor with corresponding parameters to create the instance when the constructor is injected. Bean.

1.1 Without construct injection

        When construction injection is not used, the bottom layer of Spring will call the parameterless constructor of the bean class to create an instance, so the bean class is required to provide a parameterless constructor. In this case the class element is required (unless inheritance is used), and the value of the class attribute is the implementation class of the Bean instance.

       When Spring uses the default constructor to create a Bean instance, Spring performs default initialization on all properties of the Bean instance, that is, the values ​​of all primitive types are initialized to 0 or false; the values ​​of all reference types are initialized to null. At the same time, Spring will first instantiate the dependent Bean instance according to the dependency determined by the configuration file, then inject the dependency into the Bean, and finally return a complete Bean instance to the program.

1.2 Using Constructive Injection

         If constructor injection is used, the configuration file is required to add <constructor-arg../> sub-elements to the <bean../> element, and each <constructor-arg../> sub-element is configured with a constructor parameter. The Spring container will use the constructor with corresponding parameters to create the Bean instance. The parameters passed in by Spring calling the constructor can be used to initialize the Bean instance, and finally return a complete Bean instance to the program.

       Note: See http://super-wangj.iteye.com/admin/blogs/2383803 for an instance of using the constructor to create a Bean instance, and I will not elaborate here.

2. Create a Bean using a static factory method

       When creating a Bean instance using a static factory method, you need to specify two properties:

  • class : The value of this attribute is the class name of the static factory class. (Spring knows which factory class to create the bean through this property)
  • factory-method : This attribute specifies a static factory method to produce Bean instances. (factory method must be static)
        Note: If the static factory method requires arguments, use the <constructor-arg../> element to pass it in. 2.1 Defining a Bean: an interface and its two implementations

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326357755&siteId=291194637