관리 메뉴

LIFE & IT

스프링 동작 이해 II 본문

웹 프로그래밍/Spring Framework

스프링 동작 이해 II

프린이! 2019. 5. 17. 18:25

스프링 테스트 코드를 이용해 스프링 동작을 이해해보자.

 

  • DItest 프로젝트

 

 

  • 테스트 코드

 

 


  • 테스트 결과 화면
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3835c46: defining beans [chef,restaurant,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
INFO : org.zerock.sample.SampleTests - Restaurant(chef=Chef())
INFO : org.zerock.sample.SampleTests - ----------------------------------
INFO : org.zerock.sample.SampleTests - Chef()
INFO : org.springframework.context.support.GenericApplicationContext - Closing 

 

  • SampleTest 클래스에서 new Restaurant() 와 같은 Restaurant 객체를 생성한 적이 없는데 객체가 만들어졌다.

스프링은 관리가 필요한 객체(Bean)을 어노테이션 등을 이용해 객체를 생성, 관리하는 '컨테이너' 나 '펙토리' 기능을 한다.

 

  • 스프링은 생성자 주입 혹은 setter 주입을 이용해 동작한다.

Lombok은 자동으로 setter/getter 를 만들어주고 'onMethod' 속성을 이용해 setter 에 '@Autowired' 어노테이션으로 자동 관리 되도록 한다.


  • 각 어노테이션 의미

 

 @RunWith
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Junit 의 기본 테스트 러너인 BlockJunit4ClassRunner 대신
@RunWith 이용해 지정한 클래스를 이용해 테스트 메소드를 수행하도록 지정해주는 어노테이션


ex) 스프링 프레임워크에서 제공하는 SpringJUnit4ClassRunner.class

@RunWith(SpringJUnit4ClassRunner.class)

 

 

@ContextConfiguration
import org.springframework.test.context.ContextConfiguration;

지정된 클래스나 문자열을 이용해서 필요한 객체들을 스프링 내에 객체로 등록 (= 스프링 bean으로 등록)
'classpath:' , 'file:' 을 사용하여 지정할 수 있다.

@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml") 




▶ @Log4j 
import lombok.extern.log4j.Log4j;


Lombok 을 이용해서 로그를 기록하는 Logger 변수를 생성한다.
Log4j 라이브러리와 설정이 있다면 Logger 객체 선언 없이도 사용 가능.
Spring Legacy Project 로 생성하는 경우 Log4j 와 해당 설정은 완료된 상태.

파일 위치:



▶ @Setter 
import lombok.Setter;


Lombok 라이브러리를 이용해 setter 를 자동으로 만들어주는 어노테이션

기존 setter :

public void setRestaurant(Restaurant restaurant) { 
this.restaurant = restaurant; 
} 





▶ @Autowired 
import org.springframework.beans.factory.annotation.Autowired;

인스턴스 변수 (= restaurant ) 에 알맞은 타입의 객체 (= Restaurant ) 를 자동으로 주입해달라는 어노테이션

@Setter(onMethod_ = { @Autowired }) 
private Restaurant restaurant; 



▶ @Test 
import org.junit.Test;


Junit 에서 테스트 대상을 표시한 어노테이션


 import static org.junit.Assert.assertNotNull; 

인스턴스 변수 (= restaurant ) 가 null 이 아니어야만 테스트가 성공 한 다는 의미

 

'웹 프로그래밍 > Spring Framework' 카테고리의 다른 글

JDBC 연동하기 1 (수정중 0719)  (0) 2019.07.19
어노테이션 의미  (0) 2019.05.18
스프링 동작 이해Ⅰ  (0) 2019.05.15
Spring XML 의존성 설정  (0) 2019.05.14
Spring D.I (의존성 주입)  (0) 2019.05.13