通过工厂方法配置Bean

通过调用静态工厂方法创建Bean

  • 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中。当客户端需要对象时,只需要简单地调用静态方法,而不用关心创建对象的细节。
  • 要声明通过静态方法创建的Bean。需要在Bean的class属性里指定拥有该工厂的方法的类,同时在factory-method属性里指定工厂方法的名称,最后,使用<constrctor-arg>元素为该方法传递参数。

Car.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.spring;

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;
}

public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}

public Car() {
}

@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}

beans-factory.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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.spring.StaticCarFactory" factory-method="getCar">
<constructor-arg value="audi" />
</bean>
</beans>

MainTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.test;

import com.spring.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
public static void main(String[] args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans-factory.xml");
Car car = (Car)applicationContext.getBean("car1");
System.out.println(car);
}
}

运行结果:
Car{brand='audi', price=300000.0}

通过调用实例工厂方法创建Bean

  • 实例工厂方法:将对象的创建过程封装到另一个对象实例的方法里。当客户端需要请求对象时,值需要简单的调用该实例方法而不需要关心对象的创建细节。
  • 要声明通过实例工厂方法创建的Bean
    • 在bean的factory-bean属性里指定拥有该工厂方法的Bean
    • factory-method属性里指定该工厂方法的名称
    • 使用construtor-arg元素为工厂方法传递方法参数

Car.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.spring;

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;
}

public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}

public Car() {
}

@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}

InstanceCarFactory.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.spring;

import java.util.HashMap;
import java.util.Map;
/**
* 实例工厂方法:实例工厂的方法,即现需要创建工厂本身,再调用工厂的实例方法来返回bean的实例
* */
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 brand){
return cars.get(brand);
}
}

MainTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.test;

import com.spring.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
public static void main(String[] args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans-factory.xml");
Car car = (Car)applicationContext.getBean("car2");
System.out.println(car);
}
}

测试结果:
Car{brand='ford', price=400000.0}

-------------本文结束 感谢您的阅读-------------
文章对我有益,我要小额赞赏...