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> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,22 +1,21 @@
package com.baeldung.factorybean; package com.baeldung.factorybean;
import static com.google.common.base.Preconditions.checkArgument;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;
public class InitializationToolFactory implements FactoryBean<Tool>, InitializingBean { public class InitializationToolFactory implements FactoryBean<Tool>, InitializingBean {
private int factoryId;// standard setters and getters private int factoryId;
private int toolId;// standard setters and getters private int toolId;
private String toolName;// standard setters and getters private String toolName;
private double toolPrice;// standard setters and getters private double toolPrice;
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
if (toolName == null || toolName.equals("")) { checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
throw new IllegalArgumentException("tool name cannot be empty"); checkArgument(toolPrice >= 0, "tool price should not be less than 0");
}
if (toolPrice < 0) {
throw new IllegalArgumentException("tool price should not be less than 0");
}
} }
@Override @Override

View File

@ -3,10 +3,10 @@ package com.baeldung.factorybean;
import org.springframework.beans.factory.config.AbstractFactoryBean; import org.springframework.beans.factory.config.AbstractFactoryBean;
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> { public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
private int factoryId;// standard setters and getters private int factoryId;
private int toolId;// standard setters and getters private int toolId;
private String toolName;// standard setters and getters private String toolName;
private double toolPrice;// standard setters and getters private double toolPrice;
public NonSingleToolFactory() { public NonSingleToolFactory() {
setSingleton(false); setSingleton(false);

View File

@ -1,14 +1,17 @@
package com.baeldung.factorybean; package com.baeldung.factorybean;
import static com.google.common.base.Preconditions.checkArgument;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.StringUtils;
public class PostConstructToolFactory implements FactoryBean<Tool> { public class PostConstructToolFactory implements FactoryBean<Tool> {
private int factoryId;// standard setters and getters private int factoryId;
private int toolId;// standard setters and getters private int toolId;
private String toolName;// standard setters and getters private String toolName;
private double toolPrice;// standard setters and getters private double toolPrice;
@Override @Override
public Tool getObject() throws Exception { public Tool getObject() throws Exception {
@ -27,12 +30,8 @@ public class PostConstructToolFactory implements FactoryBean<Tool> {
@PostConstruct @PostConstruct
public void checkParams() { public void checkParams() {
if (toolName == null || toolName.equals("")) { checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
throw new IllegalArgumentException("tool name cannot be empty"); checkArgument(toolPrice >= 0, "tool price should not be less than 0");
}
if (toolPrice < 0) {
throw new IllegalArgumentException("tool price should not be less than 0");
}
} }
public int getFactoryId() { 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 //no need to set singleton property because default value is true
public class SingleToolFactory extends AbstractFactoryBean<Tool> { public class SingleToolFactory extends AbstractFactoryBean<Tool> {
private int factoryId;// standard setters and getters private int factoryId;
private int toolId;// standard setters and getters private int toolId;
private String toolName;// standard setters and getters private String toolName;
private double toolPrice;// standard setters and getters private double toolPrice;
@Override @Override
public Class<?> getObjectType() { public Class<?> getObjectType() {

View File

@ -1,9 +1,9 @@
package com.baeldung.factorybean; package com.baeldung.factorybean;
public class Tool { public class Tool {
private int id;// standard setters and getters private int id;
private String name;// standard setters and getters private String name;
private double price;// standard setters and getters private double price;
public Tool() { public Tool() {
} }

View File

@ -3,10 +3,10 @@ package com.baeldung.factorybean;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
public class ToolFactory implements FactoryBean<Tool> { public class ToolFactory implements FactoryBean<Tool> {
private int factoryId;// standard setters and getters private int factoryId;
private int toolId;// standard setters and getters private int toolId;
private String toolName;// standard setters and getters private String toolName;
private double toolPrice;// standard setters and getters private double toolPrice;
@Override @Override
public Tool getObject() throws Exception { public Tool getObject() throws Exception {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,17 +1,16 @@
package com.baeldung.factorybean; package com.baeldung.factorybean;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FactoryBeanInitializeTest { public class FactoryBeanInitializeTest {
@Test(expected = Exception.class) @Test(expected = Exception.class)
public void testInitializationToolFactory() { 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) @Test(expected = Exception.class)
public void testPostConstructToolFactory() { public void testPostConstructToolFactory() {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml"); new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml");
} }
} }