Spring instance factory method and static factory Method Instance code, spring instance
Spring's instance factory method and static factory method can be used to instantiate bean. In this article, we will look at the relevant instances.
Static factory method: directly calling static methods can return Bean instances.
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 the Bean instance **/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. That is, the factory itself is called, and the factory instance method is 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. That is, call the factory itself, and then call the factory 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 bean instances are configured instead of static factory method instances. --> <! -- Class attribute: indicates the full class Name of the static method factory-method: indicates the name of the static method constructor-arg: if the factory method needs to input parameters, use constructor-arg to configure the parameter --> <bean id = "car1" factory-method = "getCar" class = "com. zhu. string. factory. staticCarFactory "> <constructor-arg value =" audi "> </constructor-arg> </bean> <! -- Configure the factory instance --> <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);}}
Running result:
Car [brand = aodi, price = 3000.0]
Car [brand = audi, price = 1000.0]
Summary
The above is all the content about Spring's instance factory method and static factory Method Instance code, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!