This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ch08] 8.9 Injection bean of java.util.Date.
- Loading branch information
Showing
6 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package exam.test15; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created by nhnent on 2017. 5. 27.. | ||
*/ | ||
public class Car { | ||
private String model; | ||
private Engine engine; | ||
private Tire[] tires; | ||
Map<String, Object> options; | ||
|
||
public Car() { | ||
|
||
} | ||
|
||
public Car(String model, Engine engine) { | ||
this.model = model; | ||
this.engine = engine; | ||
} | ||
|
||
public String getModel() { | ||
return model; | ||
} | ||
|
||
public void setModel(String model) { | ||
this.model = model; | ||
} | ||
|
||
public Engine getEngine() { | ||
return engine; | ||
} | ||
|
||
public void setEngine(Engine engine) { | ||
this.engine = engine; | ||
} | ||
|
||
public Tire[] getTires() { | ||
return tires; | ||
} | ||
|
||
public void setTires(Tire[] tires) { | ||
this.tires = tires; | ||
} | ||
|
||
public Map<String, Object> getOptions() { | ||
return options; | ||
} | ||
|
||
public void setOptions(Map<String, Object> options) { | ||
this.options = options; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuffer carInfo = new StringBuffer(); | ||
carInfo.append("[Car: " + model); | ||
carInfo.append("\n " + engine.toString()); | ||
|
||
if (tires != null) { | ||
for (Tire tire : tires) { | ||
carInfo.append("\n " + tire.toString()); | ||
} | ||
} | ||
carInfo.append("\n]"); | ||
|
||
return carInfo.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package exam.test15; | ||
|
||
/** | ||
* Created by nhnent on 2017. 5. 27.. | ||
*/ | ||
public class Engine { | ||
private String maker; | ||
private int cc; | ||
|
||
public Engine() { | ||
|
||
} | ||
|
||
public Engine(String maker) { | ||
this.maker = maker; | ||
} | ||
|
||
public String getMaker() { | ||
return maker; | ||
} | ||
|
||
public void setMaker(String maker) { | ||
this.maker = maker; | ||
} | ||
|
||
public int getCc() { | ||
return cc; | ||
} | ||
|
||
public void setCc(int cc) { | ||
this.cc = cc; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[Engine: " + maker + ", " + cc + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package exam.test15; | ||
|
||
/** | ||
* Created by nhnent on 2017. 5. 27.. | ||
*/ | ||
|
||
import org.springframework.beans.factory.config.AbstractFactoryBean; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
||
import java.text.SimpleDateFormat; | ||
|
||
/** | ||
* Created by nhnent on 2017. 5. 27.. | ||
*/ | ||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
|
||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("file:/Users/nhnent/workspace/workspace-intellij/java_webdev_workbook_spring/src/main/java/exam/test15/beans.xml"); | ||
|
||
System.out.println("[Use instance factory method]"); | ||
|
||
SimpleDateFormat dateFormat = (SimpleDateFormat)ctx.getBean("dateFormat"); | ||
|
||
Tire hankookTire = (Tire)ctx.getBean("hankookTire"); | ||
System.out.println(hankookTire.getMaker()); | ||
System.out.println(dateFormat.format(hankookTire.getCreatedDate())); | ||
|
||
Tire kumhoTire = (Tire)ctx.getBean("kumhoTire"); | ||
System.out.println(kumhoTire.getMaker()); | ||
System.out.println(dateFormat.format(kumhoTire.getCreatedDate())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package exam.test15; | ||
|
||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Created by nhnent on 2017. 5. 27.. | ||
*/ | ||
public class Tire { | ||
private String maker; | ||
private Properties spec; | ||
private Date createdDate; | ||
|
||
public String getMaker() { | ||
return maker; | ||
} | ||
|
||
public void setMaker(String maker) { | ||
this.maker = maker; | ||
} | ||
|
||
public Properties getSpec() { | ||
return spec; | ||
} | ||
|
||
public void setSpec(Properties spec) { | ||
this.spec = spec; | ||
} | ||
|
||
public Date getCreatedDate() { | ||
return createdDate; | ||
} | ||
|
||
public void setCreatedDate(Date createdDate) { | ||
this.createdDate = createdDate; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuffer specInfo = new StringBuffer(); | ||
|
||
if (spec != null) { | ||
for (Map.Entry<Object, Object> entry : spec.entrySet()) { | ||
specInfo.append(entry.getKey() + ":" + entry.getValue() + ", "); | ||
} | ||
} | ||
|
||
return "[Tire: " + maker + ", " + specInfo.toString() + ((createdDate != null) ? (", " + createdDate.toString()) : "") + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package exam.test15; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Properties; | ||
|
||
import org.springframework.beans.factory.config.AbstractFactoryBean; | ||
|
||
/** | ||
* Created by nhnent on 2017. 5. 27.. | ||
*/ | ||
public class TireFactory extends AbstractFactoryBean<Tire> { | ||
|
||
String maker; | ||
|
||
public void setMaker(String maker) { | ||
this.maker = maker; | ||
} | ||
|
||
@Override | ||
public Class<?> getObjectType() { | ||
return Tire.class; | ||
} | ||
|
||
protected Tire createInstance() { | ||
if (maker.equals("Hankook")) { | ||
return createHankookTire(); | ||
} else { | ||
return createKumhoTire(); | ||
} | ||
} | ||
|
||
private Tire createHankookTire() { | ||
Tire tire = new Tire(); | ||
tire.setMaker("Hankook"); | ||
|
||
Properties specProp = new Properties(); | ||
specProp.setProperty("width", "205"); | ||
specProp.setProperty("ratio", "65"); | ||
specProp.setProperty("rim.diameter", "14"); | ||
tire.setSpec(specProp); | ||
|
||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
||
try { | ||
tire.setCreatedDate(dateFormat.parse("2014-5-5")); | ||
} catch (Exception e) { | ||
|
||
} | ||
|
||
return tire; | ||
} | ||
|
||
private Tire createKumhoTire() { | ||
Tire tire = new Tire(); | ||
tire.setMaker("Kumho"); | ||
|
||
Properties specProp = new Properties(); | ||
specProp.setProperty("width", "185"); | ||
specProp.setProperty("ratio", "75"); | ||
specProp.setProperty("rim.diameter", "16"); | ||
tire.setSpec(specProp); | ||
|
||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
||
try { | ||
tire.setCreatedDate(dateFormat.parse("2014-3-1")); | ||
} catch (Exception e) { | ||
|
||
} | ||
|
||
return tire; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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="dateFormat" class="java.text.SimpleDateFormat" > | ||
<constructor-arg value="yyyy-MM-dd" /> | ||
</bean> | ||
|
||
<bean id="hankookTire" class="exam.test15.Tire" > | ||
<property name="maker" value="Hankook" /> | ||
<property name="createdDate"> | ||
<bean factory-bean="dateFormat" factory-method="parse"> | ||
<constructor-arg value="2014-5-5" /> | ||
</bean> | ||
</property> | ||
</bean> | ||
|
||
<bean id="kumhoTire" class="exam.test15.Tire" > | ||
<property name="maker" value="Kumho" /> | ||
<property name="createdDate"> | ||
<bean factory-bean="dateFormat" factory-method="parse"> | ||
<constructor-arg value="2014-1-14" /> | ||
</bean> | ||
</property> | ||
</bean> | ||
</beans> |