Spring's instance factory method and static factory method instance code
- 2020-12-18 01:50:48
- OfStack
Both the instance factory method and the static factory method of Spring can be used to instantiate bean. In this article, we will look at the relevant examples.
Static factory methods: Calling static methods directly returns an instance of Bean
package com.zhu.string.factory;
import java.util.HashMap;
import java.util.Map;
public class StaticCarFactory {
/**
* Static factory methods: Calling static methods directly can return Bean An instance of the
*
*/
private static Map<String ,Car > cars=new HashMap<String , Car>();
static{
cars.put("audi", new Car(3000, "aodi"));
cars.put("fodo", new Car(3000, "aodi"));
}
// Static factory method
public static Car getCar(String name){
return cars.get(name);
}
}
Instance factory method. The factory itself is called, and the factory's instance methods are called to return the bean instance
package com.zhu.string.factory;
import java.util.HashMap;
import java.util.Map;
public class InstanceCarFactory {
/**
* Instance factory method. The factory itself is called, and the factory's instance method is called to return bean The instance
*/
private Map<String ,Car> cars=null;
public InstanceCarFactory() {
// TODO Auto-generated constructor stub
cars=new HashMap<String, Car>();
cars.put("audi", new Car(1000,"audi"));
cars.put("dffdas", new Car(2000,"audi"));
}
public Car getCar(String brand){
return cars.get(brand);
}
}
beans-factory.xml
<span style="font-size:14px;"><?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Configuration is done using static methods bean, Note that instead of configuring static factory method instances, you configure them bean The instance
-->
<!--
class Property: The full class name that points to the static method factory-method : Refers to the name of a static method
constructor-arg: If the factory method needs to pass in parameters, use constructor-arg To go with
Set parameters
-->
<bean id="car1" factory-method="getCar"
class="com.zhu.string.factory.StaticCarFactory">
<constructor-arg value="audi"></constructor-arg>
</bean>
<!-- Configure an instance of the factory -->
<bean id="carFactory" class="com.zhu.string.factory.InstanceCarFactory">
</bean>
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean>
</beans></span>
Car. java entity class
package com.zhu.string.factory;
public class Car {
private double price;
private String brand;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
public Car(){
System.out.println("cars....constructor");
}
public Car(double price, String brand) {
super();
this.price = price;
this.brand = brand;
}
}
Main.java
package com.zhu.string.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext cx=new ClassPathXmlApplicationContext("beans-
factory.xml");
Car car1=(Car) cx.getBean("car1");
System.out.println(car1);
Car car2=(Car) cx.getBean("car2");
System.out.println(car2);
}
}
Operation results:
Car [brand=aodi, price=3000.0]
Car [brand=audi, price=1000.0]
conclusion
That's the end of this article on Spring's instance factory method and static factory method instance code, and I hope you found it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!