[SPRING] 제 05강 DI활용

05-1. 의존 관계

05-2. DI사용에 따른 장점   


applicationCTX.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
36
37
<?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">
    
        <!-- <property name="" value=""/> --><!-- setter를 이용할때 -->
        <constructor-arg<!-- 생성자  -->
            <value>홍길동</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.javalec.ex.Student">
        <constructor-arg value="홍길동" />
        <constructor-arg value="9살" />
        <constructor-arg value="2학년" />
        <constructor-arg value="10번" />
    </bean>
    
    <bean id="studentInfo" class="com.javalec.ex.StudentInfo">
        <constructor-arg>
            <ref bean="student1" />
        </constructor-arg>
    </bean>
    
</beans>
 
cs



MainClass.java

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.javalec.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



Student.java

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.javalec.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




StudentInfo.java

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.javalec.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






MainClass.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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) {
        // TODO Auto-generated method stub
        
        AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX.xml");
        Pencil pencil = ctx.getBean("pencil", Pencil.class); // id,객체의 타입
        pencil.use();
        
        ctx.close();
        
    }
 
}
cs


applicationCTX.xml (class의 이름만 바꿔주면된다!)

1
2
3
4
5
6
7
8
9
<?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="pencil" class="com.javalec.ex.Pencil6BWithEraser" /> --> <!-- 6B굵기로 쓰이고, 지우개가 있습니다 -->
    <!-- <bean id="pencil" class="com.javalec.ex.Pencil6B" /> --> <!-- 6B굵기로 쓰입니다 -->
    <bean id="pencil" class="com.javalec.ex.Pencil4B" /<!-- 4B굵기로 쓰입니다 -->
</beans>
cs


Pencil.java (인터페이스)

1
2
3
4
5
6
package com.javalec.ex;
 
public interface Pencil {
    public void use();
}
 
cs

Pencil4B.java

1
2
3
4
5
6
7
8
9
10
11
package com.javalec.ex;
 
public class Pencil4B implements Pencil {
 
    @Override
    public void use() {
        System.out.println("4B 굵기로 쓰입니다.");
    }
 
}
 
cs

Pencil6B.java

1
2
3
4
5
6
7
8
9
10
11
package com.javalec.ex;
 
public class Pencil6B implements Pencil {
 
    @Override
    public void use() {
        System.out.println("6B굵기로 쓰입니다.");
    }
 
}
 
cs

Pencil6BWithEraser.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.javalec.ex;
 
public class Pencil6BWithEraser implements Pencil {
 
    @Override
    public void use() {
//        super.use();
        
        System.out.println("6B굵기로 쓰이고, 지우개가 있습니다.");
        
    }
    
}
 
cs


Posted by 너래쟁이
: