delete extra comments and variable assignments, use guava precondition for check

This commit is contained in:
DianeDuan 2016-11-21 20:41:33 +08:00
parent d13f7b9ed8
commit 77240dc024
13 changed files with 50 additions and 48 deletions

View File

@ -50,6 +50,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
</dependencies>
<build>

View File

@ -1,22 +1,21 @@
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;// 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
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
@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");
}
checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
checkArgument(toolPrice >= 0, "tool price should not be less than 0");
}
@Override

View File

@ -3,10 +3,10 @@ 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
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
public NonSingleToolFactory() {
setSingleton(false);

View File

@ -1,14 +1,17 @@
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;// 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
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
@Override
public Tool getObject() throws Exception {
@ -27,12 +30,8 @@ public class PostConstructToolFactory implements FactoryBean<Tool> {
@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");
}
checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
checkArgument(toolPrice >= 0, "tool price should not be less than 0");
}
public int getFactoryId() {

View File

@ -4,10 +4,10 @@ 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
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
@Override
public Class<?> getObjectType() {

View File

@ -1,9 +1,9 @@
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
private int id;
private String name;
private double price;
public Tool() {
}

View File

@ -3,10 +3,10 @@ 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
private int factoryId;
private int toolId;
private String toolName;
private double toolPrice;
@Override
public Tool getObject() throws Exception {

View File

@ -1,8 +1,8 @@
package com.baeldung.factorybean;
public class Worker {
private String number;// standard setters and getters
private Tool tool;// standard setters and getters
private String number;
private Tool tool;
public Worker() {
}

View File

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

View File

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

View File

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

View File

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

View File

@ -1,17 +1,16 @@
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");
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");
new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml");
}
}