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

  • 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不关心创建对象的细节。
  • 要声明通过静态方法创建Bean,需要在Bean的class属性里指定拥有改工厂的方法的类,同时在factory-method属性里指定工厂方法的名称,最后使用元素为该方法传递方法参数
  1. // src/beans.xml
  2. <beans>
  3. <bean id="car1" class="com.lijunyang.model.StaticCarFactory"
  4. factory-method="getCar">
  5. <constructor-arg value="audi" />
  6. </bean>
  7. <!-- 配置工厂实例 -->
  8. <bean id="carFactory" class="com.lijunyang.model.InstanceCarFactory" />
  9. <!-- 通过实例工厂获取Bean -->
  10. <bean id="car2" factory-bean="carFactory" factory-method="getCar">
  11. <constructor-arg value="ford" />
  12. </bean>
  13. </beans>
  1. // src/com/lijunyang/model/StaticCarFactory.java
  2. public class StaticCarFactory {
  3. private static Map<String, Car> cars = new HashMap<String, Car>();
  4. static {
  5. cars.put("audi", new Car("audi", 300000));
  6. cars.put("ford", new Car("audi", 400000));
  7. }
  8. public static Car getCar(String name) {
  9. return cars.get(name);
  10. }
  11. }
  12. // src/com/lijunyang/model/InstanceCarFactory.java
  13. public class InstanceCarFactory {
  14. private Map<String, Car> cars = null;
  15. public InstanceCarFactory() {
  16. cars = new HashMap<String, Car>();
  17. cars.put("audi", new Car("audi", 300000));
  18. cars.put("ford", new Car("audi", 400000));
  19. }
  20. public Car getCar(String name) {
  21. return cars.get(name);
  22. }
  23. }
  24. // src/com/lijunyang/test/Main.java
  25. public class Main {
  26. public static main(String args[]) {
  27. ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml")
  28. Car car1 = (Car) ctx.getBean("car1");
  29. System.out.println(car1);
  30. Car car2 = (Car) ctx.getBean("car2");
  31. System.out.println(car2);
  32. }
  33. }

FactoryBean

  1. // org.springframework.beans.factory.FactoryBean.java具有3个方法
  2. public interface FactoryBean<T> {
  3. T getObject() throws Exception;
  4. Class<?> getObjectType();
  5. boolean isSingleton();
  6. }
  1. // src/beans.xml
  2. <beans>
  3. <!-- 通过FactoryBean 来配置Bean的实例
  4. class:指向FactoryBean的全类名
  5. property: 配置FactoryBean的属性
  6. 但实际返回的实例确实 FactoryBean的getObject()方法返回的实例!
  7. -->
  8. <bean id="car" class="com.lijunyang.model.CarFactoryBean">
  9. <property name="brand" value="BMW" />
  10. </bean>
  11. </beans>
  1. // src/com/lijunyang/model/CarFactoryBean.java
  2. import org.springframework.beans.factory.FactoryBean;
  3. public class CarFactoryBean implements FactoryBean {
  4. private String brand;
  5. public setBrand(String brand) {
  6. this.brand = brand;
  7. }
  8. // 返回Bean对象
  9. @Override
  10. public Car getObject() throws Exception {
  11. return new Car(brand, 500000);
  12. }
  13. // 返回Bean的类型
  14. @Override
  15. public Class<?> getObjectType() {
  16. return Car.class;
  17. }
  18. @Override
  19. public boolean isSingleton() {
  20. return true;
  21. }
  22. }
  23. // src/com/lijunyang/test/Main.java
  24. public class Main {
  25. public static main(String args[]) {
  26. ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml")
  27. Car car1 = (Car) ctx.getBean("car");
  28. System.out.println(car1);
  29. }
  30. }