SPRING - DI 활용

이번엔 SPRING 의 DI를 활용해보도록

하겠습니다. 먼저 테스트할 프로젝트 만들어줍니다

다음 src/main/java 에 패키지하나 만들어주고

메인 클래스 하나 만들어줍니다


메인 클래스는 아래와같이 만들어줍니다



--


다음 테스트할 bean 하나 만들어줍니다



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.spring.ex;
 
public class Student {
 
    private String name;
    private String age;
    private String gradeNum;
    private String classNum;
    
    public Student(String name, String age, String gradeNum, String classNum) {
        this.name = name;
        this.age =  age;
        this.gradeNum = gradeNum;
        this.classNum = classNum;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
 
    public String getGradeNum() {
        return gradeNum;
    }
 
    public void setGradeNum(String gradeNum) {
        this.gradeNum = gradeNum;
    }
 
    public String getClassNum() {
        return classNum;
    }
 
    public void setClassNum(String classNum) {
        this.classNum = classNum;
    }
    
}
 
cs


다음 화면 출력할 파일도 만들어줍니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.spring.ex;
 
public class StudentInfo {
 
    private Student student;
    
    public StudentInfo(Student student) {
        this.student = student;
    }
    
    public void getStudentInfo(){
        if(student != null) {
            System.out.println("이름 : " + student.getName());
            System.out.println("나이 : " + student.getAge());
            System.out.println("학년 : " + student.getGradeNum());
            System.out.println("반 : " + student.getClassNum());
            System.out.println("======================");
        }
    }
    
    public void setStudent(Student student) {
        this.student = student;
    }
    
}
 
cs


이렇게 한 패키지안에 3개의 파일 만들어줍니다



마지막으로 src/main/resources 에

xml 파일 하나 만들어줍니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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.spring.ex.Student">
        <constructor-arg>
            <value>홍길동1</value>
        </constructor-arg>
        <constructor-arg>
            <value>10살</value>
        </constructor-arg>
        <constructor-arg>
            <value>3학년</value>
        </constructor-arg>
        <constructor-arg>
            <value>20번</value>
        </constructor-arg>
    </bean>
    
    <bean id="student2" class="com.spring.ex.Student">
        <constructor-arg value="홍길동2" />
        <constructor-arg value="9살" />
        <constructor-arg value="2학년" />
        <constructor-arg value="10번" />
    </bean>
    
    <bean id="studentInfo" class="com.spring.ex.StudentInfo">
        <constructor-arg>
            <ref bean="student1" />
        </constructor-arg>
    </bean>
    
</beans>
 
cs


xml파일에서는 student1 과 student2를 만들어줍니다

Student.java 빈을 사용해서 2개의 객체를 만들어준겁니다

이때  빈 안에있는 생성자를 사용합니다

<constructor-arg>는 생성자를 이용해

값을 만들때 사용합니다


28line 은 student1을 참조하여 객체를 생성한것입니다

때문에 studentInfo 를 사용하면 

student1의 값들이 출력됩니다

이것을 메인클래스에서 사용되며

studentInfo.getStudentInfo();를 이용하여

studentInfo객체를 사용합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.spring.ex;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
 
    public static void main(String[] args) {
        
        String configLocatioin = "classpath:applicationCTX.xml";
        AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocatioin);
        StudentInfo studentInfo = ctx.getBean("studentInfo", StudentInfo.class);
        studentInfo.getStudentInfo();
        
        Student student2 = ctx.getBean("student2", Student.class);
        studentInfo.setStudent(student2);
        studentInfo.getStudentInfo();
        
        ctx.close();
        
    }
    
}
 
cs



메인클래스에서 는 applicationCTX.xml 파일을

사용한다고 명시해줍니다 다음

AbstractApplicationContext 객체를 생성합니다

11line은 GenericXmlApplicationContext 클래스를 사용하여

configLocatioin 의 값을 ctx로 저장합니다

studentInfo 에 xml파일에있는 studentInfo를 저장합니다.

13line에서 는 xml파일에서 만든 studentInfo 객체를 사용해

값을 출력합니다

15line에서는 xml에서 만든 student2객체값을 가져와서 studentInfo에

값을 넣어줍니다 즉 student1의 값이 지워지고 student2의값이

새로 저장된겁니다. 떄문에 현재 메인클래스를 실행시키면

student1가 한번 출력되고 student2가 출력됩니다.


메인클래스 를 실행시켜보면 콘솔창에 아래와같이

출력 되는것을 확인 할 수 있습니다.







'SPRING' 카테고리의 다른 글

SPRING - 자바를 이용한 DI 설정  (0) 2018.02.06
SPRING - DI의 장점  (0) 2018.02.05
SPRING - DI (Dependency Injection)  (0) 2018.02.05
SPRING - 이클립스에서 SPRING 사용하기  (0) 2018.02.05
SPRING - 스프링이란?  (0) 2018.02.05

댓글

Designed by JB FACTORY