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

이번에 DI설정하는 방법은 바로 JAVA를 이용해서 스프링 설정(DI 설정) 하는 방법이다. 그럼 의존성 객체를 어떻게 생성하는지 소스를 통해 확인하자
x// 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; }}===================================================================================// 그 다음으로 xml 파일을 대신할 java 파일인 ApplicationConfig.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; } public Student student2(){ ArrayList<String> hobbys = new ArrayList<String>(); hobbys.add("독서"); hobbys.add("음악감상"); Student student = new Student("홍홍홍", 15, hobbys); student.setHeight(162); student.setWeight(50); return student; }}// Configuration 은 이것이 스프링 설정 파일!(DI 설정 파일)이다 ! 라고 말하는 것// Bean은 말그대로 객체(컴포넌트) 라고 말하는 것================================================================================== // 마지막으로 MainClass.java 와 실행 결과 이다. 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()); }}이름 : 홍길동나이 : 20취미 : [수영, 요리]신장 : 172.0몸무게 : 72.0이름 : 홍홍홍나이 : 15취미 : [독서, 음악감상]신장 : 162.0몸무게 : 50.0
'Spring' 카테고리의 다른 글
| Spring 중복 로그인 (세션바인딩 리스너) 정리 (4) | 2019.04.26 |
|---|---|
| DI 설정 방법 - xml (0) | 2018.09.06 |
| DI 설정 방법 - Java & xml 같이 사용 (0) | 2018.09.06 |
| Spring 한글 깨짐 현상 (0) | 2018.07.17 |