|
| 1 | +package com.baeldung.factorybean; |
| 2 | + |
| 3 | +import static com.google.common.base.Preconditions.checkArgument; |
| 4 | + |
| 5 | +import org.springframework.beans.factory.FactoryBean; |
| 6 | +import org.springframework.beans.factory.InitializingBean; |
| 7 | +import org.springframework.util.StringUtils; |
| 8 | + |
| 9 | +public class InitializationToolFactory implements FactoryBean<Tool>, InitializingBean { |
| 10 | + private int factoryId; |
| 11 | + private int toolId; |
| 12 | + private String toolName; |
| 13 | + private double toolPrice; |
| 14 | + |
| 15 | + @Override |
| 16 | + public void afterPropertiesSet() throws Exception { |
| 17 | + checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty"); |
| 18 | + checkArgument(toolPrice >= 0, "tool price should not be less than 0"); |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + public Tool getObject() throws Exception { |
| 23 | + return new Tool(toolId, toolName, toolPrice); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public Class<?> getObjectType() { |
| 28 | + return Tool.class; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public boolean isSingleton() { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + public int getFactoryId() { |
| 37 | + return factoryId; |
| 38 | + } |
| 39 | + |
| 40 | + public void setFactoryId(int factoryId) { |
| 41 | + this.factoryId = factoryId; |
| 42 | + } |
| 43 | + |
| 44 | + public int getToolId() { |
| 45 | + return toolId; |
| 46 | + } |
| 47 | + |
| 48 | + public void setToolId(int toolId) { |
| 49 | + this.toolId = toolId; |
| 50 | + } |
| 51 | + |
| 52 | + public String getToolName() { |
| 53 | + return toolName; |
| 54 | + } |
| 55 | + |
| 56 | + public void setToolName(String toolName) { |
| 57 | + this.toolName = toolName; |
| 58 | + } |
| 59 | + |
| 60 | + public double getToolPrice() { |
| 61 | + return toolPrice; |
| 62 | + } |
| 63 | + |
| 64 | + public void setToolPrice(double toolPrice) { |
| 65 | + this.toolPrice = toolPrice; |
| 66 | + } |
| 67 | +} |
0 commit comments