관리 메뉴

LIFE & IT

어노테이션 의미 본문

웹 프로그래밍/Spring Framework

어노테이션 의미

프린이! 2019. 5. 18. 19:46

어노테이션과 import 의미

 

 @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 이 아니어야만 테스트가 성공 한 다는 의미

 

aessertNotNull 메소드 사용

    @TEST
	public void testExist() {
		
		assertNotNull(hotel);
		
		log.info(hotel);
		log.info("------------------------");
		log.info(hotel.getChef());
		
	}

 

 

 @Component 

import org.springframework.stereotype.Component;

 

어노테이션을 부여한 각 해당 클래스가 스프링에서 객체(Bean)로 만들어서 관리하는 대상임을 명시.

명시된 대상을 scan 을 이용해 해당 클래스들이 잇는 곳을 지정.

 

XML 에서는 root-context.xml 에 <context:component-scan> 으로 패키지 명시하여 관리했지만

JAVA 설정에서는 @ComponentScan 어노테이션을 이용해 해야한다.

 

 

 @AllArgsConstructor 

import lombok.AllArgsConstructor;

 

인스턴스 변수로 선언된 모든 것을 파라미터로 받는 생성자를 작성.

 

ex) 인스턴스 변수 chef, rest 둘을 가진 SampleTest2 클래스에 @AllArgsConstructor 어노테이션을 사용한 코드

package org.zerock.sample;

import org.springframework.stereotype.Component;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Component
@Getter
@ToString
@AllArgsConstructor
public class SampleTest2 {

		private Chef chef;
		private Restaurant rest;
		
}

↓ 아래 그림 처럼 STS 의 우측 상단에 outline 을 보면 쉽게 알 수 있다.

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

Spring STS 속도 향상 (Eclipse 포함)  (0) 2019.07.29
JDBC 연동하기 1 (수정중 0719)  (0) 2019.07.19
스프링 동작 이해 II  (0) 2019.05.17
스프링 동작 이해Ⅰ  (0) 2019.05.15
Spring XML 의존성 설정  (0) 2019.05.14