Jam's story

[Spring] days1 본문

Spring

[Spring] days1

애플쩀 2022. 7. 11. 17:19

referenced Libraries 폴더생성+ 스프링의 주요 모듈 jar파일 모두 추가

추가완료!!

 

 

스프링 컨테이너==ioc 컨테이너(스프링객체 ==스프링빈]  생성,관리

스프링 객체를 생성하고 연결해주는 [DI 컨테이너== 스프링컨테이너==IOC 컨테이너 ]

 

	//이제는 객체생성을 new 연산자로 하지 않는다. 
	String resourceLocations="applicationContext.xml";
	GenericXmlApplicationContext ctx=new GenericXmlApplicationContext(resourceLocations);

http://www.springframework.org/schema/beans/

 

Index of /schema/beans

 

www.springframework.org

인터페이스  Record

package di;

public interface Record {
int total();
double avg();
}

인터페이스  Record를 구현한 클래스 RecordImpl

package di;

public class RecordImpl implements Record {

	//field
private int kor;
private int eng;
private int mat;

//construct
	public RecordImpl() {
	super();
	// TODO Auto-generated constructor stub
}

	public RecordImpl(int kor, int eng, int mat) {
		super();
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
	}

//getter setter
	
	public int getKor() {
		return kor;
	}

	public void setKor(int kor) {
		this.kor = kor;
	}

	public int getEng() {
		return eng;
	}

	public void setEng(int eng) {
		this.eng = eng;
	}

	public int getMat() {
		return mat;
	}

	public void setMat(int mat) {
		this.mat = mat;
	}

	@Override
	public int total() {
		return this.kor+this.eng+this.mat;
	}
	
	@Override
	public double avg() {
		return (double)total()/3;
	}
	
	

}

 

 

interface RecordView

package di;

public interface RecordView {
void input(); // 성적정보 입력
void output(); //성적정보출력
}

 

 

interface RecordView를 구현한 RecordViewImpl

 

RecordViewImpl 객체는 RecordImpl 객체가 있어야 사용할 수 있다. -> RecordViewImpl 객체는 RecordImpl 객체에 의존한다 

package di;

import java.util.Scanner;

public class RecordViewImpl implements RecordView {


	//RecordViewImpl 객체는 RecordImpl 객체에 의존한다 
	//의존관계를 직접 생성하는 방식의 단점 
	//결합력이 높은 코딩 - 좋은코딩이 아니다 
	//new 연산자로 바로 코딩하지않는다. 
	//private RecordImpl record =new RecordImpl();
	
	
	private RecordImpl record =null;
	//생성자 방식과 프로퍼티 설정 방식 
	public RecordViewImpl() {
		this.record=new RecordImpl();
	}
	
	//생성자 의존성주입방식
	public RecordViewImpl(RecordImpl record) {
		this.record=record;
	}
	
	//프로퍼티 의존성 주입방식
	public void setRecord(RecordImpl record) {
		this.record=record;
	}
	@Override
	public void input() {
		try(Scanner scanner= new Scanner(System.in)){
			System.out.println("kor,eng,mat input?");
			int kor=scanner.nextInt();
			int eng=scanner.nextInt();
			int mat=scanner.nextInt();
		
			this.record.setKor(kor);
			this.record.setEng(eng);
			this.record.setMat(mat);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void output() {
	System.out.printf("kor:%d , eng:%d , mat:%d, tot:%d, avg =%.2f\n"
				,this.record.getKor()
				,this.record.getEng()
				,this.record.getMat()
				,this.record.total()
				,this.record.avg()
			);
	

	}

}

 

 

Ex02.java

package springDI;

import org.springframework.context.support.GenericXmlApplicationContext;

import di.RecordViewImpl;

public class Ex02 {
public static void main(String[] args) {
	//스프링 주요 모듈을 사용하여 recordViewimpl 객체 생성하여 
	//성적 입력, 출력하는 코딩을 하겠다. 
	/*
	 * 스프링 주요 모듈(jar) 추가
	 * 1) 메이븐 빌드도구를 사용하면 pom.xml <dependency></dependency>
	 * 2)라이브러리에 jar파일추가 
	 * 
	 * 
	 * file:///C:/spring-framework-3.0.2.RELEASE/docs/spring-framework-reference/htmlsingle/spring-framework-reference.html
	 */
	
	//이제는 객체생성을 new 연산자로 하지 않는다. 
	String resourceLocations="applicationContext.xml";
	GenericXmlApplicationContext ctx=new GenericXmlApplicationContext(resourceLocations);

	//Object 리턴->RecordViewImpl클래스로 다운캐스팅 
	RecordViewImpl rvi=(RecordViewImpl)ctx.getBean("rvi");
	
	rvi.input();
	rvi.output();
	System.out.println("end");
}
}

 

Ex02.를 java로 적엇다면

package springDI;

import di.RecordImpl;
import di.RecordViewImpl;

public class Ex01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//한학생의 성적정보(국영수)를 입력받고 출력하는 코딩
		
		RecordImpl record=new RecordImpl();
		//생성자를 통해서 di방식
		//RecordImpl record=new RecordImpl(record);
		
		//프로퍼티(setter)를 통해서 di방식
		 RecordViewImpl rvi=new RecordViewImpl();
		 rvi.setRecord(record);
		 rvi.input();
		 rvi.output();
		 System.out.println("end");
		
		 //실행 ctrl+f11
		 
	}

}

 

 

applicationContext.xml

<bean id="record" class="di.RecordImpl"></bean>


<bean id="rvi"  class="di.RecordViewImpl">
<constructor-arg ref="record"></constructor-arg>
</bean> 



<bean id="rvi"  class="di.RecordViewImpl">
<constructor-arg >
<!--자식 하위태그로 설정 -->
<ref bean="record"/>
</constructor-arg>
</bean>


<!-- rvi.setRecord(record) -->
<!-- 스프링에서 프로퍼티 setter 방법 -->
<bean id="rvi"  class="di.RecordViewImpl">
<!-- 프로퍼티 태그의 name 속성은 setRecord (setter)의 set 생략하고 R->r 바꾼다 -->
<property name="record" ref="record"></property>
</bean>

 

 

에러해결

 

저 파일 해당위치 열어서 파일위치를 복사한담에

프로젝트 우클릭후 buildPath 라이브러리에 추가해주기

Comments