Annotation 이란 ?
- 클래스와 메소드에 추가하여 다양한 기능을 부여하는 역할
- 코드량 감소, 쉬운 유지보수, 생산성 증가
대표적인 Annotation
@Component
개발자가 생성한 Class를 Spring의 Bean 으로 등록할 때 사용
@Component(value="myman")
public class Man {
public Man() {
System.out.println("hi");
}
}
@ComponentScan
- 말그대로 component들을 스캔해주는 어노테이션
- SpringFramework는 @Component, @Service, @Repository, @Controller, @Configuration 중 1개라도 등록된 클래스를 찾으면 Context에 bean으로 등록
@Bean
개발자가 제어할 수 없는 외부라이브러리와 같은 것들을 Bean으로 만들 때 사용
@Controller
해당 클래스가 Controller 임을 명시하는 어노테이션
@RequestHeader
Request의 Header값을 가져올 수 있으며, 해당 메소드의 파라미터에 사용
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String getUser(@RequestHeader(value="Accept-Language") String acceptLanguage) {
// GET method, /user 요청을 처리
}
}
@RequestMapping
- @RequestMapping(value="")와 같은 형태로 작성,
- 요청 들어온 URI의 요청과 Annotation의 value값이 일치하면 해당 클래스/메소드 실행
- Controller 객체 안의 메소드와 클래스에 적용 가능
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String getUser(Model model) {
// GET method, /user 요청을 처리
}
@RequestMapping(method = RequestMethod.POST)
public String addUser(Model model) {
// POST method, /user 요청을 처리
}
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String addUser(Model model) {
// GET method, /user/info 요청을 처리
}
}
- Class 단위에 사용하면 하위 메소드에 적용
- 메소드에 적용되면 해당 메소드에서 지정한 방식으로 URI처리
@RequestParam
- URI에 전달되는 파라미터 - 메소드의 인자 매칭시켜, 파라미터를 받아서 처리하는 어노테이션
- JSON 형식의 Body를 MessageConverter를 통해 Java객체로 변환
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String getUser(@RequestParam String nickname, @RequestParam(name="old") String age {
// GET method, /user 요청을 처리
// https://naver.com?nickname=dog&old=10
String sub = nickname + "_" + age;
...
}
}
@RequestBody
- Body에 전달되는 데이터를 메소드의 인자와 매칭시켜, 데이터를 받아서 처리할 수 있는 어노테이션
- 클라이언트가 body에 JSON or XML과 같은 형태로 값(객체)을 전송하면, 해당 내용을 Java Object로 변환
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(method = RequestMethod.POST)
public String addUser(@RequestBody User user) {
// POST method, /user 요청을 처리
String sub_name = user.name;
String sub_old = user.old;
}
}
@ModelAttribute
- 클라이언트가 전송하는 HTTP Parameter, Body 내용을 Setter 함수를 통해 1:1로 객체에 데이터를 바인딩
- ResponstBody와 다르게 HTTP Body내용은 multipart/form-data 형태로 요구
- ResponstBody와 다르게 JSON을 받아 처리할 수 없음
@Autowired
빈을 주입받는 방식
@GetMapping
RequestMapping(Method=RequestMethod.GET)과 같은 역할
@PostMapping
RequestMapping(Method=RequestMethod.POST)과 같은 역할
Lombok의 대표적인 Annotation
*Lombok : 코드를 크게 줄여주어 가독성을 크게 높힐 수 있는 라이브러리
@Setter
Class 모든 필드의 Setter Method를 생성해줌
@Getter
Class 모든 필드의 Getter Method를 생성해줌
@AllArgsConstructor
Class 모든 필드값을 파라미터로 받는 생성자를 추가
@NoArgsConstructor
Class의 기본 생성자를 자동으로 추가
@ToString
Class 모든 필드의 toString Method 생성
References
http://melonicedlatte.com/2021/07/18/182600.html
스프링(Spring)에서 자주 사용하는 Annotation 개념 및 예제 정리 - Easy is Perfect
melonicedlatte.com
'Java' 카테고리의 다른 글
MyBatis vs JDBC 의 개념/특징/차이점 (0) | 2022.07.28 |
---|---|
[JAVA] 인터페이스 구현 (0) | 2022.07.27 |
[Spring] VO, DTO, Entity (0) | 2022.04.20 |
[Spring] Spring Bean 이란? (0) | 2022.04.18 |
[Spring] 의존성 주입(DI) 과 제어의 역전 (IoC) (0) | 2022.04.16 |
댓글