<참고 URL>
https://youtu.be/XtXHIDnzS9c?list=PLq8wAnVUcTFUHYMzoV2RoFoY2HDTKru3T
<프로젝트 생성>
프로젝트는 그레이들로 생성한다. 이클립스에 그레이들을 설치하는 방법은 아래 포스트 참고.
https://codevision.tistory.com/9
[JDBC] (이클립스/MVC) 윈도우에 그레이들 설치
https://thecodinglog.github.io/gradle/2019/09/11/install-gradle-in-windows.html Windows에 Gradle 설치하기 Windows에 수작업으로 Gradle 설치하기 thecodinglog.github.io https://chinsun9.github.io/2020/10/05/gradle/ 이클립스에서 gradle 프
codevision.tistory.com
<그레이들로 스프링 불러오기>
1. 프로젝트 라이브러리에서 bundle.gradle 열기
2. 스프링 디펜던시 작성
dependencies {
implementation group : 'org.springframework', name : 'spring-core', version : '5.3.19'
implementation group : 'org.springframework', name : 'spring-context', version : '5.3.19'
implementation group : 'org.springframework', name : 'spring-test', version : '5.3.19'
}
3. 프로젝트 오른쪽 마우스 > Gradle > Refresh Gradle Project
4. 프로젝트 라이브러리 Project and External Dependancies 를 확인해보면 스프링이 추가된 것을 확인할 수 있다.
<간단한 프로그램 작성>
총합, 평균등의 간단한 계산을 하는 프로그램으로 패키지 및 클래스 파일들은 아래와 같이 만들 예정이다.
Program.java
프로그램을 실행시킬 클래스
package spring.di;
import spring.di.entity.Exam;
import spring.di.entity.ExamImpl;
import spring.di.ui.ExamConsole;
import spring.di.ui.GridExamConsole;
import spring.di.ui.InlineExamConsole;
public class Program {
public static void main(String[] args) {
Exam exam = new ExamImpl();
Examconsole console = new InlineExamConsol(exam);
console.setExam(exam);
console.print();
}
}
여기서 Exam은 인터페이스, ExamImpl은 이를 구현하는 클래스이다.
Examconsole 역시 인터페이스, GridExamconsol은 이를 구현하는 클래스이다.
Exam.java
package spring.di.entity;
public interface Exam {
int total();
float avg();
}
ExamImple.java
간단한 변수와 함께 성적 계산을 하는 메소드를 구현해준다.
package spring.di.entity;
public class ExamImpl implements Exam {
private int kor;
private int eng;
private int math;
private int com;
@Override
public int total() {
// TODO Auto-generated method stub
return kor+eng+math+com;
}
@Override
public float avg() {
// TODO Auto-generated method stub
return total() / 4.0f;
}
}
ExamConsole.java
package spring.di.ui;
import spring.di.entity.Exam;
public interface ExamConsole {
void print();
void setExam(Exam exam);
}
여기서 재미있는 부분은 이 ExamConsole 인터페이스를 구현하는 클래스를 두개 만드는 것이다.
ExamConsole 구현 1 : InlineExamConsole
내용은 단순하다. 멤버변수로 Exam 객체를 받아 Exam 객체의 메소드를 출력해주는 클래스이다. 여기서 Inline클래스는 이를 한줄로, Grid클래스는 이를 그리드형태로 출력한다.
package spring.di.ui;
import spring.di.entity.Exam;
public class InlineExamConsole implements ExamConsole {
private Exam exam;
public InlineExamConsole() {
super();
}
public InlineExamConsole(Exam exam) {
this.exam = exam;
}
@Override
public void print() {
System.out.printf("Total is %d, avs is %f\n", exam.total(), exam.avg());
}
@Override
public void setExam(Exam exam) {
// TODO Auto-generated method stub
this.exam = exam;
}
}
ExamConsole 구현 2 : GridExamConsole
package spring.di.ui;
import spring.di.entity.Exam;
public class GridExamConsole implements ExamConsole {
private Exam exam;
public GridExamConsole() {
super();
}
public GridExamConsole(Exam exam) {
this.exam = exam;
}
@Override
public void print() {
System.out.println("┌────────┬────────┐");
System.out.println("│ total │ avg │");
System.out.println("├────────┼────────┤");
System.out.printf("│ %3d │ %3.2f │\n", exam.total(), exam.avg());
System.out.println("└────────┴────────┘");
}
@Override
public void setExam(Exam exam) {
// TODO Auto-generated method stub
this.exam = exam;
}
}
자, 이제 다시 프로그램을 실행시킬 Program 클래스로 돌아오자.
Program.java
package spring.di;
import spring.di.entity.Exam;
import spring.di.entity.ExamImpl;
import spring.di.ui.ExamConsole;
import spring.di.ui.GridExamConsole;
import spring.di.ui.InlineExamConsole;
public class Program {
public static void main(String[] args) {
Exam exam = new ExamImpl();
ExamConsole console = new InlineExamConsole(exam);
console.setExam(exam);
console.print();
}
}
간단한 값과 이를 계산하는 메소드를 가진 객체, 그리고 이를 출력하는 객체를 구성하는 코드였다. 그런데 전 단계에서 ExamConsole 인터페이스를 구현하는 클래스를 두개 만들어놨다. 이 때 출력을 InlineExamConsole객체가 아닌 GridExamConsole객체로 바꾸려면 어떻게 해야할까? 물론 ExamConsole console = new GridExamConsole(exam); 로 바꾸면 될 것이다. 그러나 이 클래스와 클래스를 조립하는 일련의 과정을 내가 아닌 스프링이 대신해줄 수 있다.
~ 2부에서 계속 ~
'JAVA > Spring' 카테고리의 다른 글
이클립스 톰캣 에러 (0) | 2022.12.20 |
---|---|
[Spring] (이클립스/gradle) 게시판 만들기 (1) - 기본 세팅 (0) | 2022.12.19 |
[Spring] 기존 MVC방식부터 스프링까지 간단한 프로젝트 생성 (3) (0) | 2022.12.17 |
[Spring] 기존 MVC방식부터 스프링까지 간단한 프로젝트 생성 (2) (0) | 2022.12.17 |
[Spring] 스프링 STS3 설치하기 / 실행에러 해결방법 (0) | 2022.12.17 |