
어노테이션이란?
자바에서 클래스, 메서드, 필드 등에 메타데이터(데이터를 설명하는 데이터)를 제공하기 위한 문법이다.
코드에 대해 추가적인 정보를 표시하기 위한 도구라고 생각하면 되고, 컴파일러나 프레임워크(Spring 등)가 이 정보를 해석해서 특정 기능을 자동으로 수행한다.
어노테이션 기본 예시
@Override
public String toString() {
return "Hello";
}
> 부모 클래스를 Overriding 했다는 것을 알려주는 어노테이션
어노테이션의 종류와 기능
핵심 클래스 레벨 어노테이션
@SpringBootApplication
스프링 부트 애플리케이션의 진입점. @Configuration, @EnableAutoConfiguration, @ComponentScan을 포함한 복합 애너테이션.
@RestController
@Controller + @ResponseBody. REST API를 만드는 데 사용하며, JSON 또는 문자열을 반환한다.
@Controller
MVC의 컨트롤러 역할. 주로 HTML 템플릿 반환할 때 사용한다.
@Service
비즈니스 로직을 담당하는 클래스에 붙임. 스프링이 이 클래스를 서비스 빈으로 등록한다.
@Repository
데이터 접근 계층(DAO)에 사용한다. 예외 처리를 스프링이 자동으로 처리해준다.
@Component
스프링이 관리해야 하는 일반적인 빈을 의미한다. 위의 @Service, @Repository, @Controller는 이걸 상속한 특수한 형태이다.
@Configuration
설정 클래스를 나타낸다. 빈을 등록할 때 자주 사용한다.
요청 / 응답 관련 어노테이션
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping
HTTP 요청을 해당 메서드에 매핑한다. 예: @GetMapping("/hello")
@RequestMapping
위의 Mapping 애너테이션의 조상. 메서드뿐 아니라 클래스에도 사용 가능하다.
@PathVariable
URL 경로에 포함된 값을 메서드 파라미터로 받을 때 사용한다.
@RequestParam
쿼리 스트링 파라미터를 받을 때 사용한다.
@RequestBody
요청 바디(JSON 등)를 자바 객체로 변환해서 받을 때 사용한다.
@ResponseBody
자바 객체를 JSON 형태로 응답으로 보내준다. @RestController에 이미 포함되어 있다.
주요 라이브러리와 그 역할
spring-context
(org.springframework.stereotype, org.springframework.beans.factory.annotation)
DI(의존성 주입), 컨트롤러 등록, 애너테이션 지원 등 기본 기능이 있다.
spring-web (org.springframework.web.bind.annotation, org.springframework.ui)
웹 요청 매핑, 모델 전달 등 웹 MVC 핵심 기능이 있다.
spring-webmvc (org.springframework.web.servlet)
Spring MVC 구현, 뷰 리졸버, 핸들러 매핑 등이 있다.
spring-boot-starter (org.springframework.boot)
스프링 부트의 기본 실행 환경을 제공하는 핵심 스타터이다.
spring-boot-starter-web (org.springframework.boot)
웹 애플리케이션용 스타터이다. (Spring MVC + 내장 톰캣 등 포함)
spring-boot-autoconfigure (spring-boot-autoconfigure)
설정 자동화를 위한 애너테이션(@SpringBootApplication 등)을 제공한다.
spring-boot-starter-data-jpa (org.springframework.data.jpa.repository)
JPA 기능과 JpaRepository 등 제공, Spring Data JPA를 연동한다.
jakarta.persistence-api (jakarta.persistence)
@Entity, @Id, @GeneratedValue 등 JPA 표준 애너테이션을 제공한다.
REFERENCE
Using the @SpringBootApplication Annotation :: Spring Boot
None of these features are mandatory and you may choose to replace this single annotation by any of the features that it enables. For instance, you may not want to use component scan or configuration properties scan in your application: import org.springfr
docs.spring.io
'java' 카테고리의 다른 글
| [개념] JPQL : 객체지향 쿼리 언어 (1) | 2025.04.29 |
|---|---|
| [개념] JPA란 무엇인가 : JPA와 엔티티의 연관관계 (5) | 2025.04.08 |