Skip to content

Commit d6daf82

Browse files
authored
Merge pull request eugenp#848 from DianeDuan/spring-factorybean
BAEL-453: How to use the Spring FactoryBean
2 parents 8e92dd7 + e84957c commit d6daf82

16 files changed

Lines changed: 585 additions & 0 deletions

‎spring-core/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<version>4.12</version>
4848
<scope>test</scope>
4949
</dependency>
50+
<dependency>
51+
<groupId>com.google.guava</groupId>
52+
<artifactId>guava</artifactId>
53+
<version>20.0</version>
54+
</dependency>
5055
</dependencies>
5156

5257
<build>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.factorybean;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class FactoryBeanAppConfig {
8+
@Bean
9+
public ToolFactory tool() {
10+
ToolFactory factory = new ToolFactory();
11+
factory.setFactoryId(7070);
12+
factory.setToolId(2);
13+
factory.setToolName("wrench");
14+
factory.setToolPrice(3.7);
15+
return factory;
16+
}
17+
18+
@Bean
19+
public Worker worker() throws Exception {
20+
Worker worker = new Worker();
21+
worker.setNumber("1002");
22+
worker.setTool(tool().getObject());
23+
return worker;
24+
}
25+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.factorybean;
2+
3+
import static com.google.common.base.Preconditions.checkArgument;
4+
5+
import org.springframework.beans.factory.FactoryBean;
6+
import org.springframework.beans.factory.InitializingBean;
7+
import org.springframework.util.StringUtils;
8+
9+
public class InitializationToolFactory implements FactoryBean<Tool>, InitializingBean {
10+
private int factoryId;
11+
private int toolId;
12+
private String toolName;
13+
private double toolPrice;
14+
15+
@Override
16+
public void afterPropertiesSet() throws Exception {
17+
checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
18+
checkArgument(toolPrice >= 0, "tool price should not be less than 0");
19+
}
20+
21+
@Override
22+
public Tool getObject() throws Exception {
23+
return new Tool(toolId, toolName, toolPrice);
24+
}
25+
26+
@Override
27+
public Class<?> getObjectType() {
28+
return Tool.class;
29+
}
30+
31+
@Override
32+
public boolean isSingleton() {
33+
return false;
34+
}
35+
36+
public int getFactoryId() {
37+
return factoryId;
38+
}
39+
40+
public void setFactoryId(int factoryId) {
41+
this.factoryId = factoryId;
42+
}
43+
44+
public int getToolId() {
45+
return toolId;
46+
}
47+
48+
public void setToolId(int toolId) {
49+
this.toolId = toolId;
50+
}
51+
52+
public String getToolName() {
53+
return toolName;
54+
}
55+
56+
public void setToolName(String toolName) {
57+
this.toolName = toolName;
58+
}
59+
60+
public double getToolPrice() {
61+
return toolPrice;
62+
}
63+
64+
public void setToolPrice(double toolPrice) {
65+
this.toolPrice = toolPrice;
66+
}
67+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.factorybean;
2+
3+
import org.springframework.beans.factory.config.AbstractFactoryBean;
4+
5+
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
6+
private int factoryId;
7+
private int toolId;
8+
private String toolName;
9+
private double toolPrice;
10+
11+
public NonSingleToolFactory() {
12+
setSingleton(false);
13+
}
14+
15+
@Override
16+
public Class<?> getObjectType() {
17+
return Tool.class;
18+
}
19+
20+
@Override
21+
protected Tool createInstance() throws Exception {
22+
return new Tool(toolId, toolName, toolPrice);
23+
}
24+
25+
public int getFactoryId() {
26+
return factoryId;
27+
}
28+
29+
public void setFactoryId(int factoryId) {
30+
this.factoryId = factoryId;
31+
}
32+
33+
public int getToolId() {
34+
return toolId;
35+
}
36+
37+
public void setToolId(int toolId) {
38+
this.toolId = toolId;
39+
}
40+
41+
public String getToolName() {
42+
return toolName;
43+
}
44+
45+
public void setToolName(String toolName) {
46+
this.toolName = toolName;
47+
}
48+
49+
public double getToolPrice() {
50+
return toolPrice;
51+
}
52+
53+
public void setToolPrice(double toolPrice) {
54+
this.toolPrice = toolPrice;
55+
}
56+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.factorybean;
2+
3+
import static com.google.common.base.Preconditions.checkArgument;
4+
5+
import javax.annotation.PostConstruct;
6+
7+
import org.springframework.beans.factory.FactoryBean;
8+
import org.springframework.util.StringUtils;
9+
10+
public class PostConstructToolFactory implements FactoryBean<Tool> {
11+
private int factoryId;
12+
private int toolId;
13+
private String toolName;
14+
private double toolPrice;
15+
16+
@Override
17+
public Tool getObject() throws Exception {
18+
return new Tool(toolId, toolName, toolPrice);
19+
}
20+
21+
@Override
22+
public Class<?> getObjectType() {
23+
return Tool.class;
24+
}
25+
26+
@Override
27+
public boolean isSingleton() {
28+
return false;
29+
}
30+
31+
@PostConstruct
32+
public void checkParams() {
33+
checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
34+
checkArgument(toolPrice >= 0, "tool price should not be less than 0");
35+
}
36+
37+
public int getFactoryId() {
38+
return factoryId;
39+
}
40+
41+
public void setFactoryId(int factoryId) {
42+
this.factoryId = factoryId;
43+
}
44+
45+
public int getToolId() {
46+
return toolId;
47+
}
48+
49+
public void setToolId(int toolId) {
50+
this.toolId = toolId;
51+
}
52+
53+
public String getToolName() {
54+
return toolName;
55+
}
56+
57+
public void setToolName(String toolName) {
58+
this.toolName = toolName;
59+
}
60+
61+
public double getToolPrice() {
62+
return toolPrice;
63+
}
64+
65+
public void setToolPrice(double toolPrice) {
66+
this.toolPrice = toolPrice;
67+
}
68+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.factorybean;
2+
3+
import org.springframework.beans.factory.config.AbstractFactoryBean;
4+
5+
//no need to set singleton property because default value is true
6+
public class SingleToolFactory extends AbstractFactoryBean<Tool> {
7+
private int factoryId;
8+
private int toolId;
9+
private String toolName;
10+
private double toolPrice;
11+
12+
@Override
13+
public Class<?> getObjectType() {
14+
return Tool.class;
15+
}
16+
17+
@Override
18+
protected Tool createInstance() throws Exception {
19+
return new Tool(toolId, toolName, toolPrice);
20+
}
21+
22+
public int getFactoryId() {
23+
return factoryId;
24+
}
25+
26+
public void setFactoryId(int factoryId) {
27+
this.factoryId = factoryId;
28+
}
29+
30+
public int getToolId() {
31+
return toolId;
32+
}
33+
34+
public void setToolId(int toolId) {
35+
this.toolId = toolId;
36+
}
37+
38+
public String getToolName() {
39+
return toolName;
40+
}
41+
42+
public void setToolName(String toolName) {
43+
this.toolName = toolName;
44+
}
45+
46+
public double getToolPrice() {
47+
return toolPrice;
48+
}
49+
50+
public void setToolPrice(double toolPrice) {
51+
this.toolPrice = toolPrice;
52+
}
53+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.factorybean;
2+
3+
public class Tool {
4+
private int id;
5+
private String name;
6+
private double price;
7+
8+
public Tool() {
9+
}
10+
11+
public Tool(int id, String name, double price) {
12+
this.id = id;
13+
this.name = name;
14+
this.price = price;
15+
}
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public double getPrice() {
34+
return price;
35+
}
36+
37+
public void setPrice(double price) {
38+
this.price = price;
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.factorybean;
2+
3+
import org.springframework.beans.factory.FactoryBean;
4+
5+
public class ToolFactory implements FactoryBean<Tool> {
6+
private int factoryId;
7+
private int toolId;
8+
private String toolName;
9+
private double toolPrice;
10+
11+
@Override
12+
public Tool getObject() throws Exception {
13+
return new Tool(toolId, toolName, toolPrice);
14+
}
15+
16+
@Override
17+
public Class<?> getObjectType() {
18+
return Tool.class;
19+
}
20+
21+
@Override
22+
public boolean isSingleton() {
23+
return false;
24+
}
25+
26+
public int getFactoryId() {
27+
return factoryId;
28+
}
29+
30+
public void setFactoryId(int factoryId) {
31+
this.factoryId = factoryId;
32+
}
33+
34+
public int getToolId() {
35+
return toolId;
36+
}
37+
38+
public void setToolId(int toolId) {
39+
this.toolId = toolId;
40+
}
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+
}
57+
}

0 commit comments

Comments
 (0)