728x90
스프링 프로퍼티 설정
- 자바쪽의 ArrayList 대신에 프로퍼티에서 <list>를 사용해도 됨
스프링 컨테이너
예제
예제 1
<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
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
|
//BMICalculator - 비만도를 측정
package com.javalec.spring_4_1;
public class BMICalculator {
private double lowWeight;
private double normal;
private double overWeight;
private double obesity;
public void bmiCalculation(double weight, double height) {
double h = height * 0.01;
double result = weight / (h*h);
System.out.println("BMI 지수 : "+(int)result);
if (result > obesity) {
System.out.println("비만입니다.");
}else if (result > overWeight) {
System.out.println("과체중입니다.");
}else if (result > normal) {
System.out.println("정상입니다.");
}else {
System.out.println("저체중입니다.");
}
}
public double getLowWeight() {
return lowWeight;
}
public void setLowWeight(double lowWeight) {
this.lowWeight = lowWeight;
}
public double getNormal() {
return normal;
}
public void setNormal(double normal) {
this.normal = normal;
}
public double getOverWeight() {
return overWeight;
}
public void setOverWeight(double overWeight) {
this.overWeight = overWeight;
}
public double getObesity() {
return obesity;
}
public void setObesity(double obesity) {
this.obesity = obesity;
}
}
//MyInfo - 나의 정보
package com.javalec.spring_4_1;
import java.util.ArrayList;
public class MyInfo {
private String name;
private double height;
private double weight;
private ArrayList<String> hobbys;
private BMICalculator bmiCalculator;
public void bmiCalculation() {
bmiCalculator.bmiCalculation(weight, height);
}
public void getInfo() {
System.out.println("이름: "+name);
System.out.println("키: "+height);
System.out.println("몸무게: "+weight);
System.out.println("취미: "+hobbys);
bmiCalculation();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public ArrayList<String> getHobbys() {
return hobbys;
}
public void setHobbys(ArrayList<String> hobbys) {
this.hobbys = hobbys;
}
public BMICalculator getBmiCalculator() {
return bmiCalculator;
}
public void setBmiCalculator(BMICalculator bmiCalculator) {
this.bmiCalculator = bmiCalculator;
}
}
|
cs |
<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
|
<?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 class="com.javalec.spring_4_1.BMICalculator" id="bmiCalculator">
<property name="lowWeight" value="18.5"></property>
<property name="normal" value="23"></property>
<property name="overWeight">
<value>25</value>
</property>
<property name="obesity">
<value>30</value>
</property>
</bean>
<bean class="com.javalec.spring_4_1.MyInfo" id="myInfo">
<property name="name">
<value>홍길동</value>
</property>
<property name="height">
<value>187</value>
</property>
<property name="weight">
<value>84</value>
</property>
<property name="hobbys">
<list>
<value>수영</value>
<value>요리</value>
<value>독서</value>
</list>
</property>
<property name="bmiCalculator">
<ref bean="bmiCalculator"/>
</property>
</bean>
</beans>
|
cs |
- bmiCalculator에 위의 모든 값들이 세팅됨
<MainClass.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.javalec.spring_4_1;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
String configLoc = "classpath:myInfoCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyInfo myInfo = ctx.getBean("myInfo",MyInfo.class);
myInfo.getInfo();
ctx.close();
}
}
|
cs |
- MyInfo 클래스에 있는 getInfo() 메서드에 모든 정보가 담겼기 때문에 getInfo() 메서드를 호출
DI 활용 - 의존관계
생성자를 사용해서 student1 세팅
setter를 통해서 student2 세팅
예제
<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
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
|
//Student
package com.javalec.spring_5_1;
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;
}
}
//StudentInfo
package com.javalec.spring_5_1;
public class StudentInfo {
private Student student;
//생성자
public StudentInfo(Student student) {
this.student = student;
}
//Studnet클래스 타입의 객체를 통해서 학생 정보 출력
public void getStudentInfo() {
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;
}
}
//MainClass
package com.javalec.spring_5_1;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
String configLoc = "classpath:applicationCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
StudentInfo studentInfo = ctx.getBean("studentInfo",StudentInfo.class);
//StudentInfo메서드가 홍길동 정보를 가지고 있기 때문에 홍길동 출력(student1)
studentInfo.getStudentInfo();
Student student2 = ctx.getBean("student2",Student.class);
//student2 정보를 세팅
studentInfo.setStudent(student2);
studentInfo.getStudentInfo();
ctx.close();
}
}
|
cs |
<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 class="com.javalec.spring_5_1.Student" id="student1">
<!-- 생성자를 사용하여 필드에 값 세팅 -->
<!-- 값 넣는 방식의 아래의 두 방식 중 어떤 방식 사용해도 상관 없음 -->
<constructor-arg>
<value>홍길동</value>
</constructor-arg>
<constructor-arg>
<value>10살</value>
</constructor-arg>
<constructor-arg>
<value>3학년</value>
</constructor-arg>
<constructor-arg>
<value>2반</value>
</constructor-arg>
</bean>
<bean class="com.javalec.spring_5_1.Student" id="student2">
<constructor-arg value="홍길순"></constructor-arg>
<constructor-arg value="9살"></constructor-arg>
<constructor-arg value="2학년"></constructor-arg>
<constructor-arg value="1반"></constructor-arg>
</bean>
<bean class="com.javalec.spring_5_1.StudentInfo" id="studentInfo">
<constructor-arg>
<ref bean="student1"/>
</constructor-arg>
</bean>
</beans>
|
cs |
DI 사용에 따른 장점
하나의 id에 여러 클래스를 세팅할 수 있음
예제
<인터페이스>
1
2
3
4
5
6
7
|
package com.javalec.spring_5_2;
public interface Pencil {
//3가지 종류의 연필을 이 인터페이스로부터
//상속받아 메서드 오버라이딩
public void use();
}
|
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
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
|
//Pencil4B
package com.javalec.spring_5_2;
public class Pencil4B implements Pencil{
@Override
public void use() {
System.out.println("4B 입니다.");
}
}
//Pencil6B
package com.javalec.spring_5_2;
public class Pencil6B implements Pencil{
@Override
public void use() {
System.out.println("6B 입니다.");
}
}
//Pencil6BWithEraser
package com.javalec.spring_5_2;
public class Pencil6BWithEraser implements Pencil{
@Override
public void use() {
System.out.println("6B이고, 지우개가 있습니다.");
}
}
//MainClass
package com.javalec.spring_5_2;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX.xml");
//인터페이스 이름으로 클래스를 받아줌
//참조변수의 이름만 바꾸면 여러 클래스를 사용 가능
Pencil pencil = ctx.getBean("pencil",Pencil.class);
pencil.use();
Pencil pencil2 = ctx.getBean("pencil2",Pencil.class);
pencil2.use();
Pencil pencil3 = ctx.getBean("pencil3",Pencil.class);
pencil3.use();
}
}
|
cs |
<xml>
1
2
3
4
5
6
7
8
9
10
|
<?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 class="com.javalec.spring_5_2.Pencil6BWithEraser" id="pencil"></bean>
<bean class="com.javalec.spring_5_2.Pencil6B" id="pencil2"></bean>
<bean class="com.javalec.spring_5_2.Pencil4B" id="pencil3"></bean>
</beans>
|
cs |
문제 1 - 반지름과 원의 넓이
<클래스>
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
|
//Circle
package com.javalec.spring_ex5_1;
public class Circle {
private int radius;
public void process(int radius) {
System.out.println("원의 면적은: "+radius*radius*3.14);
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public Circle(int radius) {
this.radius = radius;
}
}
//CircleInfo
package com.javalec.spring_ex5_1;
public class CircleInfo {
private Circle circle;
public void setCircle(Circle circle) {
this.circle = circle;
}
public CircleInfo(Circle circle) {
this.circle = circle;
}
public void getCircleInfo() {
if (circle!=null) {
int radius = circle.getRadius();
System.out.println("반지름: "+radius);
circle.process(radius);
}
}
}
//MainCircle
package com.javalec.spring_ex5_1;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainCircle {
public static void main(String[] args) {
String configLoc = "classpath:circleCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
CircleInfo circleInfo = ctx.getBean("circleInfo",CircleInfo.class);
circleInfo.getCircleInfo();
Circle circle2 = ctx.getBean("circle2",Circle.class);
circleInfo.setCircle(circle2);
circleInfo.getCircleInfo();
}
}
|
cs |
<circleCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?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 class="com.javalec.spring_ex5_1.Circle" id="circle1">
<constructor-arg>
<value>10</value>
</constructor-arg>
</bean>
<bean class="com.javalec.spring_ex5_1.Circle" id="circle2">
<constructor-arg>
<value>5</value>
</constructor-arg>
</bean>
<bean class="com.javalec.spring_ex5_1.CircleInfo" id="circleInfo">
<constructor-arg>
<ref bean="circle1"/>
</constructor-arg>
</bean>
</beans>
|
cs |
728x90
'학원 > 스프링-학원' 카테고리의 다른 글
생명주기(LIFE CYCLE)와 범위(SCOPE) (0) | 2022.05.11 |
---|---|
DI 설정 방법 (0) | 2022.05.10 |
DI - 스프링을 이용한 객체 생성과 조립 (0) | 2022.05.09 |
스프링 프로젝트 만들기 (0) | 2022.05.09 |
스프링 설치 (0) | 2022.05.09 |