天天看點

Spring之通過工廠方法建立Bean

簡介

靜态工廠方法

1.調用靜态方法建立Bean是将對象建立的過程封裝到靜态方法中。當用戶端需要對象時,隻需要簡單的調用靜态方法,
而不必關系建立對象的細節
2.要聲明通過靜态方法建立的Bean,需要在Bean的class屬性裡指定擁有該工廠方法的類,同時在factory-method屬性裡
指定工廠方法的名稱,最後使用<constrctor-arg>元素為該方法傳遞方法參數
           

car.java

package com.lanou3g.beans.factory;
public class Car {
	private String brand;
	private double price;
	public Car() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Car(String brand, double price) {
		super();
		this.brand = brand;
		this.price = 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;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + "]";
	}	
}
           

StaticCarFactory.java

package com.lanou3g.beans.factory;
import java.util.HashMap;
import java.util.Map;
/**
 * 靜态工廠方法:直接調用某一個類的靜态方法就可以傳回Bean執行個體
 * @author Administrator
 *
 */
public class StaticCarFactory {	
	private static Map<String, Car> cars = new HashMap<String,Car>();	
	static {
		cars.put("aodi", new Car("aodi", 3000000));
		cars.put("ford", new Car("ford", 4000000));
	}	
	public static Car getCar(String name) {
		return cars.get(name);
	}
}
           

beans-factory.xml

<?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,注意不是配置靜态方法執行個體,而是配置bean執行個體 -->
	<!-- 
		class屬性:指向靜态方法的全類名
		factory-method:指向靜态工廠方法的名字
		constructor-arg:如果工廠方法需要傳遞參數,則使用constructor-arg來配置參數
	 -->
	<bean id="car1" 
	class="com.lanou3g.beans.factory.StaticCarFactory"
	factory-method="getCar">
	 <constructor-arg value="aodi"></constructor-arg>
	</bean>
</beans>
           

Test.java

package com.lanou3g.beans.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
		Car car = (Car)ctx.getBean("car1");
		System.out.println(car);
	}
}
           

結果

資訊: Loading XML bean definitions from class path resource [beans-factory.xml]
Car [brand=aodi, price=3000000.0]
           

執行個體工廠方法

1.執行個體工廠方法:将對象的建立過程封裝到另外一個對象執行個體的方法裡。當用戶端需要請求對象時,
隻需要簡單的調用該執行個體方法而不需要關心對象的建立細節。
2.要聲明通過執行個體方法建立的Bean
	-在bean的factory-bean屬性裡指定擁有該工廠方法的Bean
	-在factory-method屬性裡指定該工廠方法的名稱
	-使用constructor-arg元素為工廠方法傳遞方法參數
           

beans-factory.xml

<?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,注意不是配置靜态方法執行個體,而是配置bean執行個體 -->
	<!-- 
		class屬性:指向靜态方法的全類名
		factory-method:指向靜态工廠方法的名字
		constructor-arg:如果工廠方法需要傳遞參數,則使用constructor-arg來配置參數
	 -->
	<bean id="car1" 
	class="com.lanou3g.beans.factory.StaticCarFactory"
	factory-method="getCar">
	 <constructor-arg value="aodi"></constructor-arg>
	</bean>
	
	<!-- 配置工廠執行個體 -->
	<bean id="carFactory" class="com.lanou3g.beans.factory.InstanceCarFactory"></bean>
	<!-- 通過工廠執行個體配置bean -->
	<bean id="car2" factory-bean="carFactory" factory-method="getCar">
		<constructor-arg value="ford"></constructor-arg>
	</bean>
</beans>
           

InstanceCarFactory.java

package com.lanou3g.beans.factory;
import java.util.HashMap;
import java.util.Map;
/**
 * 執行個體工廠方法:執行個體工廠方法,即現需要建立工廠本身,再調用工廠的執行個體方法來傳回bean的執行個體
 * @author Administrator
 *
 */
public class InstanceCarFactory {
	private static Map<String, Car> cars = null;
	public InstanceCarFactory() {
		cars = new HashMap<String,Car>();
		cars.put("aodi", new Car("aodi", 400000));
		cars.put("ford", new Car("ford", 500000));
	}
	public Car getCar(String name) {
		return cars.get(name);
	}
}
           

Test.java

package com.lanou3g.beans.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
		Car car = (Car)ctx.getBean("car2");
		System.out.println(car);
	}
}
           

結果

資訊: Loading XML bean definitions from class path resource [beans-factory.xml]
Car [brand=ford, price=500000.0]