[문제1]
Package : sample02
Interface : Calc.java
추상Method : public void calculate(int x, int y);
Class : HelloSpring - public static void main(String[] args)
CalcAdd.java - Calc.java 오버라이드
CalcMul.java - Calc.java 오버라이드
[실행결과]
25 + 36 = xx
25 * 36 = xxx
xml안에서는 xml로 해주고 !!
어노테이션 안에서는 어노테이션으로 !!
Chapter01_XML
Interface : Calc.java
package sample02;
public interface Calc {
public void calculate(int x, int y);
}
CalcAdd.java
package sample02;
public class CalcAdd implements Calc {
@Override
public void calculate(int x, int y) {
System.out.println(x + " + " + y + " = " + (x+y));
}
}
CalcMul.java
package sample02;
public class CalcMul implements Calc {
@Override
public void calculate(int x, int y) {
System.out.println(x + " * " + y + " = " + (x*y));
}
}
HelloSpring.java
package sample02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Calc calc = (Calc) context.getBean("calcAdd");
calc.calculate(25, 36);
}
}
applicaionContext.xml
<bean id="calcAdd" class="sample02.CalcMul" scope="prototype"></bean>
<?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">
<!-- sample01 -->
<bean id="messageBean" class="sample01.MessageBeanKo" scope="prototype"></bean>
<!-- sample02 -->
<bean id="calcAdd" class="sample02.CalcAdd" scope="prototype"></bean>
</beans>
<bean id="calcMul" class="sample02.CalcMul" scope="prototype"></bean>
<?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">
<!-- sample01 -->
<bean id="messageBean" class="sample01.MessageBeanKo" scope="prototype"></bean>
<!-- scope="prototype"
scope="request"
scope="session"
scope="singleton"
-->
<!-- sample02 -->
<bean id="calcAdd" class="sample02.CalcAdd" scope="prototype"></bean>
<bean id="calcMul" class="sample02.CalcMul" scope="prototype"></bean>
</beans>
calc = context.getBean("calcMul", Calc.class); // 이렇게 하면 캐스팅 안 시켜도 된다.
calc.calculate(25, 36);
package sample02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Calc calc = (Calc) context.getBean("calcAdd");
calc.calculate(25, 36);
calc = context.getBean("calcMul", Calc.class); // 이렇게 하면 캐스팅 안 시켜도 된다.
calc.calculate(25, 36);
}
}
Chapter01_ANNO
applicaionContext.xml
<?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 base-package="sample02" />
</beans>
HelloSpring.java -- 경로 바꿔주기
package sample02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class HelloSpring {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");
Calc calc = (Calc) context.getBean("calc");
calc.calc(25, 36);
}
}
CalcAdd.java
package sample02;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("calcAdd")
@Scope("prototype")
public class CalcAdd implements Calc {
@Override
public void calculate(int x, int y) {
System.out.println(x + " + " + y + " = " + (x+y));
}
}
클래스명하고 똑같으면 @Component 뒤에 이름을 안 적어도된다 !!
package sample02;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class CalcAdd implements Calc {
@Override
public void calculate(int x, int y) {
System.out.println(x + " + " + y + " = " + (x+y));
}
}
CalcMul.java
package sample02;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class CalcMul implements Calc {
@Override
public void calculate(int x, int y) {
System.out.println(x + " * " + y + " = " + (x*y));
}
}
HelloSpring.java
package sample02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Calc calc = (Calc) context.getBean("calcAdd");
calc.calculate(25, 36);
calc = context.getBean("calcMul", Calc.class); // 이렇게 하면 캐스팅 안 시켜도 된다.
calc.calculate(25, 36);
}
}
[문제2] 성적계산
이름, 국어, 영어, 수학점수를 입력하여 총점과 평균을 구하여 출력하시오
- Scanner는 <bean> 으로 생성하는 것이 아니라 new 하면 된다.
Project : Chapter01_XML
Package : sample03
Interface : SungJuk.java
추상메소드 : public void calc();
public void display();
Class : SungJukImpl.java - SungJuk를 Override하는 클래스
Field : name, kor, eng, math, tot, avg
Method : 기본 생성자 - Scanner 통해서 데이터 입력
public void calc() - 총점, 평균 계산
public void display() - 출력
Class : HelloSpring.java - public static void main(String[] args)
[실행결과]
이름 입력 :
국어 입력 :
영어 입력 :
수학 입력 :
이름 국어 영어 수학 총점 평균
홍길동 95 78 96 269 89.667
Chapter01_XML
SungJuk.java -- 인터페이스 생성
package sample03;
public interface SungJuk {
public void calc();
public void display();
}
SungJukImpl.java -- SungJuk를 Override하는 클래스
public void calc() - 총점, 평균 계산
public void display() - 출력
package sample03;
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 scan = new Scanner(System.in);
System.out.print("이름 입력 : ");
name = scan.next();
System.out.print("국어 입력 : ");
kor = scan.nextInt();
System.out.print("영어 입력 : ");
eng = scan.nextInt();
System.out.print("수학 입력 : ");
math = scan.nextInt();
}
@Override
public void calc() {
tot = kor + eng + math;
avg = tot / 3.0;
}
@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"
+ String.format("%.2f", avg) + "\t");
}
}
HelloSpring.java
package sample03;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class HelloSpring {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("src/applicationContext.xml");
SungJuk sungJuk = (SungJuk)context.getBean("sungJukImpl");
sungJuk.calc();
sungJuk.display();
}
}
applicationContext.xml
<bean id="sungJukImpl" class="sample03.SungJukImpl" scope="prototype"></bean>
<!-- sample03 -->
<bean id="sungJukImpl" class="sample03.SungJukImpl" scope="prototype"></bean>
Chapter01_ANNO
applicationContext.xml -- 이거 먼저 건들기 !
<context:component-scan base-package="sample03" />
<?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 base-package="sample02" />
<context:component-scan base-package="sample03" />
</beans>
SungJukImpl.java -- @Component("sungJuk") / @Scope("prototype") -- 어노테이션 생성
package sample03;
import java.util.Scanner;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@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;
@Override
public void calc() {
Scanner scan = new Scanner(System.in);
System.out.print("이름 입력 : ");
name = scan.next();
System.out.print("국어 입력 : ");
kor = scan.nextInt();
System.out.print("영어 입력 : ");
eng = scan.nextInt();
System.out.print("수학 입력 : ");
math = scan.nextInt();
tot = kor + eng + math;
avg = tot / 3.0;
}
@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"
+ String.format("%.2f", avg) + "\t");
}
}
HelloSpring.java
package sample03;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SungJuk sungJuk = (SungJuk)context.getBean("sungJukImpl");
sungJuk.calc();
sungJuk.display();
}
}
'Spring' 카테고리의 다른 글
DAY 63 - 스프링 프레임워크 - AOP (2024.10.04) (0) | 2024.10.04 |
---|---|
DAY 62 - 스프링 프레임워크 - 파일출력 (2024.10.02) (1) | 2024.10.02 |
DAY 61 - 스프링 프레임워크 HOMEWORK (2024.10.01) (2024.10.02) (0) | 2024.10.02 |
DAY 61 - 스프링 프레임워크 (2024.10.01) (1) | 2024.10.01 |
DAY 60 - 스프링 프레임워크 (2024.09.30) (1) | 2024.09.30 |