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 list.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed May 27, 2017
1 parent a992e73 commit ff45f7c
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/java/exam/test09/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package exam.test09;

/**
* Created by nhnent on 2017. 5. 27..
*/
public class Car {
private String model;
private Engine engine;
private Tire[] tires;

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;
}

@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/test09/Engine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package exam.test09;

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

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

import org.springframework.context.support.ClassPathXmlApplicationContext;

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

System.out.println(ctx.getBean("car1"));
}
}
41 changes: 41 additions & 0 deletions src/main/java/exam/test09/Tire.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package exam.test09;

import java.util.Date;

/**
* Created by nhnent on 2017. 5. 27..
*/
public class Tire {
private String maker;
private String spec;
private Date createdDate;

public String getMaker() {
return maker;
}

public void setMaker(String maker) {
this.maker = maker;
}

public String getSpec() {
return spec;
}

public void setSpec(String spec) {
this.spec = spec;
}

public Date getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}

@Override
public String toString() {
return "[Tire: " + maker + ", " + spec + ((createdDate != null) ? (", " + createdDate.toString()) : "") + "]";
}
}
36 changes: 36 additions & 0 deletions src/main/java/exam/test09/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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="car1" class="exam.test09.Car">
<constructor-arg value="Avante" />
<constructor-arg>
<bean class="exam.test09.Engine">
<property name="maker" value="Hyundai" />
<property name="cc" value="1495" />
</bean>
</constructor-arg>
<property name="tires">
<list>
<bean class="exam.test09.Tire">
<property name="maker" value="Kumho" />
<property name="spec" value="P185/65R14" />
</bean>
<bean class="exam.test09.Tire">
<property name="maker" value="Kumho" />
<property name="spec" value="P185/65R14" />
</bean>
<bean class="exam.test09.Tire">
<property name="maker" value="Hankook" />
<property name="spec" value="P205/65R14" />
</bean>
<bean class="exam.test09.Tire">
<property name="maker" value="Hankook" />
<property name="spec" value="P205/65R14" />
</bean>
</list>
</property>
</bean>
</beans>

0 comments on commit ff45f7c

Please sign in to comment.