There are three ways to create bean objects in Spring

After studying spring, I learned that spring provides us with three ways to create bean s through ioc (inversion of control).

1. What is IOC?

The dependency relationship between objects is created by the container. The relationship between objects is originally created and maintained by our developers. After we use the Spring framework, the relationship between objects is created and maintained by the container. Let the container do what developers do. This is control inversion. BeanFactory interface is the core interface of Spring Ioc container.

2. Three ways to create bean s

1. Create by default parameterless structure

Entity class object User:

package com.atguigu.spring;

/**
 * @author wuhuai
 * @program TestBean
 * @description Test class User
 * @create 2021-08-12 19:06
 **/
public class User {
    private String name;
    private String sex;

    public User() {
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public User(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

}

xml configuration:

<!--It is created by parameterless construction by default-->
    <bean id="user" class="com.atguigu.spring.User"/>

Test:

/**
 * @author wuhuai
 * @program test
 * @description test
 * @create 2021-08-12 19:11
 **/
public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
    }
}

Output result: com atguigu. spring. User@305fd85d  

2. Create by static factory

Create factory class:

/**
 * @author wuhuai
 * @program ch-01-SpringMVCHelloWorld
 * @description Create bean s through static factory methods
 * @create 2021-08-12 19:24
 **/
public class UserStaticFactory {
    public static User userFactory(){
        return new User();
    }
}

xml configuration:

<!--Create by static factory,among factory-method It means creating User Object's static factory method-->
  <bean id="staticFactory" class="com.atguigu.spring.UserStaticFactory" factory-method="userFactory"/>

Test:

/**
 * @author wuhuai
 * @program test
 * @description test
 * @create 2021-08-12 19:11
 **/
public class Test {
    public static void main(String[] args) {
             ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        User user =  context.getBean("staticFactory",User.class);
        System.out.println(user);
    }
}

Output result: com atguigu. spring. User@bd8db5a

3. Create by instance factory

Create factory class:

/**
 * @author wuhuai
 * @program ch-01-SpringMVCHelloWorld
 * @description Using the method of example chemical plant
 * @create 2021-08-12 19:38
 **/
public class UserFactory {
    public User  factory(){
        return new User();
    }
}

Configuration xml:

<!--Create an instance factory object first by creating an instance factory-->
    <bean id="userFactory" class="com.atguigu.spring.UserFactory"/>
<!--   Factory method creation for introducing factory objects User object-->
    <bean id="user1" factory-bean="userFactory" factory-method="factory"/>

Test:

/**
 * @author wuhuai
 * @program test
 * @description test
 * @create 2021-08-12 19:11
 **/
public class Test {
    public static void main(String[] args) {
             ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        User user =  context.getBean("user1",User.class);
        System.out.println(user);
    }
}

Output result: com atguigu. spring. User@2f943d71

3. Three usage scenarios

1. Nonparametric structure

The default parameterless construction method is used to create the class. Its essence is to give the class to the Spring factory management, and the Spring factory pattern (BeanFactory) helps us maintain and create the class. If it is a parametric construction method, you can also pass in the corresponding initialization parameters through XML configuration, which is also the most used in development.

2. Static factory

The essence of static factory creation is to give classes to our own static factory management. Spring only helps us call the method of static factory to create instances, and the process of creating instances is implemented by our own static factory. In the actual development process, we often need to use the classes provided to us by the third-party jar package, This class has no construction method, but is created through the static factory provided by the third-party package. This is the time. If we want to hand over this class in the third-party jar to spring for management, we can use the static factory provided by spring to create the instance configuration.

3. Example factory

The essence of creating an instance factory is to hand over the factory class of creating an instance to Spring for management. At the same time, the process of creating an instance by calling the method of the factory class is also handed over to Spring for management. See, the process of creating an instance is also implemented in the instance factory configured by ourselves. In the actual development process, for example, Spring integrates Hibernate in this way. However, for factory classes that have not been integrated with Spring, we generally manage them with our own code.

Keywords: Spring

Added by mistjulia on Sat, 25 Dec 2021 22:53:37 +0200