simplify demos

This commit is contained in:
DianeDuan 2016-11-27 12:23:51 +08:00
parent e84957c768
commit 361874d0d6
17 changed files with 84 additions and 394 deletions

View File

@ -6,20 +6,15 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class FactoryBeanAppConfig {
@Bean
public ToolFactory tool() {
public ToolFactory toolFactory() {
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;
public Tool tool() throws Exception {
return toolFactory().getObject();
}
}

View File

@ -1,67 +0,0 @@
package com.baeldung.factorybean;
import static com.google.common.base.Preconditions.checkArgument;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;
public class InitializationToolFactory implements FactoryBean<Tool>, InitializingBean {
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
@Override
public void afterPropertiesSet() throws Exception {
checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
checkArgument(toolPrice >= 0, "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

@ -5,8 +5,6 @@ import org.springframework.beans.factory.config.AbstractFactoryBean;
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
public NonSingleToolFactory() {
setSingleton(false);
@ -19,7 +17,7 @@ public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
@Override
protected Tool createInstance() throws Exception {
return new Tool(toolId, toolName, toolPrice);
return new Tool(toolId);
}
public int getFactoryId() {
@ -37,20 +35,4 @@ public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
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

@ -1,68 +0,0 @@
package com.baeldung.factorybean;
import static com.google.common.base.Preconditions.checkArgument;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.StringUtils;
public class PostConstructToolFactory implements FactoryBean<Tool> {
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
@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() {
checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
checkArgument(toolPrice >= 0, "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

@ -6,8 +6,6 @@ import org.springframework.beans.factory.config.AbstractFactoryBean;
public class SingleToolFactory extends AbstractFactoryBean<Tool> {
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
@Override
public Class<?> getObjectType() {
@ -16,7 +14,7 @@ public class SingleToolFactory extends AbstractFactoryBean<Tool> {
@Override
protected Tool createInstance() throws Exception {
return new Tool(toolId, toolName, toolPrice);
return new Tool(toolId);
}
public int getFactoryId() {
@ -34,20 +32,4 @@ public class SingleToolFactory extends AbstractFactoryBean<Tool> {
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

@ -2,16 +2,12 @@ package com.baeldung.factorybean;
public class Tool {
private int id;
private String name;
private double price;
public Tool() {
}
public Tool(int id, String name, double price) {
public Tool(int id) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
@ -21,20 +17,4 @@ public class Tool {
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

@ -5,12 +5,10 @@ import org.springframework.beans.factory.FactoryBean;
public class ToolFactory implements FactoryBean<Tool> {
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
@Override
public Tool getObject() throws Exception {
return new Tool(toolId, toolName, toolPrice);
return new Tool(toolId);
}
@Override
@ -38,20 +36,4 @@ public class ToolFactory implements FactoryBean<Tool> {
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

@ -1,30 +0,0 @@
package com.baeldung.factorybean;
public class Worker {
private String number;
private Tool tool;
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

@ -6,34 +6,10 @@
<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

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
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

@ -6,12 +6,5 @@
<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

@ -4,32 +4,36 @@ 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;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:factorybean-abstract-spring-ctx.xml" })
public class AbstractFactoryBeanTest {
@Resource(name = "singleTool")
private Tool tool1;
@Resource(name = "singleTool")
private Tool tool2;
@Resource(name = "nonSingleTool")
private Tool tool3;
@Resource(name = "nonSingleTool")
private Tool tool4;
@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());
assertThat(tool1.getId(), equalTo(1));
assertTrue(tool1 == tool2);
}
@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());
assertThat(tool3.getId(), equalTo(2));
assertThat(tool4.getId(), equalTo(2));
assertTrue(tool3 != tool4);
}
}

View File

@ -1,17 +0,0 @@
package com.baeldung.factorybean;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FactoryBeanInitializeTest {
@Test(expected = BeanCreationException.class)
public void testInitializationToolFactory() {
new ClassPathXmlApplicationContext("classpath:factorybean-init-spring-ctx.xml");
}
@Test(expected = BeanCreationException.class)
public void testPostConstructToolFactory() {
new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml");
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.factorybean;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = FactoryBeanAppConfig.class)
public class FactoryBeanJavaConfigTest {
@Resource
private Tool tool;
@Resource(name = "&toolFactory")
private ToolFactory toolFactory;
@Test
public void testConstructWorkerByJava() {
assertThat(tool.getId(), equalTo(2));
assertThat(toolFactory.getFactoryId(), equalTo(7070));
}
}

View File

@ -1,39 +0,0 @@
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));
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.factorybean;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:factorybean-spring-ctx.xml" })
public class FactoryBeanXmlConfigTest {
@Resource
private Tool tool;
@Resource(name = "&tool")
private ToolFactory toolFactory;
@Test
public void testConstructWorkerByXml() {
assertThat(tool.getId(), equalTo(1));
assertThat(toolFactory.getFactoryId(), equalTo(9090));
}
}