Spring框架筆記(十)——IOC容器使用靜態工廠和實例工廠的方法建立bean

前面咱們建立IOC容器的bean都是經過類構造器(不管是屬性注入仍是構造器注入方式)來建立bean實例的。
java

在spring IOC容器中還提供了經過工廠來建立bean的方法。其中分爲兩種方式:靜態工廠和實例工廠spring


靜態工廠方法建立 Beanapp

調用靜態工廠方法建立 Bean是將對象建立的過程封裝到靜態方法中. 當客戶端須要對象時, 只須要簡單地調用靜態方法, 而不一樣關心建立對象的細節.ide

要聲明經過靜態方法建立的 Bean, 須要在 Bean 的 class 屬性裏指定擁有該工廠的方法的類, 同時在 factory-method 屬性裏指定工廠方法的名稱. 最後, 使用 <constrctor-arg> 元素爲該方法傳遞方法參數.測試


實例工廠方法建立 Beanthis

實例工廠方法: 將對象的建立過程封裝到另一個對象實例的方法裏. 當客戶端須要請求對象時, 只須要簡單的調用該實例方法而不須要關心對象的建立細節.spa

要聲明經過實例工廠方法建立的 Beancode

    在 bean 的 factory-bean 屬性裏指定擁有該工廠方法的 Beanxml

    在 factory-method 屬性裏指定該工廠方法的名稱對象

    使用 construtor-arg 元素爲工廠方法傳遞方法參數


咱們仍是用例子說話吧:

顯示bean類的定義:

package com.happyBKs.factory;

public class CarBean {
	String brand;
	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 CarBean(String brand, double price) {
		super();
		this.brand = brand;
		this.price = price;
	}
	public CarBean() {
		super();
		System.out.println(this.brand+" Constructor ....");
	}
	@Override
	public String toString() {
		return "CarBean [brand=" + brand + ", price=" + price + "]";
	}
	
}

容器配置文件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.happyBKs.factory.CarStaticFactory"
		factory-method="getCar">
		<constructor-arg value="VOLVONO1"></constructor-arg>
	</bean>

	<!-- 經過實體工廠的方法來配置bean (須要工廠單獨配置一個實例) -->
	<!-- 配置工廠的實例 -->
	<bean id="carFactoryBean" class="com.happyBKs.factory.CarInstanceFactory" />
	<!-- 經過工廠實例方法來配置bean -->
	<!-- 
	 factory-bean屬性:指向實例工廠 方法的全類名
	 factory-method屬性:指向實例工廠方法的名字 
	 constructor-arg:若是工廠方法須要傳入參數,則使用constructor-arg類配置參數
	 -->
	<bean id="car2" factory-bean="carFactoryBean" factory-method="getCar">
		<constructor-arg value="VOLVONO1"></constructor-arg>
	</bean>

</beans>

注意:靜態工廠直接在bean自己的class註明便可,而實例工廠則須要先爲實例工廠配置一個bean,而後將這個工廠bean在相應bean的factory-bean中指出。

測試代碼:

package com.happBKs.spring.iocaop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.happyBKs.factory.CarBean;



public class TestFactory {
	
	@Test
	public void testStaticFactory()
	{

		AbstractApplicationContext ac=new ClassPathXmlApplicationContext("beans-factory.xml");
		CarBean cb=(CarBean)ac.getBean("car1");
		System.out.println(cb);
		ac.close();
	}
	
	@Test
	public void testInstanceFactory()
	{

		AbstractApplicationContext ac=new ClassPathXmlApplicationContext("beans-factory.xml");
		CarBean cb=(CarBean)ac.getBean("car2");
		System.out.println(cb);
		ac.close();
	}
}

輸出結果都是:

CarBean [brand=volvo S60L, price=600000.0]


下面咱們經過例子來講明:

相關文章
相關標籤/搜索