Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch08] 8.3 Use dependency Injection with map, props.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed May 27, 2017
1 parent ff45f7c commit 2231eb9
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/main/java/exam/test10/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package exam.test10;

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();
}
}
38 changes: 38 additions & 0 deletions src/main/java/exam/test10/Engine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package exam.test10;

/**
* 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 + "]";
}
}
32 changes: 32 additions & 0 deletions src/main/java/exam/test10/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package exam.test10;

/**
* Created by nhnent on 2017. 5. 27..
*/

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;

/**
* 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/test10/beans.xml");

System.out.println("[Properties type]------------------");
Tire spareTire = (Tire)ctx.getBean("spareTire");
for (Map.Entry<Object, Object> entry : spareTire.getSpec().entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}

System.out.println("[Map type]-----------------");
Car car1 = (Car)ctx.getBean("car1");
for (Map.Entry<String, Object> entry : car1.getOptions().entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/exam/test10/Tire.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package exam.test10;

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()) : "") + "]";
}
}
41 changes: 41 additions & 0 deletions src/main/java/exam/test10/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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="spareTire" class="exam.test10.Tire">
<property name="maker" value="Hyundai" />
<property name="spec">
<props>
<prop key="width">205</prop>
<prop key="ratio">65</prop>
<prop key="rim.diameter">14</prop>
</props>
</property>
</bean>

<bean id="car1" class="exam.test10.Car">
<constructor-arg value="Avante" />
<constructor-arg>
<bean class="exam.test10.Engine">
<property name="maker" value="Hyundai" />
<property name="cc" value="1495" />
</bean>
</constructor-arg>
<property name="options">
<map>
<entry>
<key>
<value>sunroof</value>
</key>
<value>yes</value>
</entry>
<entry key="airbag" value="dual" />
<entry key="sparetire">
<ref bean="spareTire"></ref>
</entry>
</map>
</property>
</bean>
</beans>

0 comments on commit 2231eb9

Please sign in to comment.