DI - 스프링을 이용한 객체 생성과 조립
스프링을 이용한 객체 생성과 조립
빈 설명
<beans> : <bean>의 루트
<bean> : bean 설정 태그
id : 빈의 이름
class : 빈으로 등록할 클래스
<property> : property 설정 태그 - setter과 같은 역할이라고 볼 수 있음
name : class에서 사용할 setter 이름
ref : setter에 주입할 bean(객체)의 이름
스프링 설정 파일의 이해
예제 - 자바소스를 만들고 XML로 컨버전
<Calculator.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.spring_3_1;
public class Calculator {
public void addition(int f, int s) {
System.out.println("addition()");
int result = f+s;
System.out.println(f+"+"+s+"="+result);
}
public void substraction(int f, int s) {
System.out.println("substraction()");
int result = f-s;
System.out.println(f+"-"+s+"="+result);
}
public void multiplication(int f, int s) {
System.out.println("multiplication()");
int result = f*s;
System.out.println(f+"*"+s+"="+result);
}
public void division(int f, int s) {
System.out.println("division()");
int result = f/s;
System.out.println(f+"/"+s+"="+result);
}
}
|
cs |
<MyCalculator.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
|
package com.javalec.spring_3_1;
public class MyCalculator {
private Calculator calculator;
private int firstNum;
private int secondNum;
public void add() {
calculator.addition(firstNum, secondNum);
}
public void sub() {
calculator.substraction(firstNum, secondNum);
}
public void mul() {
calculator.multiplication(firstNum, secondNum);
}
public void div() {
calculator.division(firstNum, secondNum);
}
public Calculator getCalculator() {
return calculator;
}
public void setCalculator(Calculator calculator) {
this.calculator = calculator;
}
public int getFirstNum() {
return firstNum;
}
public void setFirstNum(int firstNum) {
this.firstNum = firstNum;
}
public int getSecondNum() {
return secondNum;
}
public void setSecondNum(int secondNum) {
this.secondNum = secondNum;
}
}
|
cs |
<MainClass.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.javalec.spring_3_1;
public class MainClass {
public static void main(String[] args) {
MyCalculator myCalculator = new MyCalculator();
myCalculator.setCalculator(new Calculator());
myCalculator.setFirstNum(10);
myCalculator.setSecondNum(2);
myCalculator.add();
myCalculator.sub();
myCalculator.mul();
myCalculator.div();
}
}
|
cs |
위의 내용을 XML을 통해서 실행할 것 - 변경하는 건 MainClass.java파일
여기에 applicationCTX.xml 이런 이름의 파일을 만든다
위의 MainClass에서 set부분은 주석처리
<applicationCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?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="calculator" class="com.javalec.spring_3_2.Calculator"></bean>
<!-- id는 변수이름 -->
<bean id="myCalculator" class="com.javalec.spring_3_2.MyCalculator">
<property name="calculator">
<ref bean="calculator"/>
</property>
<property name="firstNum" value="10"></property>
<property name="secondNum" value="2"></property>
</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
25
26
|
package com.javalec.spring_3_2;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
// MyCalculator myCalculator = new MyCalculator();
// myCalculator.setCalculator(new Calculator());
//
// myCalculator.setFirstNum(10);
// myCalculator.setSecondNum(2);
//XML파일의 경로를 가져옴
String configLoc = "classpath:applicationCTX.xml";
//이 메서드에서 xml정보를 파싱하는 것
//업캐스팅 됐음
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
//참조변수 이름은 변해도 되지만, 빈의 이름은 변하면 안됨
MyCalculator myCalculator = ctx.getBean("myCalculator",MyCalculator.class);
myCalculator.add();
myCalculator.sub();
myCalculator.mul();
myCalculator.div();
}
}
|
cs |
문제 1 - 원의 면적 구하기
<Circle.java>
1
2
3
4
5
6
7
8
|
package com.javalec.spring_ex3_1;
public class Circle {
public void area(int radius) {
float result = (float) (radius*radius*3.14);
System.out.println("원의 면적은"+result);
}
}
|
cs |
<MyCircle.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.javalec.spring_ex3_1;
public class MyCircle {
private Circle circle;
private int radius;
public void ar() {
circle.area(radius);
}
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
}
|
cs |
<circleCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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="circle" class="com.javalec.spring_ex3_1.Circle"></bean>
<bean id="myCircle" class="com.javalec.spring_ex3_1.MyCircle">
<property name="circle">
<ref bean="circle"/>
</property>
<property name="radius" value="10"></property>
</bean>
</beans>
|
cs |
<property> - setter 역할
name : class에서 사용할 setter 이름
ref : setter에 주입할 bean(객체)의 이름
<MainCircle.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.javalec.spring_ex3_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);
MyCircle myCircle = ctx.getBean("myCircle",MyCircle.class);
myCircle.ar();
}
}
|
cs |
문제 2 - 사각형 면적
<Rectangle.java>
1
2
3
4
5
6
7
|
package com.javalec.spring_ex3_2;
public class Rectangle {
public void area(int x, int y) {
System.out.println("사각형의 면적은 "+x*y);
}
}
|
cs |
<MyRectangle.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
|
package com.javalec.spring_ex3_2;
public class MyRectangle {
private Rectangle rectangle;
private int x;
private int y;
public void ar() {
rectangle.area(x, y);
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
|
cs |
<rectangleCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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="rectangle" class="com.javalec.spring_ex3_2.Rectangle"></bean>
<bean id="myRectangle" class="com.javalec.spring_ex3_2.MyRectangle">
<property name="rectangle">
<ref bean="rectangle"/>
</property>
<property name="x" value="4"></property>
<property name="y" value="6"></property>
</bean>
</beans>
|
cs |
<MainRectangle.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.javalec.spring_ex3_2;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainRectangle {
public static void main(String[] args) {
String configLoc = "classpath:rectangleCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyRectangle myRectangle = ctx.getBean("myRectangle", MyRectangle.class);
myRectangle.ar();
}
}
|
cs |
문제 3 - 계절 구하기
<Season.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.spring_ex3_3;
public class Season {
public void rsltSeason(int month) {
switch (month) {
case 3: case 4: case 5:
System.out.println("입력된 정수가"+month+"인 계절은 봄");
break;
case 6: case 7: case 8:
System.out.println("입력된 정수가"+month+"인 계절은 여름");
break;
case 9: case 10: case 11:
System.out.println("입력된 정수가"+month+"인 계절은 가을");
break;
case 12: case 1: case 2:
System.out.println("입력된 정수가"+month+"인 계절은 겨울");
break;
default:
break;
}
}
}
|
cs |
<MySeason.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.spring_ex3_3;
public class MySeason {
private Season season;
private int month;
public void result() {
season.rsltSeason(month);
}
public Season getSeason() {
return season;
}
public void setSeason(Season season) {
this.season = season;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
}
|
cs |
<seasonCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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_ex3_3.Season" id="season"></bean>
<bean class="com.javalec.spring_ex3_3.MySeason" id="mySeason">
<property name="season">
<ref bean="season"/>
</property>
<property name="month" value="9"></property>
</bean>
</beans>
|
cs |
<MainSeason.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.javalec.spring_ex3_3;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainSeason {
public static void main(String[] args) {
String configLoc = "classpath:seasonCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MySeason mySeason = ctx.getBean("mySeason",MySeason.class);
mySeason.result();
}
}
|
cs |
문제 4 - 환전
<Won2Dollar.java>
1
2
3
4
5
6
7
|
package com.javalec.spring_ex3_4;
public class Won2Dollar {
public void exchange(int won) {
System.out.println(won+"원은 $"+won/1200.0+"입니다.");
}
}
|
cs |
<MyWon2Dollar.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.spring_ex3_4;
public class MyWon2Dollar {
private Won2Dollar won2Dollar;
private int won;
public void exch() {
won2Dollar.exchange(won);
}
public Won2Dollar getWon2Dollar() {
return won2Dollar;
}
public void setWon2Dollar(Won2Dollar won2Dollar) {
this.won2Dollar = won2Dollar;
}
public int getWon() {
return won;
}
public void setWon(int won) {
this.won = won;
}
}
|
cs |
<won2DollarCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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_ex3_4.Won2Dollar" id="won2Dollar"></bean>
<bean class="com.javalec.spring_ex3_4.MyWon2Dollar" id="myWon2Dollar">
<property name="won2Dollar">
<ref bean="won2Dollar"/>
</property>
<property name="won" value="3600"></property>
</bean>
</beans>
|
cs |
<MainWon2Dollar.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.javalec.spring_ex3_4;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainWon2Dollar {
public static void main(String[] args) {
String configLoc = "classpath:won2DollarCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyWon2Dollar myWon2Dollar = ctx.getBean("myWon2Dollar",MyWon2Dollar.class);
myWon2Dollar.exch();
}
}
|
cs |
문제 5 - 3의 배수
<Three.java>
1
2
3
4
5
6
7
8
9
10
|
package com.javalec.spring_ex3_5;
public class Three {
public void process(int num) {
if (num%3==0 && num!=0) {
System.out.println("입력된 수: "+num+"는 3의 배수입니다.");
}
}
}
|
cs |
<MyThree.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.spring_ex3_5;
public class MyThree {
private Three three;
private int num;
public void pc() {
three.process(num);
}
public Three getThree() {
return three;
}
public void setThree(Three three) {
this.three = three;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
|
cs |
<threeCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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_ex3_5.Three" id="three"></bean>
<bean class="com.javalec.spring_ex3_5.MyThree" id="myThree">
<property name="three">
<ref bean="three"/>
</property>
<property name="num" value="129"></property>
</bean>
</beans>
|
cs |
<MainThree.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.javalec.spring_ex3_5;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainThree {
public static void main(String[] args) {
String configLoc = "classpath:threeCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyThree myThree = ctx.getBean("myThree",MyThree.class);
myThree.pc();
}
}
|
cs |
문제 6 - 10의자리와 1의 자리 비교
<DivAndRemains.java>
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.javalec.spring_ex3_6;
public class DivAndRemais {
public void process(int num) {
if (num/10==num%10) {
System.out.println("입력된 수: "+num+"는 10의 자리와 1의 자리가 같습니다.");
}else {
System.out.println("입력된 수: "+num+"는 10의 자리와 1의 자리가 다릅니다.");
}
}
}
|
cs |
<MyDivAndRemais.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.spring_ex3_6;
public class MyDivAndRemains {
private DivAndRemais divAndRemais;
private int num;
public void pc() {
divAndRemais.process(num);
}
public DivAndRemais getDivAndRemais() {
return divAndRemais;
}
public void setDivAndRemais(DivAndRemais divAndRemais) {
this.divAndRemais = divAndRemais;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
|
cs |
<divAndRemainsCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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_ex3_6.DivAndRemais" id="divAndRemais"></bean>
<bean class="com.javalec.spring_ex3_6.MyDivAndRemains" id="myDivAndRemains">
<property name="divAndRemais">
<ref bean="divAndRemais"/>
</property>
<property name="num" value="77"></property>
</bean>
</beans>
|
cs |
<MainDivAndRemains.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.javalec.spring_ex3_6;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainDivAndRemais {
public static void main(String[] args) {
String configLoc = "classpath:divAndRemainsCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyDivAndRemains myDivAndRemains = ctx.getBean("myDivAndRemains",MyDivAndRemains.class);
myDivAndRemains.pc();
}
}
|
cs |
문제 7 - 좌표 안에 점 확인하기
<Rectanle.java>
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.javalec.spring_ex03_07;
public class Rectangle {
public void process(int x, int y) {
if (x>=100 && y>=100 && x<=200 && y<=200) {
System.out.println("("+x+","+y+")는 사각형 안에 있습니다.");
} else {
System.out.println("("+x+","+y+")는 사각형 안에 없습니다.");
}
}
}
|
cs |
<MyRectangle.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
|
package com.javalec.spring_ex03_07;
public class MyRectangle {
private Rectangle rectangle;
private int x;
private int y;
public void pc() {
rectangle.process(x, y);
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
|
cs |
<rectangleCTX.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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_ex03_07.Rectangle" id="rectangle"></bean>
<bean class="com.javalec.spring_ex03_07.MyRectangle" id="myRectangle">
<property name="rectangle">
<ref bean="rectangle"/>
</property>
<property name="x" value="150"></property>
<property name="y" value="150"></property>
</bean>
</beans>
|
cs |
<MainRectangle.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.javalec.spring_ex03_07;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainRectangle {
public static void main(String[] args) {
String configLoc = "classpath:rectangleCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyRectangle myRectangle = ctx.getBean("myRectangle",MyRectangle.class);
myRectangle.pc();
}
}
|
cs |
문제 8 - 중간 값 구하기
<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
|
//Median
package com.javalec.spring_ex3_8;
import java.util.Arrays;
public class Median {
public void process(int a, int b, int c) {
int[] arry = new int[3];
arry[0] = a;
arry[1] = b;
arry[2] = c;
Arrays.sort(arry);
System.out.println("입력된 정수가"+arry[0]+"와 "+arry[1]+"와 "+arry[2]+"의 중간값은 "+arry[1]);
}
}
//MyMedian
package com.javalec.spring_ex3_8;
public class MyMedian {
private Median median;
private int a;
private int b;
private int c;
public void pc() {
median.process(a, b, c);
}
public Median getMedian() {
return median;
}
public void setMedian(Median median) {
this.median = median;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
}
//MainMedian
package com.javalec.spring_ex3_8;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainMedian {
public static void main(String[] args) {
String configLoc = "classpath:medianCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyMedian myMedian = ctx.getBean("myMedian",MyMedian.class);
myMedian.pc();
}
}
|
cs |
<xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?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_ex3_8.Median" id="median"></bean>
<bean class="com.javalec.spring_ex3_8.MyMedian" id="myMedian">
<property name="median">
<ref bean="median"/>
</property>
<property name="a" value="20"></property>
<property name="b" value="100"></property>
<property name="c" value="33"></property>
</bean>
</beans>
|
cs |
문제 9 - 짝수의 합
<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
|
//EvenNumber
package com.javalec.spring_ex3_9;
public class EvenNumber {
public void process(int x) {
int sum = 0;
for (int i = 1; i <= x; i++) {
if (i%2==0 && i!=0) {
sum+=i;
}
}
System.out.println(x+"까지의 짝수들의 합계는 "+sum);
}
}
//MyEvenNumber
package com.javalec.spring_ex3_9;
public class MyEvenNumber {
private EvenNumber evenNumber;
private int x;
public void pc() {
evenNumber.process(x);
}
public EvenNumber getEvenNumber() {
return evenNumber;
}
public void setEvenNumber(EvenNumber evenNumber) {
this.evenNumber = evenNumber;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
//MainEvenNumber
package com.javalec.spring_ex3_9;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainEvenNumber {
public static void main(String[] args) {
String configLoc = "classpath:evenNumberCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyEvenNumber myEvenNumber = ctx.getBean("myEvenNumber",MyEvenNumber.class);
myEvenNumber.pc();
}
}
|
cs |
<xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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_ex3_9.EvenNumber" id="evenNumber"></bean>
<bean class="com.javalec.spring_ex3_9.MyEvenNumber" id="myEvenNumber">
<property name="evenNumber">
<ref bean="evenNumber"/>
</property>
<property name="x" value="10"></property>
</bean>
</beans>
|
cs |
문제 10 - 알파벳 찍기
<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
|
//Capital
package com.javalec.spring_ex3_10;
public class Capital {
public void alphabet(char input) {
for (char i = input; i >= 'A'; i--) {
for (char j = 'A'; j <= i; j++) {
System.out.print(j);
}
System.out.println("");
}
}
}
//MyCapital
package com.javalec.spring_ex3_10;
public class MyCapital {
private Capital capital;
private char input;
public void alpha() {
capital.alphabet(input);
}
public Capital getCapital() {
return capital;
}
public void setCapital(Capital capital) {
this.capital = capital;
}
public char getInput() {
return input;
}
public void setInput(char input) {
this.input = input;
}
}
//MainCapital
package com.javalec.spring_ex3_10;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainCapital {
public static void main(String[] args) {
String configLoc = "classpath:capitalCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLoc);
MyCapital myCapital = ctx.getBean("myCapital",MyCapital.class);
myCapital.alpha();
}
}
|
cs |
<xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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_ex3_10.Capital" id="capital"></bean>
<bean class="com.javalec.spring_ex3_10.MyCapital" id="myCapital">
<property name="capital">
<ref bean="capital"/>
</property>
<property name="input" value="F"></property>
</bean>
</beans>
|
cs |