[SPRING] 제 06강. DI설정 방법

- 6.1 XML파일을 이용한 DI설정 방법

- 6.2 JAVA를 이용한 DI설정 방법 (별로안씀)

- 6.3 XML과 JAVA를 같이 사용하는 방법 (별로안씀)





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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 configLocation1 = "classpath:applicationCTX.xml";
        String configLocation2 = "classpath:applicationCTX1.xml";
        AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocation1, configLocation2);
        
        Student student1 = ctx.getBean("student1", Student.class);
        System.out.println(student1.getName());    //홍길동
        System.out.println(student1.getHobbys());    // 수영, 요리
        
        StudentInfo studentInfo = ctx.getBean("studentInfo1", StudentInfo.class);
        Student student2 = studentInfo.getStudent();    //student1  == student2
        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);
        System.out.println(student3.getName());
        
        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();
        
    }
    
}
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
    
    // construct로 설정한다
    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;
    }
    
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
    public ArrayList<String> getHobbys() {
        return hobbys;
    }
    
    public double getHeight() {
        return height;
    }
    
    public double getWeight() {
        return weight;
    }
    
}
 
cs


StudentInfo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.javalec.ex;
 
public class StudentInfo {
 
    private Student student;
    
    public StudentInfo() {
        // TODO Auto-generated constructor stub
    }
    
    public void setStudent(Student student) {
        this.student = student;
    }
    
    public Student getStudent() {
        return student;
    }
    
}
 
cs


Family.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 Family {
 
    String papaName;
    String mamiName;
    String sisterName;
    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;
    }
    
    
    
}
 
cs



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
<?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="10" />
        <constructor-arg>
            <list>
                <value>수영</value>
                <value>요리</value>
            </list>
        </constructor-arg>
        
        
        
        
        <property name="height">
            <value>187</value>  
        </property>
        
        <property name="weight" value="84" />
    </bean>
    
    <bean id="studentInfo1" class="com.javalec.ex.StudentInfo">
        <property name="student">
            <ref bean="student1"/>
        </property>
    </bean>
    
 
</beans>
 
cs



applicationCTX1.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
<?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="8" />
        <constructor-arg>
            <list>
                <value>줄넘기</value>
                <value>공기놀이</value>
            </list>
        </constructor-arg>
        
        <property name="height">
            <value>126</value>
        </property>
        
        <property name="weight" value="21" />
    </bean>
    
    <bean id="family" class="com.javalec.ex.Family" c:papaName="홍아빠" c:mamiName="홈엄마" p:sisterName="홍누나">
        <property name="brotherName" value="홍오빠" />
    </bean>
    <!-- c == construct-arg -->
    <!-- p == property -->
</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
25
26
27
28
package com.javalec.ex;
 
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());
        
        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
51
52
53
54
55
56
57
58
59
60
61
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;
    
//    name, age, hobbys는 생성자, hegiht, weight는 setter을 이용함
    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;
    }
    
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
    public ArrayList<String> getHobbys() {
        return hobbys;
    }
    
    public double getHeight() {
        return height;
    }
    
    public double getWeight() {
        return weight;
    }
    
}
 
cs



ApplicationConfig.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
package com.javalec.ex;
 
import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
//'이 클래스는 스프링 설정에 사용되는 클래스 입니다' 라고 명시해주는 어노테이션
@Configuration
public class ApplicationConfig {
 
    // 객체생성
    @Bean
    public Student student1(){
        
        ArrayList<String> hobbys = new ArrayList<String>();
        hobbys.add("수영");
        hobbys.add("요리");
        
        Student student = new Student("홍길동"20, hobbys);
        student.setHeight(180);
        student.setWeight(80);
        
        return student;
    }
    
    @Bean
    public Student student2(){
        
        ArrayList<String> hobbys = new ArrayList<String>();
        hobbys.add("독서");
        hobbys.add("음악감상");
        
        Student student = new Student("홍길순"18, hobbys);
        student.setHeight(170);
        student.setWeight(55);
        
        return student;
    }
    
}
 
cs


Posted by 너래쟁이
: