Skip to content

Commit 9d7ad52

Browse files
authored
Merge pull request eugenp#860 from DianeDuan/spring-factorybean
BAEL-453: How to use the Spring FactoryBean (simplified version)
2 parents b451fcf + 750aad4 commit 9d7ad52

17 files changed

Lines changed: 86 additions & 399 deletions

‎spring-core/src/main/java/com/baeldung/factorybean/FactoryBeanAppConfig.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@
77
public class FactoryBeanAppConfig {
88

99
@Bean
10-
public ToolFactory tool() {
10+
public ToolFactory toolFactory() {
1111
ToolFactory factory = new ToolFactory();
1212
factory.setFactoryId(7070);
1313
factory.setToolId(2);
14-
factory.setToolName("wrench");
15-
factory.setToolPrice(3.7);
1614
return factory;
1715
}
1816

1917
@Bean
20-
public Worker worker() throws Exception {
21-
Worker worker = new Worker();
22-
worker.setNumber("1002");
23-
worker.setTool(tool().getObject());
24-
return worker;
18+
public Tool tool() throws Exception {
19+
return toolFactory().getObject();
2520
}
2621
}

‎spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java‎

Lines changed: 0 additions & 68 deletions
This file was deleted.

‎spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
66

77
private int factoryId;
88
private int toolId;
9-
private String toolName;
10-
private double toolPrice;
119

1210
public NonSingleToolFactory() {
1311
setSingleton(false);
@@ -20,7 +18,7 @@ public Class<?> getObjectType() {
2018

2119
@Override
2220
protected Tool createInstance() throws Exception {
23-
return new Tool(toolId, toolName, toolPrice);
21+
return new Tool(toolId);
2422
}
2523

2624
public int getFactoryId() {
@@ -38,20 +36,4 @@ public int getToolId() {
3836
public void setToolId(int toolId) {
3937
this.toolId = toolId;
4038
}
41-
42-
public String getToolName() {
43-
return toolName;
44-
}
45-
46-
public void setToolName(String toolName) {
47-
this.toolName = toolName;
48-
}
49-
50-
public double getToolPrice() {
51-
return toolPrice;
52-
}
53-
54-
public void setToolPrice(double toolPrice) {
55-
this.toolPrice = toolPrice;
56-
}
5739
}

‎spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java‎

Lines changed: 0 additions & 69 deletions
This file was deleted.

‎spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ public class SingleToolFactory extends AbstractFactoryBean<Tool> {
77

88
private int factoryId;
99
private int toolId;
10-
private String toolName;
11-
private double toolPrice;
1210

1311
@Override
1412
public Class<?> getObjectType() {
@@ -17,7 +15,7 @@ public Class<?> getObjectType() {
1715

1816
@Override
1917
protected Tool createInstance() throws Exception {
20-
return new Tool(toolId, toolName, toolPrice);
18+
return new Tool(toolId);
2119
}
2220

2321
public int getFactoryId() {
@@ -35,20 +33,4 @@ public int getToolId() {
3533
public void setToolId(int toolId) {
3634
this.toolId = toolId;
3735
}
38-
39-
public String getToolName() {
40-
return toolName;
41-
}
42-
43-
public void setToolName(String toolName) {
44-
this.toolName = toolName;
45-
}
46-
47-
public double getToolPrice() {
48-
return toolPrice;
49-
}
50-
51-
public void setToolPrice(double toolPrice) {
52-
this.toolPrice = toolPrice;
53-
}
5436
}
Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.baeldung.factorybean;
22

33
public class Tool {
4-
54
private int id;
6-
private String name;
7-
private double price;
85

9-
public Tool(int id, String name, double price) {
6+
public Tool() {
7+
}
8+
9+
public Tool(int id) {
1010
this.id = id;
11-
this.name = name;
12-
this.price = price;
1311
}
1412

1513
public int getId() {
@@ -19,20 +17,4 @@ public int getId() {
1917
public void setId(int id) {
2018
this.id = id;
2119
}
22-
23-
public String getName() {
24-
return name;
25-
}
26-
27-
public void setName(String name) {
28-
this.name = name;
29-
}
30-
31-
public double getPrice() {
32-
return price;
33-
}
34-
35-
public void setPrice(double price) {
36-
this.price = price;
37-
}
3820
}

‎spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ public class ToolFactory implements FactoryBean<Tool> {
66

77
private int factoryId;
88
private int toolId;
9-
private String toolName;
10-
private double toolPrice;
119

1210
@Override
1311
public Tool getObject() throws Exception {
14-
return new Tool(toolId, toolName, toolPrice);
12+
return new Tool(toolId);
1513
}
1614

1715
@Override
@@ -39,20 +37,4 @@ public int getToolId() {
3937
public void setToolId(int toolId) {
4038
this.toolId = toolId;
4139
}
42-
43-
public String getToolName() {
44-
return toolName;
45-
}
46-
47-
public void setToolName(String toolName) {
48-
this.toolName = toolName;
49-
}
50-
51-
public double getToolPrice() {
52-
return toolPrice;
53-
}
54-
55-
public void setToolPrice(double toolPrice) {
56-
this.toolPrice = toolPrice;
57-
}
5840
}

‎spring-core/src/main/java/com/baeldung/factorybean/Worker.java‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,10 @@
66
<bean id="singleTool" class="com.baeldung.factorybean.SingleToolFactory">
77
<property name="factoryId" value="3001"/>
88
<property name="toolId" value="1"/>
9-
<property name="toolName" value="screwdriver"/>
10-
<property name="toolPrice" value="1.5"/>
119
</bean>
1210

1311
<bean id="nonSingleTool" class="com.baeldung.factorybean.NonSingleToolFactory">
1412
<property name="factoryId" value="3002"/>
1513
<property name="toolId" value="2"/>
16-
<property name="toolName" value="screwdriver"/>
17-
<property name="toolPrice" value="1.5"/>
18-
</bean>
19-
20-
<bean id="worker1" class="com.baeldung.factorybean.Worker">
21-
<property name="number" value="50001"/>
22-
<property name="tool" ref="singleTool"/>
23-
</bean>
24-
25-
<bean id="worker2" class="com.baeldung.factorybean.Worker">
26-
<property name="number" value="50002"/>
27-
<property name="tool" ref="singleTool"/>
28-
</bean>
29-
30-
<bean id="worker3" class="com.baeldung.factorybean.Worker">
31-
<property name="number" value="50003"/>
32-
<property name="tool" ref="nonSingleTool"/>
33-
</bean>
34-
35-
<bean id="worker4" class="com.baeldung.factorybean.Worker">
36-
<property name="number" value="50004"/>
37-
<property name="tool" ref="nonSingleTool"/>
3814
</bean>
3915
</beans>

0 commit comments

Comments
 (0)