10 通过FactorBean配置Bean

配置Bean的第三种方式:FactoryBean

FactoryBean需要实现FactoryBean接口,接口中有三个方法:

  • getObject()
  • getObjectType()
  • isSingleton()

FactoryBean.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 factoryBean;/*
* @Author Sheng WenZeng
* @Date 2019/8/12 14:38
* @Version 1.0
*/

/**
* @author Sheng Wenzeng
* @ClassName FactoryBean
* @Description TODO 自定义的FactoryBean,需要实现Spring提供的接口FactoryBean
* @Date 2019/8/12 14:38
* @Version 1.0
*/
public class FactoryBean implements org.springframework.beans.factory.FactoryBean<Car> {

private String brand;

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

//返回Bean的对象
@Override
public Car getObject() throws Exception {
return new Car(brand, 100, 400000);
}

//返回Bean的类型
@Override
public Class<?> getObjectType() {
return Car.class;
}

@Override
public boolean isSingleton() {
return true;
}
}

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 factoryBean;/*
* @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 factoryBean;/*
* @Author Sheng WenZeng
* @Date 2019/8/12 14:43
* @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:43
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("factoryBean/BeanFactory.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
}
}

BeanFactory.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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">
<!--
通过FactoryBean来配置Bean的实例
class:指向FactoryBean的全类名
property:配置FactoryBean的属性,但实际上返回的是getObject()方法返回的实例
-->
<bean id="car" class="factoryBean.FactoryBean">
<property name="brand" value="Audi"/>
</bean>
</beans>

运行结果为:

1
Car{brand='Audi', speed=100, price=400000.0}

评论

Your browser is out-of-date!

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

×