配置工厂Bean
通常由应用程序直接使用new创建新的对象,为了将对象的创建和使用相分离,采用工厂模式,即应用程序将对象的创建及初始化职责交给工厂对象.
一般情况下,应用程序有自己的工厂对象来创建bean.如果将应用程序自己的工厂对象交给Spring管理,那么Spring管理的就不是普通的bean,而是工厂Bean.
调用getBean()方法,Spring返回的不是直接创建的Bean的实例,而是由工厂Bean创建的Bean实例.
一般在Spring中配置工厂Bean,有3中不同类型的工厂Bean的配置.
1.静态工厂
创建具体Bean实例的是静态方法
import java.util.Random;
public class StaticFactoryBean {
public static Integer createRandom() {
return new Integer(new Random().nextInt());
}
}
}
}
将其纳入Spring容器来管理,需要通过factory-method指定静态方法名称
<bean id="random"
class="example.chapter3.StaticFactoryBean"
factory-method="createRandom" //createRandom方法必须是static的,才能找到
scope="prototype"
/>
class="example.chapter3.StaticFactoryBean"
factory-method="createRandom" //createRandom方法必须是static的,才能找到
scope="prototype"
/>
测试:
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("random").toString());
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("random").toString());
//StaticFactoryBean sfb = (StaticFactoryBean)factory.getBean("random");
//System.out.println(sfb.createRandom().toString());
//System.out.println(sfb.createRandom().toString());
//调用getBean()时,返回随机数.如果没有指定factory-method,会返回StaticFactoryBean的实例,即返回工厂Bean的实例
}
}
2.实例工厂
创建具体Bean实例的是实例,不是静态方法
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Date;
public class InstanceFactoryBean {
private String format = "yy-MM-dd HH:mm:ss";
public void setFormat(String format) {
this.format = format;
}
this.format = format;
}
public String createTime() {
return new SimpleDateFormat(format).format(new Date());
}
}
return new SimpleDateFormat(format).format(new Date());
}
}
配置文件需要配置两个bean:第一个Bean和普通的Bean没有区别,第二个bean定义如何通过工厂Bean获取Bean,需要指定
工厂Bean的名称和方法名称
<bean id="instanceFactoryBean" class="example.chapter3.InstanceFactoryBean">
< property name="format" value="yyyy-MM-dd HH:mm:ss" />
< /bean>
< property name="format" value="yyyy-MM-dd HH:mm:ss" />
< /bean>
<bean id="currentTime"
factory-bean="instanceFactoryBean"
factory-method="createTime"
/>
factory-bean="instanceFactoryBean"
factory-method="createTime"
/>
测试:
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("currentTime"));
}
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("currentTime"));
}
3.实现FactoryBean接口
public class PiFactoryBean implements FactoryBean {
public Object getObject() throws Exception {
return new Double(3.14159265358979);
}
return new Double(3.14159265358979);
}
public Class getObjectType() {
return Double.class;
}
return Double.class;
}
public boolean isSingleton() {
return true;
}
return true;
}
}
实现了FactoryBean接口的Bean,不再被视为普通的Bean.Spring会自动检测.
<bean id="pi" class="example.chapter3.PiFactoryBean" />
测试
public static void main(String[] args) throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("pi"));//返回PiFactoryBean 的工厂方法getObject返回的Double对象实例
//PiFactoryBean p = (PiFactoryBean)factory.getBean("&pi"); //加"&"返回工厂Bean的实例.
//System.out.println(p.getObject());
}
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("pi"));//返回PiFactoryBean 的工厂方法getObject返回的Double对象实例
//PiFactoryBean p = (PiFactoryBean)factory.getBean("&pi"); //加"&"返回工厂Bean的实例.
//System.out.println(p.getObject());
}