通過工廠方法配置bean
1.通過調用靜態工廠方法創建bean
調用靜態工廠方法創建bean是將對象創建的過程封裝到靜態方法中。當客戶端需要對象時,只需要簡單地調用靜態方法,而不用關心創建對象的細節。
要聲明通過靜態方法創建的bean,需要在bean的class屬性裏指定擁有該工廠的方法的類,通知在factory-method屬性裏指定工廠方法的名稱,最後,使用<constructor-arg>元素爲該方法傳遞方法參數。
2.通過調用實例工廠方法創建bean
實例工廠方法:將對象的創建過程封裝到另外一個對象實例的方法裏。當客戶端需要請求對象時,只需要簡單的調用該實例方法而不需關心對象的創建細節。
要聲明通過實例工廠方法創建的bean:-在bean的factory-bean屬性裏指定擁有該工廠方法的bean
-在factory-method屬性裏指定該工廠方法的名稱
-使用constructor-arg元素爲工廠方法傳遞方法參數
bean類:
package com.atguigu.spring.factory;
/**
* bean
* @author xiejianwei
*
*/
public class Car {
private String brand;
private float price;
public Car() {
}
public Car(String brand, float price) {
super();
this.brand = brand;
this.price = price;
}
/**
* @return the brand
*/
public String getBrand() {
return brand;
}
/**
* @param brand the brand to set
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* @return the price
*/
public float getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(float price) {
this.price = price;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
靜態工廠類:
package com.atguigu.spring.factory;
import java.util.HashMap;
import java.util.Map;
/**
* 靜態工廠方法:直接調用某一個類的靜態方法就可以返回 Bean 的實例
* @author xiejianwei
*
*/
public class StaticCarFactory {
private static Map<String, Car> cars = new HashMap<String, Car>();
static {
cars.put("audi", new Car("audi", 300000));
cars.put("ford", new Car("ford", 400000));
}
/**
* 靜態工廠方法
* @param name
* @return
*/
public static Car getCar(String name) {
return cars.get(name);
}
}
實例工廠類:
package com.atguigu.spring.factory;
import java.util.HashMap;
import java.util.Map;
/**
* 實例工廠方法:實例工廠的方法,即先需要創建工廠本身,再調用工廠的實例方法來返回bean的實例
* @author xiejianwei
*
*/
public class InstanceCarFactory {
private Map<String, Car> cars = null;
public InstanceCarFactory() {
cars = new HashMap<String, Car>();
cars.put("audi", new Car("audi", 300000));
cars.put("ford", new Car("ford", 400000));
}
public 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.atguigu.spring.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean>
<!-- 配置工廠的實例 -->
<bean id="carFactory" class="com.atguigu.spring.factory.InstanceCarFactory"></bean>
<!-- 通過實例工廠方法來配置bean -->
<!--
factory-bean:指向實例工廠方法的bean
factory-method:指向實例工廠方法的名字
constructor-arg:如果實例工廠方法需要傳入參數,則使用constructor-arg來配置參數
-->
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>
</beans>
測試類:
package com.atguigu.spring.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 測試類
* @author xiejianwei
*
*/
public class Main {
public static void main(String[] args) {
//創建 IOC 容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1);
Car car2 = (Car) ctx.getBean("car2");
System.out.println(car2);
}
}
運行結果:
Car
[brand=audi, price=300000.0]
Car [brand=ford, price=400000.0]
通過Factorybean配置bean:
package com.atguigu.spring.factorybean;
import org.springframework.beans.factory.FactoryBean;
/**
* 自定義的Factorybean需要實現FactoryBean接口
* @author xiejianwei
*
*/
public class CarFactoryBean implements FactoryBean<Car> {
private String brand;
/**
* @param brand the brand to set
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* 返回bean的對象
*/
@Override
public Car getObject() throws Exception {
return new Car(brand, 500000);
}
/**
* 返回bean的類型
*/
@Override
public Class<?> getObjectType() {
return Car.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
配置文件 beans-beanfactory.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">
<!--
通過Factorybean來配置bean的實例
class:指向Factorybean的全類名
property:配置Factorybean的屬性
但實際返回的實例卻是Factorybean的getObject()方法返回的實例
-->
<bean id="car" class="com.atguigu.spring.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
</beans>
測試類:
package com.atguigu.spring.factorybean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-beanfactory.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
}
}
運行結果:
Car
[brand=BMW, price=500000.0]