18
생생생생 생생생 DI config.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="first" class="edu.seowon.bean.First"> <constructor-arg> <value>GGoReb</value> </constructor-arg> </bean> </beans>

생성자를 이용한 DI

Embed Size (px)

DESCRIPTION

생성자를 이용한 DI. config.xml.

Citation preview

Page 1: 생성자를  이용한  DI

생성자를 이용한 DIconfig.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="first" class="edu.seowon.bean.First"><constructor-arg>

<value>GGoReb</value></constructor-arg>

</bean>

</beans>

Page 2: 생성자를  이용한  DI

MainBean.java

생성자를 이용한 DI

public class MainBean {

public static void main(String[] args) {ClassPathXmlApplicationContext ctx =

new ClassPathXmlApplicationContext("edu/seowon/bean/config.xml");

Object obj = ctx.getBean("first");System.out.println(((First)obj).getName());

}

}

- ClassPathXmlApplicationContext 클래스를 이용하여 스프링에서 사용할 외부조립기 (config-bean.xml) 로딩

Page 3: 생성자를  이용한  DI

First.java

생성자를 이용한 DI

public class First {private String name = null;

public First() {}

public First(String name) {this.name = name;

}

public String getName() {return name;

}}

Page 4: 생성자를  이용한  DI

생성자를 이용한 방법 ( 스프링 방식을 사용하지 않을 경우 )

Page 5: 생성자를  이용한  DI

Setter 메소드를 이용한 DIconfig.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="first" class="edu.seowon.bean.First"><constructor-arg>

<value>GGoReb</value></constructor-arg>

</bean>

<bean id="second" class="edu.seowon.bean.Second"><property name="age">

<value>18</value></property>

</bean>

</beans>

Page 6: 생성자를  이용한  DI

MainBean.java

Setter 메소드를 이용한 DI

public class MainBean {

public static void main(String[] args) {ClassPathXmlApplicationContext ctx =

new ClassPathXmlApplicationContext("edu/seowon/bean/config.xml");

Object obj = ctx.getBean("first");System.out.println(((First)obj).getName());

Object obj2 = ctx.getBean("second");System.out.println(((Second)obj2).getAge());

}

}

Page 7: 생성자를  이용한  DI

Second.java

Setter 메소드를 이용한 DI

public class Second {private int age = 0;

public Second() {}

public void setAge(int age) {this.age = age;

}

public int getAge() {return age;

}}

Page 8: 생성자를  이용한  DI

Setter 메소드를 이용한 방법 ( 스프링 방식을 사용하지 않을 경우 )

Page 9: 생성자를  이용한  DI

Bean 객체 ( 클래스 ) 주입config.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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

… 생략 …

<bean id="third" class="edu.seowon.bean.Third" p:first-ref="first">

<constructor-arg><ref bean="second" />

</constructor-arg></bean>

</beans>

Page 10: 생성자를  이용한  DI

MainBean.java

Bean 객체 ( 클래스 ) 주입

public class MainBean {

public static void main(String[] args) {ClassPathXmlApplicationContext ctx =

new ClassPathXmlApplicationContext("edu/seowon/bean/config.xml");

… 생략 …

Object obj3 = ctx.getBean("third");

System.out.println(((Third)obj3).getSecond().getAge());System.out.println(((Third)obj3).getFirst().getName());

}

}

Page 11: 생성자를  이용한  DI

Third.java

Bean 객체 ( 클래스 ) 주입

public class Third {private First first = null;private Second second = null;

public Third(Second second) {this.second = second;

}

public Second getSecond() {return second;

}

public void setFirst(First first) {this.first = first;

}

public First getFirst() {return first;

}}

Page 12: 생성자를  이용한  DI

xml

List / Map 주입

<bean id="mapInjection" class="edu.seowon.bean.EtcInjection"><constructor-arg>

<map><entry key="A">

<value>1</value></entry><entry key="B">

<value>2</value></entry><entry key="C">

<value>3</value></entry>

</map></constructor-arg><property name="list">

<list><value>1</value><value>2</value> <value>3</value></list>

</property></bean>

Page 13: 생성자를  이용한  DI

EtcInjection.java

List / Map 주입

public class EtcInjection {public EtcInjection(Map<String, Object> map) {

Set<Entry<String, Object>> set = map.entrySet();Iterator<Entry<String, Object>> iter = set.iterator();System.out.println("=== EtcInjection Constructor ===");while(iter.hasNext()) {

Entry<String, Object> e = iter.next();System.out.println(e.getKey() + " / " + e.getValue());

}}

public void setList(List<String> list) {System.out.println("=== EtcInjection Setter ===");for(String item : list) {

System.out.println(item);}

}}

Page 14: 생성자를  이용한  DI

Bean 선언 및 사용 실습

ㅇ SpringExam 프로젝트 생성

ㅇ 패키지 : exam.spring.bean

ㅇ 클래스 생성 - MainBean.java : xml 설정을 불러들인 후 동작하는 메인 클래스 - AndroidPhone.java : 앱을 주입받은 후 구동시킬 클래스 - KakaoTalk.java : AndroidPhone 클래스로 주입될 클래스 - Tmap.java : AndroidPhone 클래스로 주입될 클래스 - config.xml : 스프링에서 사용할 Bean 클래스 선언 및 설정 파일

Page 15: 생성자를  이용한  DI

Bean 선언 및 사용 실습MainBean.java

public class MainBean {

public static void main(String[] args) {

… 생략 …

AndroidPhone phone = (AndroidPhone) ctx.getBean("androidPhone");

phone.runKakaoTalk();phone.runTmap();

}

}

Page 16: 생성자를  이용한  DI

Bean 선언 및 사용 실습AndroidPhone.java

public class AndroidPhone { private KakaoTalk kakaoTalk = null; private Tmap tmap = null;

public void runKakaoTalk() { kakaoTalk.execute(); }

public void runTmap() { tmap.execute(); }

// kakaoTalk setter 메소드 DI

// Tmap 생성자 DI}

Page 17: 생성자를  이용한  DI

Bean 선언 및 사용 실습KakaoTalk.java

public class KakaoTalk {public void execute() {

System.out.println("KakaoTalk Execute!"); }}

Tmap.java

public class Tmap {public void execute() {

System.out.println("Tmap Execute!"); }}

Page 18: 생성자를  이용한  DI

Bean 선언 및 사용 실습config-bean.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"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="androidPhone" class="exam.spring.bean.AndroidPhone"><!-- kakaoTalk setter 메소드 DI --><!-- tmap 생성자 DI -->

</bean>

<!-- kakaoTalk bean 선언 --><!-- tmap bean 선언 -->

</beans>