静态工厂方法、实例工厂方法创建 Bean

通过调用静态工厂方法创建 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>

 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 +
                 '}';
     }
}

 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);
     }
     
 }
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
  1. 在 bean 的factory-bean 属性里指定拥有该工厂方法的bean。
  2. 在 factory-method 属性里指定该工厂方法的名称。
  3. 使用 <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>
 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);
     }
 }
 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}

猜你喜欢

转载自blog.csdn.net/saafdgvsdg/article/details/80331064