Spring factory method

Keywords: Java Spring

Spring factory method

IOC is a typical factory pattern. Now let's learn how to use the factory pattern to create bean s.

Note: before learning this part, please review or supplement the background knowledge of factory mode in design mode by yourself. You should know that when creating an object, using factory mode is not unnecessary, but a very necessary operation for object initialization. If you don't understand this, you will feel that this operation is redundant and incomprehensible when reading this article.

Cartoon: the "factory mode" of design mode
IoC can create bean s through patterns in two ways:

  1. Static factory method
  2. Example factory method

1. Static plant method

Step 1: create a Car entity class;

package com.trainingl.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Car {
    private int num;
    private String brand;
}

Step 2: create static factory class and static factory method;

package com.trainingl.Factory;

import com.trainingl.entity.Car;

import java.util.HashMap;
import java.util.Map;

public class StaticCarFactory {
    private static Map<Integer,Car> cars;

    static {
        cars = new HashMap<Integer, Car>();
        cars.put(1,new Car(1,"audi"));
        cars.put(2,new Car(2,"Alto"));
        cars.put(3,new Car(3,"Benz"));
        cars.put(4,new Car(4,"bmw"));
    }

    public static Car getCar(int num){
        return cars.get(num);
    }
}

Step 3: configure the static factory in spring.xml;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	   http://www.springframework.org/schema/context
	   http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
    <!--Configure static factory creation Car object-->
    <bean id="car1" class="com.trainingl.Factory.StaticCarFactory" factory-method="getCar">
        <constructor-arg value="1"></constructor-arg>
    </bean>
</beans>
  1. Factory method refers to the static method of the class;
  2. The value attribute of constructor arg is the parameter passed by calling the static method;

Step 4: directly obtain the car1 object in the test class;

package com.trainingl.test;

import com.trainingl.entity.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Car car = (Car)applicationContext.getBean("car1");
        System.out.println(car);
    }
}

2. Example factory method

Step 1: create an instance factory class and factory method;

package com.trainingl.Factory;

import com.trainingl.entity.Car;

import java.util.HashMap;
import java.util.Map;

public class InstanceCarFactory {
    private Map<Integer, Car> carMap;
    public InstanceCarFactory(){
        carMap = new HashMap<Integer, Car>();
        carMap.put(1,new Car(1,"Honda"));
        carMap.put(2,new Car(2,"audi"));
        carMap.put(3,new Car(3,"bmw"));
    }
    public Car getCar(Integer num){
        return carMap.get(num);
    }
}

Step 2: configure bean s in spring.xml;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	   http://www.springframework.org/schema/context
	   http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
    
    <!--Configure instance factory object-->
    <bean id="carFactory" class="com.trainingl.Factory.InstanceCarFactory"></bean>
    
    <!--Create from instance factory object car object-->
    <bean id="car2" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="2"></constructor-arg>
    </bean>
    
</beans>
  1. Factory bean points to the instantiated factory object;
  2. Factory method refers to the non static method of the instance factory object;
  3. The value attribute of constructor arg is the parameter passed by calling the non static method;

Step 3: directly obtain the car2 object in the test class;

package com.trainingl.test;

import com.trainingl.entity.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Car car = (Car)applicationContext.getBean("car2");
        System.out.println(car);
    }
}

Compare the differences between the two methods:

  1. The Car object is created in the way of static factory method, and the instance factory object is not required. Because the static method of static factory can be called without creating an object, spring.xml only needs to configure a Car bean, not a factory bean.

  2. To create a car object using the instance factory method, you must first instance the chemical factory object. Because the non static method to be called must be called through the object and cannot be called directly through the class, you need to configure the factory bean first and then the car bean in spring.xml.

The difference between the two methods is essentially the difference between static method and non static method invocation.

Posted by kylecooper on Tue, 07 Sep 2021 13:37:50 -0700