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

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

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

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

System.out.println(ctx.getBean("car1"));

Engine engine = (Engine)ctx.getBean("engine1");
engine.setCc(3000);

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

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()) : "") + "]";
}
}
39 changes: 39 additions & 0 deletions src/main/java/exam/test07/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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="engine1" class="exam.test07.Engine">
<constructor-arg>
<value>Hyundai</value>
</constructor-arg>
<property name="cc">
<value>1998</value>
</property>
</bean>

<bean id="car1" class="exam.test07.Car">
<property name="model">
<value>Avante</value>
</property>
<property name="engine">
<ref bean="engine1" />
</property>
</bean>

<bean id="car2" class="exam.test07.Car">
<property name="model" value="Sonata" />
<property name="engine" ref="engine1" />
</bean>

<bean id="car3" class="exam.test07.Car">
<property name="model" value="Grandeur" />
<property name="engine" ref="engine1" />
</bean>

<bean id="car4" class="exam.test07.Car">
<property name="model" value="Equus" />
<property name="engine" ref="engine1" />
</bean>
</beans>

0 comments on commit 4558790

Please sign in to comment.