09 通过工厂方法配置Bean

Bean的配置方式,除了全类名(反射机制)之外,还可以通过**工厂方法(静态工厂方法&实例工厂方法)**以及FactoryBean的方式来配置

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

  • 静态工厂方法:将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单的调用静态方法,而不用关心创建对象的细节
  • 要声明通过静态方法创建的Bean,需要在Bean的class属性里指定拥有该工厂的类,同时在factory-method属性里指定该工厂方法的名称.最后,使用<constructor-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
39
40
41
42
43
44
45
package factory;/*
* @Author Sheng WenZeng
* @Date 2019/8/12 14:02
* @Version 1.0
*/

/**
* @author Sheng Wenzeng
* @ClassName Car
* @Description TODO
* @Date 2019/8/12 14:02
* @Version 1.0
*/
class Car {
private String brand;
private int speed;
private double price;

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

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

public void setBrand(String brand) {
this.brand = brand;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public void setPrice(double price) {
this.price = price;
}
}

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

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

代码示例:

instanceFactory.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
package factory;/*
* @Author Sheng WenZeng
* @Date 2019/8/12 14:19
* @Version 1.0
*/

import java.util.HashMap;
import java.util.Map;

/**
* @author Sheng Wenzeng
* @ClassName InstanceFactory
* @Description TODO 先需要创建工厂本身,再调用工厂的实例方法来返回Bean的实例
* @Date 2019/8/12 14:19
* @Version 1.0
*/
public class InstanceFactory {
private Map<String, Car> cars = new HashMap<>();

public InstanceFactory() {
cars.put("Audi", new Car("Audi", 100, 400000));
cars.put("Ford", new Car("Ford", 50, 200000));
}

public Car getCar(String name) {
return cars.get(name);
}
}

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
39
40
41
42
43
44
45
package factory;/*
* @Author Sheng WenZeng
* @Date 2019/8/12 14:02
* @Version 1.0
*/

/**
* @author Sheng Wenzeng
* @ClassName Car
* @Description TODO
* @Date 2019/8/12 14:02
* @Version 1.0
*/
class Car {
private String brand;
private int speed;
private double price;

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

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

public void setBrand(String brand) {
this.brand = brand;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public void setPrice(double price) {
this.price = price;
}
}

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package factory;/*
* @Author Sheng WenZeng
* @Date 2019/8/12 14:08
* @Version 1.0
*/

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author Sheng Wenzeng
* @ClassName Main
* @Description TODO
* @Date 2019/8/12 14:08
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("factory/factory.xml");
Car car1 = (Car) applicationContext.getBean("car1");
System.out.println(car1);
}
}

factory.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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="instanceFactory" class="factory.InstanceFactory"/>

<bean id="car1" factory-bean="instanceFactory" factory-method="getCar">
<constructor-arg value="Ford"/>
</bean>
</beans>

运行结果:

1
Car{brand='Ford', speed=50, price=200000.0}

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×