<?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">
<bean id="car" class="com.hgh.spring.factorymethod.StaticFactoryMethod" factory-method="getCar">
<constructor-arg value="bmw"></constructor-arg>
</bean>
<bean id="InstanceFactoryMethod" class="com.hgh.spring.factorymethod.InstanceFactoryMethod"></bean>
<bean id="car2" factory-bean="InstanceFactoryMethod" factory-method="getCar">
<constructor-arg value="bmw"></constructor-arg>
</bean>
</beans>
package com.hgh.spring.factorymethod;
import java.util.HashMap;
import java.util.Map;
public class StaticFactoryMethod {
public static Map<String, Car> cars;
static{
cars = new HashMap<String, Car>();
cars.put("audi", new Car("audi", 1200.00, 200));
cars.put("bmw", new Car("bmw", 1300.00, 300));
}
public static Car getCar(String name){
return cars.get(name);
}
}
package com.hgh.spring.factorymethod;
import java.util.HashMap;
import java.util.Map;
public class InstanceFactoryMethod {
public static Map<String, Car> cars;
public InstanceFactoryMethod(){
cars = new HashMap<String, Car>();
cars.put("audi", new Car("audi", 1200.00, 200));
cars.put("bmw", new Car("bmw", 1300.00, 300));
}
public Car getCar(String name){
return cars.get(name);
}
}