Spring's instance factory method and static factory method can be used to instantiate beans. In this article, we will take a look at related examples.
Static factory method: Directly calling static methods can return the instance of the bean
package com.zhu.string.factory; import java.util.HashMap; import java.util.Map; public class StaticCarFactory { /** * Static factory method: Directly call the static method to return an instance of the bean* */ 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); } }Example factory method. That is, call the factory itself, and then call the factory's instance method to return the bean instance
package com.zhu.string.factory; import java.util.HashMap; import java.util.Map; public class InstanceCarFactory { /** * Instance factory method. That is, call the factory itself, and then call the factory's instance method to return the bean 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"> <!-- Configure beans through static methods. Note that it is not configuring static factory method instances, but configuring bean instances --> <!-- class attribute: pointing to the full class name of the static method factory-method: pointing to the name of the static method constructor-arg: If the factory method needs to pass in parameters, use constructor-arg to configure the parameters --> <bean id="car1" factory-method="getCar" > <constructor-arg value="audi"></constructor-arg> </bean> <!-- Configure factory instance --> <bean id="carFactory"> </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);}} Running results:
Car [brand=aodi, price=3000.0]
Car [brand=audi, price=1000.0]
Summarize
The above is the entire content of this article about Spring's example factory method and static factory method example code. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!