1. 使用静态工厂方法创建Bean,用到一个工厂类
例子:一个Car类,有brand和price属性。
1 package com.guigu.spring.factory; 2 3 public class Car { 4 private String brand; 5 private double price; 6 7 public Car(){ 8 } 9 public Car(String brand,double price){ 10 this.brand=brand; 11 this.price=price; 12 } 13 public String getBrand() { 14 return brand; 15 } 16 public void setBrand(String brand) { 17 this.brand = brand; 18 } 19 public double getPrice() { 20 return price; 21 } 22 public void setPrice(double price) { 23 this.price = price; 24 } 25 @Override 26 public String toString() { 27 return "Car [brand=" + brand + "]"; 28 } 29 }
工厂类:
1 package com.guigu.spring.factory; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class StaticCarFactory { 7 8 private static Map<String,Car> cars=new HashMap<String,Car>(); 9 //静态代码块,防止重复添加 10 static{ 11 cars.put("aodi", new Car("aodi",300000)); 12 cars.put("ford", new Car("ford",200000)); 13 } 14 //静态工厂方法 15 public static Car getCar(String name){ 16 return cars.get(name); 17 } 18 }
xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 9 <!-- 10 class属性:指向静态工厂方法的全类名,不是bean实例的实现类 11 factory-method:指向静态工厂方法的名字,方法里面返回bean 12 constructor-arg:如果工厂方法需要传入参数,则用constructor-arg配置 13 14 --> 15 <bean id="car1" class="com.guigu.spring.factory" factory-method="getCar"> 16 <constructor-arg value="aodi"></constructor-arg> 17 </bean> 18 19 </beans>
main函数测试
1 public class main { 2 3 public static void main(String[] args) { 4 ApplicationContext ctx =new ClassPathXmlApplicationContext("bean-factory.xml"); 5 Car car1=(Car) ctx.getBean("car1"); 6 System.out.println(car1); 7 } 8 }
输出:
这个过程中,spring不再自己创建bean实例,bean实例由用户自己提供的工厂方法创建。静态工厂方法,方法必须是静态的
---------------------------------------
其实我还是不懂, 调用静态方法
什么时候对cars进行初始化了?单单调用静态方法并没有执行工厂类。望看观指教
--------------------------------------
2. 实例工厂方法,工厂本身是实例,要实现创建工厂对象本身
1 /* 2 * 实例工厂方法:实例工厂的方法,即先需要创建工厂本身,再调用工厂的实例方法来返回bean的实例 3 * */ 4 public class InstanceCarFactory { 5 6 private Map<String,Car> cars=null; 7 8 public InstanceCarFactory(){ 9 cars = new HashMap<String, Car>(); 10 cars.put("aodi", new Car("aodi",300000)); 11 cars.put("ford", new Car("ford",400000)); 12 } 13 // 14 public Car getCar(String brand){ 15 return cars.get(brand); 16 } 17 }
xml配置
1 <!--先配置工厂的实例--> 2 <bean id="carFactory" class="com.guigu.spring.factory"></bean> 3 <!-- 4 factory-bean属性:指向实例工厂方法的bean 5 method:指向静态工厂方法的名字 6 constructor-arg:如果工厂方法需要传入参数,用constructor-arg配置 7 --> 8 <bean id="car2" factory-bean="carFactory" factory-method="getCar"> 9 <constructor-arg value="ford"></constructor-arg> 10 </bean>
main函数
public class main { public static void main(String[] args) { ApplicationContext ctx =new ClassPathXmlApplicationContext("bean-factory.xml"); Car car2 = (Car) ctx.getBean("car2"); System.out.println(car2); } }
输出:
这个好理解,先实例工厂,调用构造函数,然后bean调用工厂的方法。
文章为学习记录,若有错误,望指正