使用構(gòu)造器創(chuàng)建Bean實(shí)例(如前所示)
使用靜態(tài)工廠方法創(chuàng)建實(shí)例:
Being.java
package inter;
public interface Being {
public void testBeing();
}
Cat.java
package entity;
import inter.Being;
public class Cat implements Being{
private String msg;
public void setMsg(String msg)
{
this.msg=msg;
}
@Override
public void testBeing() {
// TODO Auto-generated method stub
System.out.println(msg+",愛(ài)吃老鼠!");
}
}
Dog.java
package entity;
import inter.Being;
public class Dog implements Being{
private String msg;
public void setMsg(String msg)
{
this.msg=msg;
}
@Override
public void testBeing() {
// TODO Auto-generated method stub
System.out.println(msg+",愛(ài)啃骨頭!");
}
}
BeanFactory.java
package config;
import entity.Cat;
import entity.Dog;
import inter.Being;
public class BeingFactory {
//返回Being實(shí)例的靜態(tài)工廠方法
public static Being getBeing(String msg)
{
if(msg.equalsIgnoreCase("dog"))
{
return new Dog();
}
else
{
return new Cat();
}
}
}
beans.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<bean id="dog" class="config.BeingFactory" factory-method="getBeing">
<!-- 配置靜態(tài)工廠方法的參數(shù) -->
<constructor-arg value="dog"/>
<!-- 驅(qū)動(dòng)Spring以“狗”為參數(shù)來(lái)執(zhí)行dog的setMsg()方法 -->
<property name="msg" value="狗" />
</bean>
<bean id="cat" class="config.BeingFactory" factory-method="getBeing">
<!-- 配置靜態(tài)工廠方法的參數(shù) -->
<constructor-arg value="cat"/>
<!-- 驅(qū)動(dòng)Spring以“貓”為參數(shù)來(lái)執(zhí)行dog的setMsg()方法 -->
<property name="msg" value="貓" />
</bean>
</beans>
采用靜態(tài)工廠方法來(lái)生產(chǎn)Bean實(shí)例需要指定兩個(gè)屬性:
- class:該屬性值為靜態(tài)工廠類(lèi)的類(lèi)名。
- factory-method:該屬性指定靜態(tài)工廠方法生產(chǎn)來(lái)生產(chǎn)Bean實(shí)例。
使用靜態(tài)工廠方法是必須指定靜態(tài)工廠類(lèi),工廠類(lèi)包含產(chǎn)生實(shí)例的靜態(tài)方法。
- class屬性不再是Bean實(shí)例的實(shí)現(xiàn)類(lèi),而是生成Bean實(shí)例的靜態(tài)工廠類(lèi)。
- 使用factory-method屬性指定創(chuàng)建Bean實(shí)例的靜態(tài)工廠方法。
- 如果靜態(tài)工廠方法需要參數(shù),則使用<constructor-arg.../>元素來(lái)指定靜態(tài)工廠方法的參數(shù)。
調(diào)用實(shí)例工廠方法創(chuàng)建Bean:
工廠方法的區(qū)別:
package config;
import entity.Cat;
import entity.Dog;
import inter.Being;
public class BeingFactory {
//返回Being實(shí)例的靜態(tài)工廠方法
public Being getBeing(String msg)
{
if(msg.equalsIgnoreCase("dog"))
{
return new Dog();
}
else
{
return new Cat();
}
}
}
配置文件的差別:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<!-- 配置工廠Bean,該Bean負(fù)責(zé)產(chǎn)生其他的Bean -->
<bean id="beingFactory" class="config.BeingFactory"/>
<bean id="dog" class="entity.Dog" factory-bean="beingFactory" factory-method="getBeing">
<constructor-arg value="dog"/>
<property name="msg" value="狗"/>
</bean>
<bean id="cat" class="entity.Cat" factory-bean="beingFactory" factory-method="getBeing">
<constructor-arg value="cat"/>
<property name="msg" value="貓"/>
</bean>
</beans>
靜態(tài)工廠方法和實(shí)例工廠方法的異同:
- 配置實(shí)例工廠創(chuàng)建Bean,必須將實(shí)例工廠配置成Bean實(shí)例;而配置靜態(tài)工廠創(chuàng)建Bean,則無(wú)需配置工廠Bean。
- 配置實(shí)例工廠方法創(chuàng)建Bean必須使用factory-bean屬性確定工廠Bean;配置靜態(tài)工廠方法創(chuàng)建Bean,使用class元素確定靜態(tài)工廠類(lèi)。
- 都需要使用factory-method屬性指定產(chǎn)生Bean實(shí)例的工廠方法。
- 工廠方法如果需要參數(shù),都是用<constructor-arg.../>元素指定參數(shù)值。
- 普通的設(shè)值注入都使用<property.../>元素確定參數(shù)。