Spring静态工厂方法


Spring之静态工厂方法

通过调用静态工厂方法创建 bean:

  • 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节。
  • 要声明通过静态方法创建的 bean , 需要在 bean 的 class 属性里面指定拥有该工厂的方法的类 , 同时在 factory-method 属性里指定工厂方法的名称。最后 , 使用 元素为该方法传递方法参数。

spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--通过静态工厂方法配置bean,注意不是配置静态工厂实例 而是配置 bean实例
        class属性指向类的全类名
        factory-method 指向静态工厂方法的名字
        construtor-arg :如果静态工厂方法有参数 则使用该标签传入该value
    -->
    <bean id="car1" class="com.an.factory.StaticCarFactory" factory-method="getCar">
        <constructor-arg value="audi"/>
    </bean>
</beans>

主函数Main.java(Ioc容器)

package com.an.factory;

import com.an.things.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

静态工厂

package com.an.factory;

import com.an.things.Car;

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

public class StaticCarFactory {
    private static Map<String, Car> cars = new HashMap<>();
    
    static{
        cars.put("audi",new Car("audi","shanghai",333.0));
        cars.put("auto",new Car("auto","shanghai",233.0));
    }
    
    public static Car getCar(String name){
        return cars.get(name);
    }
}

Car.java

package com.an.things;

public class Car {
    private String brand;
    private String corp;
    private double price;
    private int maxSpeed;
    public Car(){
        System.out.println("创建了一个汽车!");
    }
    public Car(String brand, String corp, double price) {
        this.brand = brand;
        this.corp = corp;
        this.price = price;
    }
    
    public Car(String brand, String corp, int maxSpeed) {
        this.brand = brand;
        this.corp = corp;
        this.maxSpeed = maxSpeed;
    }
    
    public String getBrand() {
        return brand;
    }
    
    public void setBrand(String brand) {
        this.brand = brand;
    }
    
    public String getCorp() {
        return corp;
    }
    
    public void setCorp(String corp) {
        this.corp = corp;
    }
    
    public double getPrice() {
        return price;
    }
    
    public void setPrice(double price) {
        this.price = price;
    }
    
    public int getMaxSpeed() {
        return maxSpeed;
    }
    
    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }
    
    @Override
    public String toString() {
        return "Car{" +
            "brand='" + brand + '\'' +
            ", corp='" + corp + '\'' +
            ", price=" + price +
            ", maxSpeed=" + maxSpeed +
            '}';
    }
}

运行主函数程序输出

Car{brand='audi', corp='shanghai', price=333.0, maxSpeed=0}

文章作者: Bxan
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Bxan !
  目录