뚜벅이

왕초보가 스프링 sts 4 에서 bean 갖고놀기 본문

국비과정 JAVA공부

왕초보가 스프링 sts 4 에서 bean 갖고놀기

ZZM 2021. 4. 28. 08:47

폰 관련 bean들 담겨있는 xml파일  

pp.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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?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 name="ps" class="java.util.ArrayList">
 
        <constructor-arg>
            <list>
                <bean class="di.model.Ph">
                    <property name="name" value="폰넘버1" />
                    
                    <property  name="btr"  ref="btr1"  />
                    <property name="cmr" ref="cmr1" />
                    <property name="sz" ref="sz1" />
                </bean>
                <bean class="di.model.Ph">
                    <property name="name" value="갤갤" />
                    <property name="btr" ref="btr2" />
                    <property name="cmr" ref="cmr2" />
                    <property name="sz" ref="sz2" />
                </bean>
                <bean class="di.model.Ph">
                    <property name="name">
                        <value>갤럭시</value>
                    </property>
                    <property name="btr" ref="btr3" />
                    <property name="cmr" ref="cmr3" />
                    <property name="sz" ref="sz3" />
                </bean>
 
 
 
 
            </list>
 
 
 
        </constructor-arg>
 
 
    </bean>
 
 
 
 
    <bean name="ppp1" class="di.model.Ph">
        <property name="name" value="폰넘버1" />
        <property name="btr" ref="btr1" />
        <property name="cmr" ref="cmr1" />
        <property name="sz" ref="sz1" />
    </bean>
    <bean name="ppp2" class="di.model.Ph">
        <property name="name" value="갤갤" />
        <property name="btr" ref="btr2" />
        <property name="cmr" ref="cmr2" />
        <property name="sz" ref="sz2" />
    </bean>
    <bean name="ppp3" class="di.model.Ph">
        <property name="name">
            <value>갤럭시</value>
        </property>
        <property name="btr" ref="btr3" />
        <property name="cmr" ref="cmr3" />
        <property name="sz" ref="sz3" />
    </bean>
 
    <bean name="btr1" class="di.model.Btr">
        <property name="yong">
            <value>2000</value>
        </property>
        <property name="scale" value="중형" />
    </bean>
    <bean name="btr2" class="di.model.Btr">
        <property name="yong">
            <value>6000</value>
        </property>
        <property name="scale" value="소형" />
    </bean>
    <bean name="btr3" class="di.model.Btr">
        <property name="yong">
            <value>12000</value>
        </property>
        <property name="scale" value="대형" />
    </bean>
 
    <bean name="cmr1" class="di.model.Cmr">
        <property name="type" value="전문가용"></property>
        <property name="pixel" value="4500"></property>
    </bean>
    <bean name="cmr2" class="di.model.Cmr">
        <property name="type" value="일반용"></property>
        <property name="pixel" value="1500"></property>
    </bean>
    <bean name="cmr3" class="di.model.Cmr">
        <property name="type" value="천문용"></property>
        <property name="pixel" value="9500"></property>
    </bean>
 
 
    <bean name="sz1" class="di.model.Sz">
        <constructor-arg name="width" value="600" />
        <constructor-arg name="height" value="900" />
    </bean>
    <bean name="sz2" class="di.model.Sz">
        <constructor-arg name="width" value="1600" />
        <constructor-arg name="height" value="1900" />
    </bean>
    <bean name="sz3" class="di.model.Sz">
        <constructor-arg name="width" value="2600" />
        <constructor-arg name="height" value="2900" />
    </bean>
 
 
 
 
 
</beans>
 

dddd

ddd폰 정의 클래스

+
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package di.model;
 
import java.util.Arrays;
 
public class Ph {
 
    String name;
 
    Btr btr;
 
    Cmr cmr;
 
    Sz sz;
 
    @Override
    public String toString() {
        return "폰:: [name=" + name + ", btr=" + btr + ", cmr=" + cmr + ", sz=" + sz + "]\n";
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Btr getBtr() {
        return btr;
    }
 
    public void setBtr(Btr btr) {
        this.btr = btr;
    }
 
    public Cmr getCmr() {
        return cmr;
    }
 
    public void setCmr(Cmr cmr) {
        this.cmr = cmr;
    }
 
    public Sz getSz() {
        return sz;
    }
 
    public void setSz(Sz sz) {
        this.sz = sz;
    }
 
}
 
class Btr {
 
    String yong, scale;
 
    public String getYong() {
        return yong;
    }
 
    public void setYong(String yong) {
        this.yong = yong + " mAh";
    }
 
    public String getScale() {
        return scale;
    }
 
    public void setScale(String scale) {
        this.scale = scale + " 인치";
    }
 
    @Override
    public String toString() {
        return "Btr [yong=" + yong + ", scale=" + scale + "]";
    }
 
}
 
class Cmr {
 
    String type;
    String pixel;
 
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    public String getPixel() {
        return pixel;
    }
 
    public void setPixel(String pixel) {
        this.pixel = pixel + " 만 화소";
    }
 
    @Override
    public String toString() {
        return "Cmr [type=" + type + ", pixel=" + pixel + "]";
    }
 
}
 
class Sz {
    int width, height;
 
    private Sz(int width, int height) {
        this.width = width;
        this.height = height;
 
    }
 
    @Override
    public String toString() {
        return "Sz [width=" + width + ", height=" + height + "]";
    }
 
}
 
cs

 

 

 

 

 

폰 출력 실행 main클래스 

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 di.main;
 
import java.util.ArrayList;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import di.model.Ph;
 
public class PoMain {
 
    static AbstractApplicationContext context = new ClassPathXmlApplicationContext("di_xml/pp.xml");
 
    public static void main(String[] args) {
 
        Ph p1 = (Ph) context.getBean("ppp1""di.model.Ph");
        p1 = context.getBean("ppp1", Ph.class);
        System.out.println("폰 넘버원" + p1);
 
        ArrayList<Ph> arr = new ArrayList<>();
        arr = context.getBean("ps", ArrayList.class);
        System.out.println(arr);
 
    }
}
 
cs

ds

 느낀 점

 

bean 에서 > string으로 받는다 파싱을 해주어야 하는걸까?  "26"  숫자만 넣으면 되는것이 있긴했다.

한마디로 어떤 클래스의  변수가. String 형인데. int 로 setter 를 만들면. 

spring은 헷갈려하는 것 같다. 

  예를들어서 

 

public void setPixel(String pixel) {

        this.pixel = pixel + " 만 화소";

    }

이거를

public void setPixel( int  pixel) {

        this.pixel = pixel + " 만 화소";

    }

로 하고  싶었는데. 

  스프링은 일단 pixel 이 뭔  자료형인지를 따진 뒤에 . 

setter 를 읽는 것 같다.  그래서오류를 밷는데 

  " 얌마  너 pixel 스트링으로 선언 했잖아.  근데 왜 세터는 int로 세터 돌아가게 하느냐 ??

 

라고  하면서  거부한다. "

 bean이 다 해줄것 같지만    내부적으로 자기만의 엄격한 규칙,공식, 형식, 규율 이 있음을 느낄 수 있었다.