Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 스프링 부트 테스트
- 작업명중복
- java I/O
- Java
- ./gradlew docker
- 동기
- 세션기반 인증
- 로그인/로그아웃
- ORM
- IPC
- @temproal
- JPA
- 스프링
- 로그인 인증 흐름
- MSA
- 스프링부트
- 퍼시스턴스 계층
- 비동기
- 프레젠테이션 계층
- spring
- 토큰기반 인증
- ./gr
- 스프링부트 구조
- formmatted
- 비즈니스 계층
- 스프링부트 계층구조
- http
- RESTfull API
- JWT
- 어노테이션
Archives
- Today
- Total
[DEV] J-Jay
스프링 부트 코드 이해 (2) 본문
728x90
TestController
@RestController
public class TestController {
@GetMapping("/test") // /test GET 요청이 오면 test() 메서드 실행
public String test() {
return "Hello, world";
}
}
@RestController는 라우터 역할을 하는 어노테이션이다. (라우터 : HTTP 요청과 메서드를 연결하는 장치)
해당 어노테이션이 있어야 클러이언트의 요청에 맞는 메서드를 실행할 수 있다.
지금의 경우 TestController를 라우터로 지정해 /test GET 요청이 왔을 때 test() 메서드를 실행한다.
여기서 의문이 하나 생긴다 @RestController와 @Component는 어노테이션 용어가 다른데 어떻게 같은 @Component처럼 취급하는 걸까?
직접 살펴보자.
1단계 : @RestController
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
* @since 4.0.1
*/
@AliasFor(annotation = Controller.class)
String value() default "";
}
코드를 보면 @Controller, @ResponseBody 어노테이션이 함께있다.
말 그대로 @RestController는 @Controller + @ResponseBody 합쳐진 결과물이다. 중요한 건 @Component가 없다.
2단계 : @Controller
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
드디어 @Component 어노테이션을 찾았다.
이를 통해 @Controller 어노테이션이 @ComponentScan을 통해 빈으로 등록되는 이유를 찾았고 추가적으로
@Configuration, @Repository, @Service 어노테이션 모두 @Component 어노테이션을 가지고 있다.
다만 빈이 무슨 역할을 하는지 명확하게 구분하기 위해 다름 이름으로 설정되어 있을 뿐이다.
'Back-end > Spring' 카테고리의 다른 글
스프링 부트 계층 파악하기 (0) | 2023.09.02 |
---|---|
스프링 부트 구조 (1) | 2023.09.02 |
스프링 부트 코드 이해 (1) (0) | 2023.08.31 |
스프링 부트 스타터 (0) | 2023.08.31 |
스프링 Concept (2) (0) | 2023.08.31 |