FactoryBean example code

This commit is contained in:
DianeDuan 2016-11-20 10:52:09 +08:00
parent 95425deb3c
commit 42fc383518
15 changed files with 582 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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<Tool>, 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;
}
}

View File

@ -0,0 +1,56 @@
package com.baeldung.factorybean;
import org.springframework.beans.factory.config.AbstractFactoryBean;
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
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;
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.factorybean;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.FactoryBean;
public class PostConstructToolFactory implements FactoryBean<Tool> {
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;
}
}

View File

@ -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<Tool> {
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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.factorybean;
import org.springframework.beans.factory.FactoryBean;
public class ToolFactory implements FactoryBean<Tool> {
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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,39 @@
<?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="singleTool" class="com.baeldung.factorybean.SingleToolFactory">
<property name="factoryId" value="3001"/>
<property name="toolId" value="1"/>
<property name="toolName" value="screwdriver"/>
<property name="toolPrice" value="1.5"/>
</bean>
<bean id="nonSingleTool" class="com.baeldung.factorybean.NonSingleToolFactory">
<property name="factoryId" value="3002"/>
<property name="toolId" value="2"/>
<property name="toolName" value="screwdriver"/>
<property name="toolPrice" value="1.5"/>
</bean>
<bean id="worker1" class="com.baeldung.factorybean.Worker">
<property name="number" value="50001"/>
<property name="tool" ref="singleTool"/>
</bean>
<bean id="worker2" class="com.baeldung.factorybean.Worker">
<property name="number" value="50002"/>
<property name="tool" ref="singleTool"/>
</bean>
<bean id="worker3" class="com.baeldung.factorybean.Worker">
<property name="number" value="50003"/>
<property name="tool" ref="nonSingleTool"/>
</bean>
<bean id="worker4" class="com.baeldung.factorybean.Worker">
<property name="number" value="50004"/>
<property name="tool" ref="nonSingleTool"/>
</bean>
</beans>

View File

@ -0,0 +1,17 @@
<?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="initializationTool" class="com.baeldung.factorybean.InitializationToolFactory">
<property name="factoryId" value="1010"/>
<property name="toolId" value="1"/>
<property name="toolName" value="screwdriver"/>
<property name="toolPrice" value="-1"/>
</bean>
<bean id="worker" class="com.baeldung.factorybean.Worker">
<property name="number" value="36"/>
<property name="tool" ref="initializationTool"/>
</bean>
</beans>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="postConstructTool" class="com.baeldung.factorybean.PostConstructToolFactory">
<property name="factoryId" value="2020"/>
<property name="toolId" value="1"/>
<property name="toolName" value=""/>
<property name="toolPrice" value="2.2"/>
</bean>
<bean id="worker" class="com.baeldung.factorybean.Worker">
<property name="number" value="37"/>
<property name="tool" ref="postConstructTool"/>
</bean>
</beans>

View File

@ -0,0 +1,17 @@
<?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="tool" class="com.baeldung.factorybean.ToolFactory">
<property name="factoryId" value="9090"/>
<property name="toolId" value="1"/>
<property name="toolName" value="screwdriver"/>
<property name="toolPrice" value="1.5"/>
</bean>
<bean id="worker" class="com.baeldung.factorybean.Worker">
<property name="number" value="1001"/>
<property name="tool" ref="tool"/>
</bean>
</beans>

View File

@ -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());
}
}

View File

@ -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");
}
}

View File

@ -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));
}
}