#DI 설정 방법
오늘은 IOC컨테이너 안에 의존하는 객체인 DI를 설정하는 방법을 알아보자 !
총 3가지의 방법이 있다.
- DI in xml
- DI in java
- java 와 xml 같이 사용
###1. XML파일을 이용한 DI 설정 방법(DI in XML)
x// 일단 Student.java 파일 부터 보자
package com.javalec.ex;
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 void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setHobbys(ArrayList<String> hobbys) {
this.hobbys = hobbys;
}
public void setHeight(double height) {
this.height = height;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
===========================================================================================
// 다음으로 Family.java
package com.javalec.ex;
public class Family {
private String papaName;
private String mamiName;
private String sisterName;
private String brotherName;
public Family(String papaName,String mamiName) {
this.papaName = papaName;
this.mamiName = mamiName;
}
public String getPapaName() {
return papaName;
}
public void setPapaName(String papaName) {
this.papaName = papaName;
}
public String getMamiName() {
return mamiName;
}
public void setMamiName(String mamiName) {
this.mamiName = mamiName;
}
public String getSisterName() {
return sisterName;
}
public void setSisterName(String sisterName) {
this.sisterName = sisterName;
}
public String getBrotherName() {
return brotherName;
}
public void setBrotherName(String brotherName) {
this.brotherName = brotherName;
}
}
===========================================================================================
// 이렇게 두 .java 파일이 있을 때 DI설정 방법
// 첫번째 !
<?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="student1" class="com.javalec.ex.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>
<bean id="studentInfo" class="com.javalec.ex.StudentInfo">
<property name="student">
<ref bean="student1"/>
</property>
</bean>
</beans>
// constructor-age 는 생성자 에서 값을 넣을 때
// property 는 setter를 이용해서 값을 넣을 때
===========================================================================================
// 두번쨰
<?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:c="http://www.springframework.org/schema/c"
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="student3" class="com.javalec.ex.Student">
<constructor-arg value ="홍홍홍"/>
<constructor-arg value ="18"/>
<constructor-arg>
<list>
<value>게임</value>
<value>산책</value>
<value>운동</value>
</list>
</constructor-arg>
<property name="height" value="162"/>
<property name="weight" value="50"/>
</bean>
<bean id ="family" class="com.javalec.ex.Family" c:papaName="홍아빠" c:mamiName="홍엄마" p:sisterName="홍스터">
<property name="brotherName" value="홍브라"></property>
</bean>
</beans>
// 여기서 c(construct)와 p(property)라는 네임스페이스를 이용하고 싶으면
// xmlns:c="http://www.springframework.org/schema/c"
// xmlns:p="http://www.springframework.org/schema/p"
// 이 두가지를 넣어주며 된다. 두 가지방법중 골라서 사용하면 된다.
===========================================================================================
// 마지막으로 MainClass.java 에서 어떻게 사용해야 하는지 보자!
package com.javalec.ex;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
String configLog1 = "classpath:applicationCTX.xml";
String configLog2 = "classpath:applicationCTX1.xml";
//IOC 컨테이너에 두개의 컴포넌트를 삽입
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLog1, configLog2);
Student student1 = ctx.getBean("student1", Student.class);
System.out.println(student1.getName());
System.out.println(student1.getHobbys());
// studentInfo 클래스의 getStudent를 이용하여 어떤것을 참조하는지 확인할 것(applicationCTX.xml에서!)
StudentInfo studentInfo = ctx.getBean("studentInfo", StudentInfo.class);
Student student2 = studentInfo.getStudent();
System.out.println(student2.getName());
System.out.println(student2.getHobbys());
if(student1.equals(student2)) {
System.out.println("student1 == student2");
}
Student student3 = ctx.getBean("student3", Student.class);
if(student1.equals(student3)) {
System.out.println("student1 == student3");
} else {
System.out.println("student1 != student3");
}
Family family = ctx.getBean("family", Family.class);
System.out.println(family.getPapaName());
System.out.println(family.getMamiName());
System.out.println(family.getSisterName());
System.out.println(family.getBrotherName());
ctx.close();
}
}
======결과======
홍길동
[게임, 산책, 운동]
홍길동
[게임, 산책, 운동]
student1 == student2
student1 != student3
홍아빠
홍엄마
홍스터
홍브라
====================================================================================
// StudentInfo 클래스를 깜빡했는데, 위에 결과가 나오기 위해선 getStudent()가 어떻게 작동하는지
// applicationCTX.xml 과 잘 생각하여 확인 !
package com.javalec.ex;
public class StudentInfo {
Student student;
public StudentInfo() {
}
public void setStudent(Student student) {
this.student = student;
}
public Student getStudent() {
return student;
}
}
'Spring' 카테고리의 다른 글
Spring 중복 로그인 (세션바인딩 리스너) 정리 (4) | 2019.04.26 |
---|---|
DI 설정 방법 - JAVA (0) | 2018.09.06 |
DI 설정 방법 - Java & xml 같이 사용 (0) | 2018.09.06 |
Spring 한글 깨짐 현상 (0) | 2018.07.17 |