DI 설정 방법
오늘은 IOC컨테이너 안에 의존하는 객체인 DI를 설정하는 방법을 알아보자 !
총 3가지의 방법이 있다.
- DI in xml
- DI in java
- java 와 xml 같이 사용
3. XML과 JAVA를 같이 사용

1. xml파일에 JAVA파일을 포함시켜 사용 하는방법
DI in xml 과 DI in JAVA와 두개 합친거와 거의 동일하지만, xml파일 안에 configuration 어노테이션을 가져오겠다는 명령문 하나를 삽입해야한다.
// Student.java 부터 보자. 이놈 은 어차피 계속 똑같다.import java.util.ArrayList;public class Student { private String name; private int age; private ArrayList<String> hobbys; private double height; private double weight; public Student(String name, int age, ArrayList<String> hobbys) { this.name = name; this.age = age; this.hobbys = hobbys; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public ArrayList<String> getHobbys() { return hobbys; } public void setHobbys(ArrayList<String> hobbys) { this.hobbys = hobbys; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } ========================================================================== // 다음으로 ApplicationConfig.java , DI in JAVA에서 썼던 코드와 동일, 어노테이션 쓰는 거 잘 알아 둘것. import java.util.ArrayList;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;public class ApplicationConfig { public Student student1(){ ArrayList<String> hobbys = new ArrayList<String>(); hobbys.add("수영"); hobbys.add("요리"); Student student = new Student("홍길동", 20, hobbys); student.setHeight(172); student.setWeight(72); return student; }} ================================================================================ // 마지막으로 applicationCTX.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <context:annotation-config/> <bean class="ApplicationConfig"/> <bean id="student2" class="Student"> <constructor-arg value ="홍길동"/> <constructor-arg value ="27"/> <constructor-arg> <list> <value>게임</value> <value>산책</value> <value>운동</value> </list> </constructor-arg> <property name="height" value="172"/> <property name="weight" value="72"/> </bean> </beans> // 기존과 달라진 것은 ApplicatiopConfig.java 에서 어노테이션한 것을 가져와서 사용하겠다는 // <context:annotation-config/> // <bean class="ApplicationConfig"/> // 추가와 이것을 사용하기 위한 xmlns:context="http://www.springframework.org/schema/context" ==========================================================================================
2. JAVA파일 안에 XML 파일을 가져와 사용하는 방법
xxxxxxxxxximport java.util.ArrayList;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;("classpath:applicationCTX.xml")public class ApplicationConfig { public Student student1(){ ArrayList<String> hobbys = new ArrayList<String>(); hobbys.add("수영"); hobbys.add("요리"); Student student = new Student("홍길동", 20, hobbys); student.setHeight(172); student.setWeight(72); return student; }}============================================================================ // 마지막으로 applicationCTX.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="student2" class="Student"> <constructor-arg value ="홍길동"/> <constructor-arg value ="27"/> <constructor-arg> <list> <value>게임</value> <value>산책</value> <value>운동</value> </list> </constructor-arg> <property name="height" value="172"/> <property name="weight" value="72"/> </bean> </beans>================================================================================// MainClass.java 이놈은 JAVA파일을 이용할 떄와 같이 MainClass를 만들면 된다.import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainClass { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class); Student student1 = ctx.getBean("student1", Student.class); System.out.println("이름 : " +student1.getName()); System.out.println("나이 : " +student1.getAge()); System.out.println("취미 : " +student1.getHobbys()); System.out.println("신장 : " +student1.getHeight()); System.out.println("몸무게 : " +student1.getWeight()); Student student2 = ctx.getBean("student2", Student.class); System.out.println("이름 : " +student2.getName()); System.out.println("나이 : " +student2.getAge()); System.out.println("취미 : " +student2.getHobbys()); System.out.println("신장 : " +student2.getHeight()); System.out.println("몸무게 : " +student2.getWeight()); }}
'Spring' 카테고리의 다른 글
| Spring 중복 로그인 (세션바인딩 리스너) 정리 (4) | 2019.04.26 |
|---|---|
| DI 설정 방법 - xml (0) | 2018.09.06 |
| DI 설정 방법 - JAVA (0) | 2018.09.06 |
| Spring 한글 깨짐 현상 (0) | 2018.07.17 |