接下来的代码都会复用Car.java
,请预先准备好这个类。
public class Car {
private String brand;
private 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;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
public Car() {
System.out.println("Car's constructor");
}
public Car(String brand, double price) {
super();
this.brand = brand;
this.price = price;
}
}
调用静态工厂方法创建 Bean是将对象创建的过程封装到静态方法中. 当客户端需要对象时, 只需要简单地调用静态方法, 而不同关心创建对象的细节。
StaticCarFactory.java
/**
* 静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例
*/
public class StaticCarFactory {
private static Map<String, Car> carmap=new HashMap<String, Car>();
static{
carmap.put("audi", new Car("audi",300000));
carmap.put("ford", new Car("ford",400000));
}
//静态方法
public static Car getCar(String name) {
return carmap.get(name);
}
}
beans-factory.xml
<bean id="car1" class="com.spring.factory.StaticCarFactory" factory-method="getCar">
<constructor-arg value="audi">constructor-arg>
bean>
属性 | 说明 |
---|---|
class | 指向静态工厂方法的的全类名 |
factory-method | 指向静态工厂方法的名字 |
constructor-arg | 如果工厂方法需要传入参数。则使用 传入参数 |
Main.java
public class main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
Car car =(Car) ctx.getBean("car1");
System.out.println(car);
//关闭IOC容器
ctx.close();
}
}
将对象的创建过程封装到另外一个对象实例的方法里. 当客户端需要请求对象时, 只需要简单的调用该实例方法而不需要关心对象的创建细节。
InstanceCarFactory.java
/**
* 实例工厂方法:即先需要创建工厂本身,再调用工厂的实例方法来返回bean的实例
*/
public class InstanceCarFactory {
private static Map<String, Car> carmap=new HashMap<String, Car>();
static{
carmap.put("audi", new Car("audi",300000));
carmap.put("ford", new Car("ford",400000));
}
//方法
public Car getCar(String name) {
return carmap.get(name);
}
}
beans-factory.xml
配置方法和上面的静态工厂差不多。
<bean id="carFactory" class="com.spring.factory.InstanceCarFactory">bean>
<constructor-arg value="ford">constructor-arg>
bean>
Main.java
public class main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
Car car2 =(Car) ctx.getBean("car2");
System.out.println(car2);
}
}
getObject()
方法所返回的对象。CarFactoryBean.java
,应用FactoryBean<>接口注意!!
FactoryBean<>中的泛型应该为目标Bean的类型
package top.cheungchingyin.beans.factorybean;
import org.springframework.beans.factory.FactoryBean;
//自定义FactoryBean需要实现FactoryBean接口
public class CarFactoryBean implements FactoryBean<Car> {
private String brand;
private double price;
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(double price){
this.price = price;
}
// 返回bean对象
@Override
public Car getObject() throws Exception {
return new Car(brand, price);
}
// 返回bean的类型
@Override
public Class<?> getObjectType() {
return Car.class;
}
// 是指是否为单例
@Override
public boolean isSingleton() {
return false;
}
}
beans-factory.xml
<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 id="car" class="top.cheungchingyin.beans.factorybean.CarFactoryBean">
<property name="brand" value="BMW">property>
<property name="price" value="500000">property>
bean>
beans>
Main.java
package top.cheungchingyin.beans.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);
}
}