Car.java
package com.test.diEx05;
public interface Car {
public void drive();
}
HDCar.java
package com.test.diEx05;
public class HDCar implements Car {
public void drive(){
System.out.println("현대차를 운전");
}
}
DWCar.java
package com.test.diEx05;
public class DWCar implements Car{
public void drive(){
System.out.println("대우차 운전");
}
}
KiaCar.java
package com.test.diEx05;
public class KiaCar implements Car {
public void drive(){
System.out.println("기아차 운전");
}
}
Main.java
package com.test.diEx05;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:car.xml");
Car car = ctx.getBean("car",Car.class);
car.drive();
}
}
car.xml
<?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="car" class="com.test.diEx05.KiaCar" />
<!--
<bean id="car" class="com.test.diEx05.HDCar" />
<bean id="car" class="com.test.diEx05.DWCar" />
-->
</beans>
'기존카테고리 > Spring_basic1' 카테고리의 다른 글
자바코드를 이용한 의존관계 설정 (0) | 2017.07.10 |
---|---|
XML 파일을 이용한 의존관계 설정 (0) | 2017.07.10 |
DI - 프로퍼티(property) 방식 (0) | 2017.07.10 |
DI - Constructor(생성자)를 통한 주입 예제2 (0) | 2017.07.10 |
DI - Constructor(생성자)를 통한 주입 (0) | 2017.07.10 |