通过调用静态工厂方法创建 bean:
- 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节。
- 要声明通过静态方法创建的 bean , 需要在 bean 的 class 属性里面指定拥有该工厂的方法的类 , 同时在 factory-method 属性里指定工厂方法的名称。最后 , 使用 <constructor-arg> 元素为该方法传递方法参数。
<?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 id="car" class="com.itdoc.spring.factory.StaticFactory"
factory-method="getCar">
<constructor-arg value="Maserati"/>
</bean> </beans>
package com.itdoc.spring.factory; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 19:30
*/
public class Car { private String brand; private double 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;
} public Car(String brand, double price) {
this.brand = brand;
this.price = price;
} public Car() {
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
package com.itdoc.spring.factory; import java.util.HashMap;
import java.util.Map; /**
* 静态工厂方法
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 19:27
*/
public class StaticFactory { public static Map<String, Car> cars = new HashMap<String, Car>(); static {
cars.put("Ferrari", new Car("Ferrari", 25000000));
cars.put("Maserati", new Car("Maserati", 2870000));
} public static Car getCar(String name) {
return cars.get(name);
} }
package com.itdoc.spring.factory; import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 19:41
*/
public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("factory-beans.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car); }
}
控制台输出:
Car{brand='Maserati', price=2870000.0} |
通过调用实例工厂方法创建 bean:
- 实例工厂方法:将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时 , 只需要简单的调用该实例方法而不需要关心对象的创建细节。
- 要声明通过实例工厂方法创建的 bean
- 在 bean 的factory-bean 属性里指定拥有该工厂方法的bean。
- 在 factory-method 属性里指定该工厂方法的名称。
- 使用 <constructor-arg> 元素为工厂方法传递方法参数。
<?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 id="carFactory" class="com.itdoc.spring.factory.InstanceFactory"></bean> <bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="Ferrari"/>
</bean> </beans>
package com.itdoc.spring.factory; import java.util.HashMap;
import java.util.Map; /**
* 实例工厂方法
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 20:02
*/
public class InstanceFactory { private Map<String, Car> cars = null; public InstanceFactory() {
cars = new HashMap<String, Car>();
cars.put("Ferrari", new Car("Ferrari", 25000000));
cars.put("Maserati", new Car("Maserati", 2870000));
} public Car getCar(String name) {
return cars.get(name);
}
}
package com.itdoc.spring.factory; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-02 19:41
*/
public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("factory-beans.xml"); car = (Car) ctx.getBean("car2");
System.out.println(car); }
}
控制台输出:
Car{brand='Ferrari', price=2.5E7} |