Car類
spring
public class Car { private String brand; private Double price; @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", price=" + price + '}'; } public Car(String brand, Double price) { this.brand = brand; this.price = price; } get/set/toString ... } }
StaticCarFactory
ide
public class StaticCarFactory { private static Map<String ,Car> cars = new HashMap<String,Car>(); static { cars.put("AA",new Car("奧迪",2000000.00)); cars.put("BB",new Car("奔馳",3000000.00)); } // 靜態工廠方法 public static Car getCar(String name){ return cars.get(name); } }
Spring.xml
this
<bean id="car1" class="ashley.spring.beans.factory.StaticCarFactory" factory-method="getCar"> <constructor-arg value="AA"/> </bean>