우당탕탕 개발일지
60일차_과제 본문
반응형
과제
- 계산기: homework01
- 성적 계산: homework02
방법1. Bean 등록
1. 계산기: homework01
applicationContext.xml
Bean으로 사용할 클래스 등록
- calMul
- calAdd
- sg
<?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="messageBean" class="sample01.MessageBeanKo" scope="prototype"></bean>
<bean id="calcAdd" class="homework01.CalcAdd" scope="prototype"></bean>
<bean id="calcMul" class="homework01.CalcMul" scope="prototype"></bean>
<bean id="sg" class="homework02.SungJukImpl" scope="prototype"></bean>
</beans>
과제1. 계산기
Cal.java
인터페이스 Calc 생성
Spring Framwork는 대형 프로젝트이기 때문에 인터페이스를 통해 구현 클래스 호출
CalAdd.java
인터페이스 Calc 사용
덧셈 오버라이드
CalMul.java
인터페이스 Calc 사용
곱셈 오버라이드
과제2. 성적 계산
SungJuk.java
인터페이스 SungJuk 생성
SungJukImpl.java
인터페이스 SungJuk 사용
package homework02;
import java.util.Scanner;
public class SungJukImpl implements SungJuk {
private String name;
private int kor;
private int eng;
private int math;
private int tot;
private double avg;
public SungJukImpl() {
Scanner sc = new Scanner(System.in);
System.out.print("이름: ");
name = sc.nextLine();
System.out.print("국어: ");
kor = sc.nextInt();
System.out.print("영어: ");
eng = sc.nextInt();
System.out.print("수학: ");
math = sc.nextInt();
}
@Override
public void calc() {
tot = kor + eng + math;
avg = (double) tot / 3;
}
@Override
public void display() {
System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t");
System.out.println(name + "\t"
+ kor + "\t"
+ eng + "\t"
+ math + "\t"
+ tot + "\t"
+ avg + "\t");
}
}
HellowSpring.java
xml 파일에서 Bean으로 등록한 클래스 객체 사용
package sample01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import homework01.Calc;
import homework02.SungJuk;
public class HellowSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Calc c = (Calc) context.getBean("calcAdd");
c.calculate(25, 36);
c = (Calc) context.getBean("calcMul");
c.calculate(25, 36);
SungJuk s = (SungJuk) context.getBean("sg");
s.calc();
s.display();
}
}
방법2. 패키지 등록
applicationContext.xml
Bean으로 사용할 클래스의 상위 패키지 등록
- homework01
- homework02
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="sample01"></context:component-scan>
<context:component-scan base-package="homework01"></context:component-scan>
<context:component-scan base-package="homework02"></context:component-scan>
</beans>
Cal.java
인터페이스 Calc 생성
Spring Framwork는 대형 프로젝트이기 때문에 인터페이스를 통해 구현 클래스 호출
CalAdd.java
인터페이스 Calc 사용
덧셈 오버라이드
@Component
- Bean 등록
- 이름: calcAdd
- 빈 이름 설정하지 않아도 spring framework에서 클래스명과 동일하며 첫번째 문자를 소문자로 설정
Scope("prototype")
- 싱글톤 사용 X
CalMul.java
인터페이스 Calc 사용
곱셈 오버라이드
@Component("calcMul")
- Bean 등록
- 빈 이름 설정
Scope("prototype")
- 싱글톤 사용 X
과제2. 성적 계산
SungJuk.java
인터페이스 SungJuk 생성
SungJukImpl.java
인터페이스 SungJuk 사용
@Component("sg")
- Bean 등록
- 빈 이름 설정
Scope("prototype")
- 싱글톤 사용 X
package homework02;
import java.util.Scanner;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("sg")
@Scope("prototype")
public class SungJukImpl implements SungJuk {
private String name;
private int kor;
private int eng;
private int math;
private int tot;
private double avg;
public SungJukImpl() {
Scanner sc = new Scanner(System.in);
System.out.print("이름: ");
name = sc.nextLine();
System.out.print("국어: ");
kor = sc.nextInt();
System.out.print("영어: ");
eng = sc.nextInt();
System.out.print("수학: ");
math = sc.nextInt();
}
@Override
public void calc() {
tot = kor + eng + math;
avg = (double) tot / 3;
}
@Override
public void display() {
System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t");
System.out.println(name + "\t"
+ kor + "\t"
+ eng + "\t"
+ math + "\t"
+ tot + "\t"
+ avg + "\t");
}
}
HellowSpring.java
xml 파일에서 Bean으로 등록한 클래스 객체 사용
package sample01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import homework01.Calc;
import homework02.SungJuk;
public class HellowSpring {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");
Calc c1 = (Calc) context.getBean("calcAdd");
c1.calculate(25, 36);
c1 = (Calc) context.getBean("calcMul");
c1.calculate(25, 36);
SungJuk s = (SungJuk) context.getBean("sg");
s.calc();
s.display();
}
}
결과 화면
실행 결과는 방법1과 방법2가 동일함.
반응형