From 42fc383518761be292e36041cc3d0f81f27ed7b2 Mon Sep 17 00:00:00 2001 From: DianeDuan Date: Sun, 20 Nov 2016 10:52:09 +0800 Subject: [PATCH] FactoryBean example code --- .../factorybean/FactoryBeanAppConfig.java | 25 +++++++ .../InitializationToolFactory.java | 68 ++++++++++++++++++ .../factorybean/NonSingleToolFactory.java | 56 +++++++++++++++ .../factorybean/PostConstructToolFactory.java | 69 +++++++++++++++++++ .../factorybean/SingleToolFactory.java | 53 ++++++++++++++ .../java/com/baeldung/factorybean/Tool.java | 40 +++++++++++ .../com/baeldung/factorybean/ToolFactory.java | 57 +++++++++++++++ .../java/com/baeldung/factorybean/Worker.java | 30 ++++++++ .../factorybean-abstract-spring-ctx.xml | 39 +++++++++++ .../resources/factorybean-init-spring-ctx.xml | 17 +++++ .../factorybean-postconstruct-spring-ctx.xml | 20 ++++++ .../main/resources/factorybean-spring-ctx.xml | 17 +++++ .../factorybean/AbstractFactoryBeanTest.java | 35 ++++++++++ .../FactoryBeanInitializeTest.java | 17 +++++ .../baeldung/factorybean/FactoryBeanTest.java | 39 +++++++++++ 15 files changed, 582 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/factorybean/FactoryBeanAppConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/factorybean/Tool.java create mode 100644 spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/factorybean/Worker.java create mode 100644 spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml create mode 100644 spring-core/src/main/resources/factorybean-init-spring-ctx.xml create mode 100644 spring-core/src/main/resources/factorybean-postconstruct-spring-ctx.xml create mode 100644 spring-core/src/main/resources/factorybean-spring-ctx.xml create mode 100644 spring-core/src/test/java/com/baeldung/factorybean/AbstractFactoryBeanTest.java create mode 100644 spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanInitializeTest.java create mode 100644 spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanTest.java diff --git a/spring-core/src/main/java/com/baeldung/factorybean/FactoryBeanAppConfig.java b/spring-core/src/main/java/com/baeldung/factorybean/FactoryBeanAppConfig.java new file mode 100644 index 0000000000..ab36df27cb --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/factorybean/FactoryBeanAppConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.factorybean; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class FactoryBeanAppConfig { + @Bean + public ToolFactory tool() { + ToolFactory factory = new ToolFactory(); + factory.setFactoryId(7070); + factory.setToolId(2); + factory.setToolName("wrench"); + factory.setToolPrice(3.7); + return factory; + } + + @Bean + public Worker worker() throws Exception { + Worker worker = new Worker(); + worker.setNumber("1002"); + worker.setTool(tool().getObject()); + return worker; + } +} diff --git a/spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java new file mode 100644 index 0000000000..925ba2d8e4 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java @@ -0,0 +1,68 @@ +package com.baeldung.factorybean; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; + +public class InitializationToolFactory implements FactoryBean, InitializingBean { + private int factoryId;// standard setters and getters + private int toolId;// standard setters and getters + private String toolName;// standard setters and getters + private double toolPrice;// standard setters and getters + + @Override + public void afterPropertiesSet() throws Exception { + if (toolName == null || toolName.equals("")) { + throw new IllegalArgumentException("tool name cannot be empty"); + } + if (toolPrice < 0) { + throw new IllegalArgumentException("tool price should not be less than 0"); + } + } + + @Override + public Tool getObject() throws Exception { + return new Tool(toolId, toolName, toolPrice); + } + + @Override + public Class getObjectType() { + return Tool.class; + } + + @Override + public boolean isSingleton() { + return false; + } + + public int getFactoryId() { + return factoryId; + } + + public void setFactoryId(int factoryId) { + this.factoryId = factoryId; + } + + public int getToolId() { + return toolId; + } + + public void setToolId(int toolId) { + this.toolId = toolId; + } + + public String getToolName() { + return toolName; + } + + public void setToolName(String toolName) { + this.toolName = toolName; + } + + public double getToolPrice() { + return toolPrice; + } + + public void setToolPrice(double toolPrice) { + this.toolPrice = toolPrice; + } +} diff --git a/spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java new file mode 100644 index 0000000000..0cd80eab41 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java @@ -0,0 +1,56 @@ +package com.baeldung.factorybean; + +import org.springframework.beans.factory.config.AbstractFactoryBean; + +public class NonSingleToolFactory extends AbstractFactoryBean { + private int factoryId;// standard setters and getters + private int toolId;// standard setters and getters + private String toolName;// standard setters and getters + private double toolPrice;// standard setters and getters + + public NonSingleToolFactory() { + setSingleton(false); + } + + @Override + public Class getObjectType() { + return Tool.class; + } + + @Override + protected Tool createInstance() throws Exception { + return new Tool(toolId, toolName, toolPrice); + } + + public int getFactoryId() { + return factoryId; + } + + public void setFactoryId(int factoryId) { + this.factoryId = factoryId; + } + + public int getToolId() { + return toolId; + } + + public void setToolId(int toolId) { + this.toolId = toolId; + } + + public String getToolName() { + return toolName; + } + + public void setToolName(String toolName) { + this.toolName = toolName; + } + + public double getToolPrice() { + return toolPrice; + } + + public void setToolPrice(double toolPrice) { + this.toolPrice = toolPrice; + } +} diff --git a/spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java new file mode 100644 index 0000000000..8298f6f4db --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java @@ -0,0 +1,69 @@ +package com.baeldung.factorybean; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.FactoryBean; + +public class PostConstructToolFactory implements FactoryBean { + private int factoryId;// standard setters and getters + private int toolId;// standard setters and getters + private String toolName;// standard setters and getters + private double toolPrice;// standard setters and getters + + @Override + public Tool getObject() throws Exception { + return new Tool(toolId, toolName, toolPrice); + } + + @Override + public Class getObjectType() { + return Tool.class; + } + + @Override + public boolean isSingleton() { + return false; + } + + @PostConstruct + public void checkParams() { + if (toolName == null || toolName.equals("")) { + throw new IllegalArgumentException("tool name cannot be empty"); + } + if (toolPrice < 0) { + throw new IllegalArgumentException("tool price should not be less than 0"); + } + } + + public int getFactoryId() { + return factoryId; + } + + public void setFactoryId(int factoryId) { + this.factoryId = factoryId; + } + + public int getToolId() { + return toolId; + } + + public void setToolId(int toolId) { + this.toolId = toolId; + } + + public String getToolName() { + return toolName; + } + + public void setToolName(String toolName) { + this.toolName = toolName; + } + + public double getToolPrice() { + return toolPrice; + } + + public void setToolPrice(double toolPrice) { + this.toolPrice = toolPrice; + } +} diff --git a/spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java new file mode 100644 index 0000000000..94d68ef113 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java @@ -0,0 +1,53 @@ +package com.baeldung.factorybean; + +import org.springframework.beans.factory.config.AbstractFactoryBean; + +//no need to set singleton property because default value is true +public class SingleToolFactory extends AbstractFactoryBean { + private int factoryId;// standard setters and getters + private int toolId;// standard setters and getters + private String toolName;// standard setters and getters + private double toolPrice;// standard setters and getters + + @Override + public Class getObjectType() { + return Tool.class; + } + + @Override + protected Tool createInstance() throws Exception { + return new Tool(toolId, toolName, toolPrice); + } + + public int getFactoryId() { + return factoryId; + } + + public void setFactoryId(int factoryId) { + this.factoryId = factoryId; + } + + public int getToolId() { + return toolId; + } + + public void setToolId(int toolId) { + this.toolId = toolId; + } + + public String getToolName() { + return toolName; + } + + public void setToolName(String toolName) { + this.toolName = toolName; + } + + public double getToolPrice() { + return toolPrice; + } + + public void setToolPrice(double toolPrice) { + this.toolPrice = toolPrice; + } +} diff --git a/spring-core/src/main/java/com/baeldung/factorybean/Tool.java b/spring-core/src/main/java/com/baeldung/factorybean/Tool.java new file mode 100644 index 0000000000..abdd074e9a --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/factorybean/Tool.java @@ -0,0 +1,40 @@ +package com.baeldung.factorybean; + +public class Tool { + private int id;// standard setters and getters + private String name;// standard setters and getters + private double price;// standard setters and getters + + public Tool() { + } + + public Tool(int id, String name, double price) { + this.id = id; + this.name = name; + this.price = price; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } +} diff --git a/spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java new file mode 100644 index 0000000000..9b2f7fa42e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java @@ -0,0 +1,57 @@ +package com.baeldung.factorybean; + +import org.springframework.beans.factory.FactoryBean; + +public class ToolFactory implements FactoryBean { + private int factoryId;// standard setters and getters + private int toolId;// standard setters and getters + private String toolName;// standard setters and getters + private double toolPrice;// standard setters and getters + + @Override + public Tool getObject() throws Exception { + return new Tool(toolId, toolName, toolPrice); + } + + @Override + public Class getObjectType() { + return Tool.class; + } + + @Override + public boolean isSingleton() { + return false; + } + + public int getFactoryId() { + return factoryId; + } + + public void setFactoryId(int factoryId) { + this.factoryId = factoryId; + } + + public int getToolId() { + return toolId; + } + + public void setToolId(int toolId) { + this.toolId = toolId; + } + + public String getToolName() { + return toolName; + } + + public void setToolName(String toolName) { + this.toolName = toolName; + } + + public double getToolPrice() { + return toolPrice; + } + + public void setToolPrice(double toolPrice) { + this.toolPrice = toolPrice; + } +} diff --git a/spring-core/src/main/java/com/baeldung/factorybean/Worker.java b/spring-core/src/main/java/com/baeldung/factorybean/Worker.java new file mode 100644 index 0000000000..070322a5f9 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/factorybean/Worker.java @@ -0,0 +1,30 @@ +package com.baeldung.factorybean; + +public class Worker { + private String number;// standard setters and getters + private Tool tool;// standard setters and getters + + public Worker() { + } + + public Worker(String number, Tool tool) { + this.number = number; + this.tool = tool; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public Tool getTool() { + return tool; + } + + public void setTool(Tool tool) { + this.tool = tool; + } +} diff --git a/spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml b/spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml new file mode 100644 index 0000000000..2f34e2e1cf --- /dev/null +++ b/spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/main/resources/factorybean-init-spring-ctx.xml b/spring-core/src/main/resources/factorybean-init-spring-ctx.xml new file mode 100644 index 0000000000..f5835c1e70 --- /dev/null +++ b/spring-core/src/main/resources/factorybean-init-spring-ctx.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/main/resources/factorybean-postconstruct-spring-ctx.xml b/spring-core/src/main/resources/factorybean-postconstruct-spring-ctx.xml new file mode 100644 index 0000000000..81e02a0b35 --- /dev/null +++ b/spring-core/src/main/resources/factorybean-postconstruct-spring-ctx.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/main/resources/factorybean-spring-ctx.xml b/spring-core/src/main/resources/factorybean-spring-ctx.xml new file mode 100644 index 0000000000..800e489ba0 --- /dev/null +++ b/spring-core/src/main/resources/factorybean-spring-ctx.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/factorybean/AbstractFactoryBeanTest.java b/spring-core/src/test/java/com/baeldung/factorybean/AbstractFactoryBeanTest.java new file mode 100644 index 0000000000..790107f114 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/factorybean/AbstractFactoryBeanTest.java @@ -0,0 +1,35 @@ +package com.baeldung.factorybean; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class AbstractFactoryBeanTest { + @Test + public void testSingleToolFactory() { + ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-abstract-spring-ctx.xml"); + + Worker worker1 = (Worker) context.getBean("worker1"); + Worker worker2 = (Worker) context.getBean("worker2"); + + assertThat(worker1.getNumber(), equalTo("50001")); + assertThat(worker2.getNumber(), equalTo("50002")); + assertTrue(worker1.getTool() == worker2.getTool()); + } + + @Test + public void testNonSingleToolFactory() { + ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-abstract-spring-ctx.xml"); + + Worker worker3 = (Worker) context.getBean("worker3"); + Worker worker4 = (Worker) context.getBean("worker4"); + + assertThat(worker3.getNumber(), equalTo("50003")); + assertThat(worker4.getNumber(), equalTo("50004")); + assertTrue(worker3.getTool() != worker4.getTool()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanInitializeTest.java b/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanInitializeTest.java new file mode 100644 index 0000000000..851c15a3ec --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanInitializeTest.java @@ -0,0 +1,17 @@ +package com.baeldung.factorybean; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class FactoryBeanInitializeTest { + @Test(expected = Exception.class) + public void testInitializationToolFactory() { + ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-init-spring-ctx.xml"); + } + + @Test(expected = Exception.class) + public void testPostConstructToolFactory() { + ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml"); + } +} diff --git a/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanTest.java b/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanTest.java new file mode 100644 index 0000000000..d06448b63c --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanTest.java @@ -0,0 +1,39 @@ +package com.baeldung.factorybean; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class FactoryBeanTest { + @Test + public void testConstructWorkerByXml() { + ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-spring-ctx.xml"); + + Worker worker = (Worker) context.getBean("worker"); + assertThat(worker.getNumber(), equalTo("1001")); + assertThat(worker.getTool().getId(), equalTo(1)); + assertThat(worker.getTool().getName(), equalTo("screwdriver")); + assertThat(worker.getTool().getPrice(), equalTo(1.5)); + + ToolFactory toolFactory = (ToolFactory) context.getBean("&tool"); + assertThat(toolFactory.getFactoryId(), equalTo(9090)); + } + + @Test + public void testConstructWorkerByJava() { + ApplicationContext context = new AnnotationConfigApplicationContext(FactoryBeanAppConfig.class); + + Worker worker = (Worker) context.getBean("worker"); + assertThat(worker.getNumber(), equalTo("1002")); + assertThat(worker.getTool().getId(), equalTo(2)); + assertThat(worker.getTool().getName(), equalTo("wrench")); + assertThat(worker.getTool().getPrice(), equalTo(3.7)); + + ToolFactory toolFactory = (ToolFactory) context.getBean("&tool"); + assertThat(toolFactory.getFactoryId(), equalTo(7070)); + } +}