通过工厂方法配置Bean暴扣静态工厂方法和实例工厂方法。
1.静态工厂方法
调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不去关心创建对象的细节。
要声明通过静态方法创建的Bean,需要在Bean的class属性里指定拥有该工厂的方法的类,同时在factory-method属性里指定工厂方法的名称,最后,使用<constructor-arg>元素为该方法传递方法参数。
public class Car {
private String brand;
private double price; public Car() {
System.out.println("com.cn.Car's Constructor");
} public Car(String brand, double price) {
this.brand = brand;
this.price = price;
} public void setBrand(String brand) {
System.out.println("setBrand");
this.brand = brand;
} public String getBrand() {
return brand;
} public void setPrice(double price) {
this.price = price;
} public double getPrice() {
return price;
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
/**
* 静态工厂方法:直接调用某一个类的静态方法,就可以返回Bean的实例
*/
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));
} //静态工厂方法
public static Car getCar(String name) {
return cars.get(name);
}
}
<?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="factory.StaticCarFactory" factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean>
</beans>
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("11.xml");
Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1);
}
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAAAgCAIAAACj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ztd+ttlrABiXfhUb8ZVO5qbErPooKeWYAtdltivt8XjM1gL7jH/AarnO2pV7vNGMuRGH3Wc6Sg57VHAOM0t9totZUuyz3RjrmYyUf1i5JQaVszexqHWiPi2s6ocVRBeGfJnu2osQ8wIFjUgKKGhEUkBBI5ICChqRFFDQiKSAgkYkBRQ0IimgoBFJAQWNSAooaERS+Adb06dxVjPvdQAAAABJRU5ErkJggg==" alt="" />
2.实例工厂方法
将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时,只需要简单的调用该实例方法而不需要关心对象的创建细节。
要声明通过实例工厂方法创建的Bean。在Bean的factory-bean属性里指定拥有该工厂方法的Bean,同时在factory-method属性里指定工厂方法的名称,最后,使用<constructor-arg>元素为该方法传递方法参数。
public class Car {
private String brand;
private double price; public Car() {
System.out.println("com.cn.Car's Constructor");
} public Car(String brand, double price) {
this.brand = brand;
this.price = price;
} public void setBrand(String brand) {
System.out.println("setBrand");
this.brand = brand;
} public String getBrand() {
return brand;
} public void setPrice(double price) {
this.price = price;
} public double getPrice() {
return price;
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
/**
* 实例工厂方法:实例工厂的方法,即先需要创建工厂本身,在调用工厂的实例方法来返回bean的实例
*/
public class InstanseCarFactory {
private Map<String, Car> cars = null; public InstanseCarFactory() {
cars = new HashMap<String, Car>();
cars.put("audi", new Car("audi", 300000));
cars.put("ford", new Car("ford", 400000));
} public Car getCar(String brand) {
return cars.get(brand);
}
}
<?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 id="carFactory" class="factory.InstanseCarFactory"></bean>
<!--通过实例工厂方法来配置bean-->
<!--
factory-bean:指向实例工厂方法的bean
factory-method:指向实例工厂方法的名字
constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数
-->
<bean id="car" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>
</beans>
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("11-2.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
}
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOIAAAApCAIAAACnXWL3AAAEZklEQVR4nO1ZPY7iMBTe6/BSrXagWmlAGjeIaiPaOB2jzAE8HGJcbpdqfYlNJPo9BUdhC4fYju1gQsjg0ftEER72+/Pn9xzz7efzSn6+/5i3n+12m1g4vb3aQnuM73NxbguA5aFI9wC2RP8JFutjsTu9vZ7Mn4wxpirnmHSTSQ8PK3C5kyRJst/ujnl22i6VksX6WGTlwqHwYlzmw65J0TVxDdTsT2xfni/NCtHj17w7FVm58OZNjv82gKaBFLyKmrZnbcz7zfrX7KnMs3IBsFgf83Xqjn+uxgTTNIhejcLlocjKRTtlecjXqUfPfrNOLZ3KqIpiXuYXNmQYTbux25rtbITkp38WrJaePM8D1mtZbuZO/bbPA2nqlLfP9kh9TIBbO2MbyBoma4y282CVqjEyF1KSZ8didyrS99nzoVuW2q9yH6d7gP22W3Wc/hzzTI55nz0finQP83KzlLEoQ6bPtqoy71ZuVTvzhtlGXDT95/LZodyMvdkMmma7vpoJceenk8NuWlR76Y7R18uj+anMdz3p0jWPXE07NO0RPgjgXCOTS/sH8YkYs5p2+Nrh8WPSNNHOpv3HU8Qn4gqaIhCfBaQpIgIgTRERAGmKiABIU0QEQJoiIgDSFBEBkKaICDARTSmvqorTaf/gAaD8Pkbvp/mrAqChwLDpV9CU8qqu67qurzXWLmrj63gL3K8QgHLBiP03PeVNINd4otvyaR4F4+4BqStJEgDCRFXXdXX2fCyJKwTHmFviCqUpYUJGmyQJZdetkO7f6HXIpxCAMCEYseWUVw55uK1s9uLU/IAAyoVoFq5dQSCMMzKixIZzzBQ0ZWI4t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