Spring学习记录(九)---通过工厂方法配置bean

时间:2022-05-12 21:36:03

1. 使用静态工厂方法创建Bean,用到一个工厂类

例子:一个Car类,有brand和price属性。

 package com.guigu.spring.factory;

 public class Car {
private String brand;
private double price; public Car(){
}
public Car(String brand,double price){
this.brand=brand;
this.price=price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + "]";
}
}

工厂类:

 package com.guigu.spring.factory;

 import java.util.HashMap;
import java.util.Map; public class StaticCarFactory { private static Map<String,Car> cars=new HashMap<String,Car>();
//静态代码块,防止重复添加
static{
cars.put("aodi", new Car("aodi",300000));
cars.put("ford", new Car("ford",200000));
}
//静态工厂方法
public static Car getCar(String name){
return cars.get(name);
}
}

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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
class属性:指向静态工厂方法的全类名,不是bean实例的实现类
factory-method:指向静态工厂方法的名字,方法里面返回bean
constructor-arg:如果工厂方法需要传入参数,则用constructor-arg配置 -->
<bean id="car1" class="com.guigu.spring.factory" factory-method="getCar">
<constructor-arg value="aodi"></constructor-arg>
</bean> </beans>

main函数测试

 public class main {

     public static void main(String[] args) {
ApplicationContext ctx =new ClassPathXmlApplicationContext("bean-factory.xml");
Car car1=(Car) ctx.getBean("car1");
System.out.println(car1);
}
}

输出:

Spring学习记录(九)---通过工厂方法配置bean

这个过程中,spring不再自己创建bean实例,bean实例由用户自己提供的工厂方法创建。静态工厂方法,方法必须是静态的

---------------------------------------

其实我还是不懂, 调用静态方法

Spring学习记录(九)---通过工厂方法配置bean

什么时候对cars进行初始化了?单单调用静态方法并没有执行工厂类。望看观指教

--------------------------------------

2. 实例工厂方法,工厂本身是实例,要实现创建工厂对象本身

 /*
* 实例工厂方法:实例工厂的方法,即先需要创建工厂本身,再调用工厂的实例方法来返回bean的实例
* */
public class InstanceCarFactory { private Map<String,Car> cars=null; public InstanceCarFactory(){
cars = new HashMap<String, Car>();
cars.put("aodi", new Car("aodi",300000));
cars.put("ford", new Car("ford",400000));
}
//
public Car getCar(String brand){
return cars.get(brand);
}
}

xml配置

      <!--先配置工厂的实例-->
<bean id="carFactory" class="com.guigu.spring.factory"></bean>
<!--
factory-bean属性:指向实例工厂方法的bean
method:指向静态工厂方法的名字
constructor-arg:如果工厂方法需要传入参数,用constructor-arg配置
-->
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>

main函数

public class main {

    public static void main(String[] args) {
ApplicationContext ctx =new ClassPathXmlApplicationContext("bean-factory.xml");
Car car2 = (Car) ctx.getBean("car2");
System.out.println(car2);
}
}

输出:Spring学习记录(九)---通过工厂方法配置bean

这个好理解,先实例工厂,调用构造函数,然后bean调用工厂的方法。

文章为学习记录,若有错误,望指正