Three ways to create beans

In most cases, the Spring container directly calls the constructor 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.
    Spring supports the following ways to create beans:
    1. Call the constructor to create the Bean
    2. Call the static factory method to create the Bean
   
<!--The following configuration drives Spring to call the static method staticMethod() of the factory class BeingFactory to create           
    Bean, this configuration will drive Spring to execute the following code in reflection:
    dog=BeanFactory.staticMethod("dog");
    -->
     <bean id="dog" class="BeingFactory" factory-method="staticMethod">
          <!--Configure the parameters of the static factory method-->
          <constructor-arg value="dog"/>
          <!--Drive Spring to execute the dog's setMsg() method with "I am a dog" as a parameter-->
          <property name="msg" value="I am a dog"/>
     </bean>

     The interface being is omitted here, its implementation class Dog (including the setMsg() method), and the static factory class BeingFactory (including the static method staticMethod())
    3. To call the instance factory method to create a bean
      , just change the static method in 2 to An instance method is enough (the method does not contain static), and the others are similar.
   

Guess you like

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