
Spring DI
"지금 배우는 과정들은 모두 스프링을 사용해서 객체를 만들기 위해 배우는 것이다."
'목적을 잊지말자'
1.스프링 주요 모듈(jar) 추가
1) 메이븐(빌드 도구)를 사용하면 pom.xml <dependency></dependency>
2) 라이브러리에 jar 파일 추가 ***
수업시간에는 2)번 방식을 사용할 것이다.
스프링 컨테이너 (== IOC 컨테이너) : [스프링 객체 == 스프링 빈] 생성, 관리
???ApplicationContext 공장 : 스프링 객체(빈) 생성 + 연결 조합()
xml파일 == (설명서)
스프링에서는 new 연산자를 사용해서 객체를 사용하거나 하지 않는다.
스프링 사용 객체 생성법
1단계 빌드 페스 가서 스프링 주요 자르 파일 추가한다.(사진 참고)
2단계 springContainer 가 객체를 생성하는 역할을 한다.
총 springContainer 5가지 종류중에
GenericXmlApplicationContext는 xml 파일 이용해서 객체를 생성한다.
GenericXmlApplicationContext 라고 하는 컨테이너를 사용하기 위해 xml파일을 생성해 보자.
강사님이 주신 파일 jar파일 2개를 우선 압축을 풀고 C드라이브에 옮긴다.
=> xml파일 양식을 모르기 때문에
c드라이브의 spring-framework-3.0.2.RELEASE 안의 docs에서 spring-framework-reference.html을 검색해서 클릭해본다.
사이트에서 스프링 버전에 맞게 xml 양식을 가져온다.
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="record" class="di.RecordImpl"></bean> ㄱ-ㄱ //주입 시킬 대상을 선언하는 부분이다.
<bean id="rvi" class="di.RecordViewImpl">
<property name="record" ref="record"></property> //의존성 주입을 하는 부분이다.
</bean>
</beans>
property 태그의 name 속성은 RecordViewImpl 안의 setRecord(setter)메서드에서
set 생략하고 R => 소문자 r로 바꾼것이다. (문법이다.)
property 태그 속성에 있는 ref의 record는 ㄱ-ㄱ의 record를 가져오는거다.
property 태그의 처음 record의 의미와 두번쨰 record의 의미차이를 알아야한다.
(*****굉장히 중요*****)
실행시키는 부분 구현
String resourceLocations = "applicationContext.xml";
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(resourceLocations);
RecordViewImpl rvi = (RecordViewImpl) ctx.getBean("rvi");
rvi.input();
rvi.output();
String resourceLocations = "applicationContext.xml";
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(resourceLocations);
ctx가 생성되었다는 소리는
벌써 스프링컨테이너 안에 스프링 빈 객체가 생성되고 조립 되었다는 의미이다.
스프링공장을 통해 생성된 객체를 가져오기 위해서는 getBean 함수가 필요하다.
getBean 함수는 Object형으로 리턴한다 => 따라서 필요한 클래스로 다운캐스팅 필요!
RecordViewImpl rvi = (RecordViewImpl) ctx.getBean("rvi");
rvi.input();
rvi.output();
System.out.println("=END=");
위 코드를 실행 시키면 아래와 같은 오류가 나온다.
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
=> 필요한 jar파일이 없기 떄문이다.
스프링 주요 모듈을 추가했지만 의존 모듈이 빠져있었기 때문에 에러가 난것이다.
commons-logging.jar 파일을 추가해준다.
C드라이브에 옮겨 놓은 폴더(파일) 2개(강사님이 주신 2개의 파일)중 spring-framework-3.0.2.RELEASE-dependencies에 들어가서
buildPath를 통하여 com.springsource.org.apache.commons.logging-1.1.1 파일을 찾아다가 추가한다.
'Spring' 카테고리의 다른 글
[Spring]AOP 개요__개발공부 87일차 (0) | 2022.07.12 |
---|---|
[Spring]Spring DI(2) 복습__개발공부 87일차 (0) | 2022.07.12 |
[Spring]Spring DI(2)__개발공부 86일차 (0) | 2022.07.12 |
[Spring]Spring DI(1) 복습__개발공부 86일차 (0) | 2022.07.12 |
[Spring]Spring개요__개발공부 84일차 (0) | 2022.07.12 |