180 likes | 335 Views
생성자를 이용한 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
E N D
생성자를 이용한 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 MainBean.java public class MainBean { public static void main(String[] args) { ClassPathXmlApplicationContextctx = new ClassPathXmlApplicationContext( "edu/seowon/bean/config.xml"); Object obj = ctx.getBean("first"); System.out.println(((First)obj).getName()); } } - ClassPathXmlApplicationContext클래스를 이용하여 스프링에서 사용할 외부조립기(config-bean.xml) 로딩
생성자를 이용한 DI First.java public class First { private String name = null; public First() {} public First(String name) { this.name = name; } public String getName() { return name; } }
생성자를 이용한 방법 (스프링 방식을 사용하지 않을 경우)
Setter 메소드를 이용한 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> <bean id="second" class="edu.seowon.bean.Second"> <property name="age"> <value>18</value> </property> </bean> </beans>
Setter 메소드를 이용한 DI MainBean.java public class MainBean { public static void main(String[] args) { ClassPathXmlApplicationContextctx = 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()); } }
Setter 메소드를 이용한 DI Second.java public class Second { private int age = 0; public Second() {} public void setAge(int age) { this.age = age; } public intgetAge() { return age; } }
Setter 메소드를 이용한 방법 (스프링 방식을 사용하지 않을 경우)
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>
Bean 객체(클래스) 주입 MainBean.java public class MainBean { public static void main(String[] args) { ClassPathXmlApplicationContextctx = 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()); } }
Bean 객체(클래스) 주입 Third.java 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; } }
List / Map 주입 xml <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>
List / Map 주입 EtcInjection.java 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); } } }
Bean 선언 및 사용 실습 ㅇSpringExam프로젝트 생성 ㅇ 패키지 : exam.spring.bean ㅇ 클래스 생성 - MainBean.java : xml 설정을 불러들인 후 동작하는 메인 클래스 - AndroidPhone.java : 앱을주입받은 후 구동시킬 클래스 - KakaoTalk.java : AndroidPhone클래스로 주입될 클래스 - Tmap.java : AndroidPhone클래스로 주입될 클래스 - config.xml : 스프링에서 사용할 Bean 클래스 선언 및 설정 파일
Bean 선언 및 사용 실습 MainBean.java public class MainBean { public static void main(String[] args) { … 생략 … AndroidPhone phone = (AndroidPhone) ctx.getBean("androidPhone"); phone.runKakaoTalk(); phone.runTmap(); } }
Bean 선언 및 사용 실습 AndroidPhone.java public class AndroidPhone { private KakaoTalkkakaoTalk = null; private Tmaptmap = null; public void runKakaoTalk() { kakaoTalk.execute(); } public void runTmap() { tmap.execute(); } // kakaoTalk setter 메소드DI // Tmap생성자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!"); } }
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 선언 --> <!-- tmapbean 선언 --> </beans>