-------------------------------
Spring Framework
1. Framework - 어플리케이션을 구현하고 관리하는 환경
2. Spring Framework - 설정정보(XML)에 의해서 어플리케이션을 구성하고, 객체를 생성, 관리하는 주체.
관리대상(객체) -> 설정정보(XML) 등록 -> 객체 생성 -> 컨테이너(IoC)에 등록
-> 사용자 요청 -> 객체 제공 -> 액션 진행
3. IoC (Inversion of Control, 제어의 역전)
Spring Framework -> 객체 생성 -> 관리
사용자 -> 사용하고자 하는 객체에 대한 요청 -> Spring Framework 가 제공
중간부품(객체) 조립->최종부품(객체) 조립 -> 완성품(객체)
A -> B -> C
완성품(객체) <- 틀(프레임워크) <- 부품(객체)을 적절한 자리에 배치(설정정보)
C <- B <- A
4. DI (Dependency Injection, 의존주입, 의존성 삽입)
최종부품을 얻기 위해서 중간부품을 최종부품에 조립을 해야 한다면
최종부품은 중간부품이 포함된 상태로만 사용할 수 있다.
최종부품은 중간부품에 대해서 의존성이 존재한다.
단점은 중간부품에 대한 교체 또는 교환이 쉽게 진행되지 않는다.
아이폰과 삼성폰의 차이
- 배터리(중간부품) 차이
- 아이폰(최종결과물)은 일체형. 배터리 교환 및 교체가 어렵다. 의존성이 높다.
- 삼성폰(최종결과물)은 배터리(중간부품)가 교환형. 배터리 교환 및 교체가 쉽다. 의존성이 낮다.
- 객체와 객체의 결합은 약할 수록 좋다. 약한 결합을 만들려면 Interface 가 필요하다.
class A {
private B b;
public A() {
b = new B();
}
}
A 객체 사용을 위해서는 B 객체(의존 객체)가 필요하고,
B 객체를 C, D로 교체 또는 교환이 쉽지 않다.
틀을 만들고, 중간부품과 최종부품이 서로 연결될 수 있도록 한다.
장점은, 중간부품을 교체 및 교환하기가 쉽다.
class A {
private B의인터페이스 b;
public void setA(B의인터페이스 b) {
this.b = b;
}
}
A 객체 사용을 위해서는 B 객체(의존 객체)가 필요하고,
B 객체를 외부에서 제공하도록 수정되었기 때문에
B 객체를 C, D로 교체 또는 교환이 쉽게 된다.
A, B 객체를 외부에서 관리할 수 있고, A 와 B의 의존 상태를 조정할 수 있다.
그리고, B 객체 대신에 C, D 로 대체하는 것이 쉬어진다.
의존 객체를 외부에서 제공하는 과정을 DI(의존 주입)라고 한다.
-------------------------------------
스프링 프레임워크 다운로드
http://docs.spring.io/downloads/nightly/release-download.php?project=SPR
-SPR/spring-framework-3.0.2.RELEASE-with-docs.zip
-SPR/spring-framework-3.0.2.RELEASE-dependencies.zip
--------------------------------------
DI 테스트 프로그램 작성
- 콘솔 프로젝트
- 성적 처리
1. 스프링 환경 설정
- Java Project 생성
- 프로젝트 선택>Build Path>Configure Build Path...>Libraries탭 선택>Add External JARS... 선택
- 아래 .jar 파일 선택
경로명1 : C:\spring-docs\spring-framework-3.0.2.RELEASE\dist
파일명 :
org.springframework.asm-3.0.2.RELEASE.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.expression-3.0.2.RELEASE.jar
경로명2 : C:\spring-dependencies\org.apache.commons\com.springsource.org.apache.commons.logging\1.1.1
파일명 :
com.springsource.org.apache.commons.logging-1.1.1.jar
2. 프로그램 구성
//Record.java -> 인터페이스
//RecordView.java -> 인터페이스
//RecordImpl1.java -> 클래스
//RecordImpl2.java -> 클래스
//RecordViewImpl.java -> 클래스
//Main.java -> 클래스. main() 메소드.
//applicationContext.xml -> 스프링 환경 설정 (src 폴더 하위에 생성)
3. 프로그램 소스 코드
//Record.java -> 인터페이스
package com.test;
public interface Record {
public void setKor(int kor);
public int getKor();
public void setEng(int eng);
public int getEng();
public void setMat(int mat);
public int getMat();
public int getTotal();
public double getAve();
}
//RecordView.java -> 인터페이스
package com.test;
public interface RecordView {
//setter 메소드 정의
public void setRecord(Record record);
//입력 액션 전용 메소드
public void input();
//출력 액션 전용 메소드
public void output();
}
//RecordImpl1.java -> 클래스
package com.test;
public class RecordImpl1 implements Record {
private int kor, eng, mat;
@Override
public int getKor() {
return kor;
}
@Override
public void setKor(int kor) {
this.kor = kor;
}
@Override
public int getEng() {
return eng;
}
@Override
public void setEng(int eng) {
this.eng = eng;
}
@Override
public int getMat() {
return mat;
}
@Override
public void setMat(int mat) {
this.mat = mat;
}
@Override
public int getTotal() {
int result = 0;
result = this.getKor() + this.getEng() + this.getMat();
return result;
}
@Override
public double getAve() {
double result = 0;
result = this.getTotal() / 3.0;
return result;
}
}
//RecordImpl2.java -> 클래스
package com.test;
public class RecordImpl2 implements Record {
private int kor, eng, mat;
@Override
public int getKor() {
return kor;
}
@Override
public void setKor(int kor) {
this.kor = kor;
}
@Override
public int getEng() {
return eng;
}
@Override
public void setEng(int eng) {
this.eng = eng;
}
@Override
public int getMat() {
return mat;
}
@Override
public void setMat(int mat) {
this.mat = mat;
}
@Override
public int getTotal() {
int result = 0;
//기본 점수 추가
result = 100 + this.getKor() + this.getEng() + this.getMat();
return result;
}
@Override
public double getAve() {
double result = 0;
result = this.getTotal() / 4.0;
return result;
}
}
//RecordViewImpl.java -> 클래스
package com.test;
import java.util.Scanner;
public class RecordViewImpl implements RecordView {
private Record record;
@Override
public void setRecord(Record record) {
this.record = record;
}
@Override
public void input() {
Scanner sc = new Scanner(System.in);
System.out.print("국어 영어 수학?");
String kor = sc.next();
String eng = sc.next();
String mat = sc.next();
record.setKor(Integer.parseInt(kor));
record.setEng(Integer.parseInt(eng));
record.setMat(Integer.parseInt(mat));
sc.close();
}
@Override
public void output() {
System.out.println("국어 영어 수학 총점 평균");
System.out.printf("%3s %3s %3s %3d %3.1f %n"
, record.getKor()
, record.getEng()
, record.getMat()
, record.getTotal()
, record.getAve());
}
}
//Main.java -> 클래스. main() 메소드.
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//기존 방법 사용하는 경우
/*
RecordView view = new RecordViewImpl();
//둘 중에 하나만 사용
//RecordImpl1 클래스의 객체 사용 상태와
//RecordImpl2 클래스의 객체 사용 상태를
//쉽게 바꿀 수 있다.
view.setRecord(new RecordImpl1());
//view.setRecord(new RecordImpl2());
view.input(); //입력 폼 구성
view.output(); //출력 폼 구성
*/
//스프링 프레임워크 사용하는 경우(.xml 파일 필요)
//RecordImpl1 클래스의 객체 사용 상태와
//RecordImpl2 클래스의 객체 사용 상태를
//쉽게 바꿀 수 있다.
//->외부 환경 설정 파일에서 설정 가능
ApplicationContext context
= new ClassPathXmlApplicationContext("applicationContext.xml");
RecordView view = context.getBean("recordViewImpl"
,RecordView.class);
view.input();
view.output();
}
}
//applicationContext.xml -> 스프링 환경 설정 (src 폴더 하위에 생성)
<?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-3.0.xsd">
<!-- 관리대상(객체) - 설정정보(XML) 등록 -->
<!-- <bean></bean> 엘리먼트를 사용해서 객체 등록 -->
<!-- id="" 식별자 지정, class="" 클래스이름 지정 -->
<bean id="recordImpl1" class="com.test.RecordImpl1"></bean>
<bean id="recordImpl2" class="com.test.RecordImpl2"></bean>
<!-- 의존 객체 지정하는 부분 -->
<bean id="recordViewImpl" class="com.test.RecordViewImpl">
<!-- <property></property> 엘리먼트는 의존객체 주입(DI)을
setter를 통해서 지정하는 경우 -->
<!--
RecordImpl1 클래스의 객체 사용 상태와
RecordImpl2 클래스의 객체 사용 상태를
쉽게 바꿀 수 있다.
-->
<property name="record">
<ref bean="recordImpl1" />
</property>
</bean>
</beans>
-------------------------------------------------------
'Spring' 카테고리의 다른 글
Spring 기본 설정방법! (0) | 2015.07.01 |
---|---|
Spring 모듈 다운로드~ (0) | 2015.07.01 |
04일차_Spring MVC, 데이터송수신, 회원관리.txt (0) | 2015.07.01 |
03일차_DI테스트 (0) | 2015.06.30 |
02일차_DI테스트 (1) | 2015.06.30 |