[Spring] 주요 annotation 정리
https://www.jrebel.com/blog/spring-annotations-cheat-sheet
Spring Annotations Cheat Sheet | JRebel by Perforce
Our Spring annotations cheat sheet contains the most frequently used annotations and when to use them. Learn how to use @Autowired, @Bean, and more!
www.jrebel.com
1. Spring Boot and Web Annotations
Spring Boot 를 통해 web application 을 구성할 때 사용하는 annotation
적용대상 | Annotations | function |
Class | @SpringBootApplication | @Configuration, @EnableAutoConfiguration, @ComponentScan 기능사용 |
Class | @EnableAutoConfiguration | classpath 를 기반으로 Spring 을 구성하도록 함. |
Class | @Controller | Controller 임을 알려주며, Request 를 처리할 수 있음. spring 에서 View 와 API 를 동시에 사용하는 경우 사용. |
Class | @RestController | @Controller, @ResponseBody 기능을 포함하여 사용 |
Class/Method | @ResponseBody | Class, Method 의 return 값을 Response body 로 매핑하며, JSON 형태로 반환. Spring 에서 View 로 응답하지않고 API 만 사용하는 Controller. |
Method | @RequestMapping | Controller 에 사용하며, HTTP Request 를 URL 로 매핑함. |
Parameter | @RequestParam | HTTP parameter를 Method의 arguments 로 바인딩함. |
Parameter | @PathVariable | URI에 전달된 variable을 method의 parameter 로 바인딩함 |
2. Spring Framework Annotations
Spring Framework 에서 자주사용되는 annotation
@Component Annotation = @Repository @Service @Controller @Component @Configuration 등 존재
적용대상 | Annotations | function |
Class | @Configuration | bean defintions 의 소스로 class 를 표시하고자 할때 사용할 때 사용 |
Class | @ComponentScan | Spring 이 @Configuration class로 구성된 패키지를 스캔하도록 하여. 자동으로 Spring bean으로 등록해주는 역할을 함. @Component Annotation 가 있는 Class 에 대해서 Bean 생성 |
Class | @Import | 추가적인 Configuration을 로드함. XML 에 bean 을 명시할때도 사용함. |
Class | @Component | Class 를 Spring bean 으로 변환/등록함 (auto-scan time 내) 개발자가 직접 작성한 Class를 Bean 으로 등록하기 위한 annotation |
Class | @Service | 비즈니스 로직을 수행하는 Class 라는 것을 명시. |
Field Constructor Method |
@Autowired | Type 에 따라서 자동적으로 Bean 을 주입시킨다(객체에 대한 의존성 주입) 주로 DAO 나 Service 에 관한 객체들을 주입시킬 때 사용된다. |
Method | @Bean | 개발자가 직접 제어 불가능한 외부 라이브러리를 Bean 으로 등록하기 위해 사용 |
Method | @Lookup | ** 추가이해필요 |
Class Method |
@Primary | 동일 타입의 빈이 다수 존재할 때, 특정 빈에 우선순위를 높게 명시함. |
Field Constructor Method |
@Required | 반드시 프로퍼티를 이용하여 값을 주입받도록 정의 스프링 설정 파일에 클래스를 빈으로 등록할 시, 해당 클래스의 setter method 에 사용 |
Field Constructor Method |
@Value | 설정파일에 설정한 내용을 가져와서 주입(적용)시켜줌( property / xml) 1) xml + @Value 방식 2) @PropertySource + @Value 방식 정보보안, 수정 및 관리에 용이하게 해주는 Annotation |
Class | @Lazy | 지연로딩 지원. bean 을 Class 가 로드될때 등록하는 것이아니라 Class 가 실제로 사용될 때 등록하게 하는 방법. |
Class | @Profile | Spring profile 을 설정할 수 있음. Spring profile 을 설정하여 테스트환경과 실제 프로덕션 환경을 어렵지 않게 전환가능 |
Class Method |
@Scope | @Component 나 bean 의 scope 정의를 변경할 수 있음. 명시하지 않으면 sington 객체로 관리됨. [ sington / prototype / request / session / global session / thread / custom ] |
Class Method |
@DependsOn | bean 간의 의존관계를 위하여 bean 을 등록하는 순서를 지정할 때 사용 |
@Lookup @Provider 는 생명주기가 다른 두개의 bean 에 대한 작업을 할 때 사용하는 Injection method 이다.
Singleton Bean이 prototype Bean을 참조할 때 Prototype Bean 이 Prototype 이 아닌 Singleton 으로 동작하는 문제를 해결하기 위해 나왔다.
많이 사용되는 Annotation 을 정리하면서 아직 배울게 많다는 생각이 들었다. 실제로 어떻게 사용하는지 찾아보았고, 언제 사용해야하는지 감을 잡게 되었다. 사용했던 어노테이션을 다시 정리하는 시간이 되었다. 실제로 사용한 어노테이션들은 반절도 안된 것 같다고 생각했다. 앞으로 새로운 것을 배우면 계속 정리해야겠다는 생각이 들었다
사실 이 포스팅을 처음 적은 날이 3주 앞이었다. 하지만 정리하는 시간이 길어지면서 Spring boot 정도만 정리했었다.
앞으로는 빠르게 정리하는 게 낫겠다고 생각했다. 왜냐하면 이 포스팅을 마무리한 시점도 아직 미완성된 포스팅을 완성하고자 결심했기 때문이다.
참고 및 출처
[Spring] Annotation 정리
Annotation(@)은 사전적 의미로는 주석이라는 뜻이다. 자바에서 사용될 때의 Annotation은 코드 사이에 주석처럼 쓰여서 특별한 의미, 기능을 수행하도록 하는 기술이다.
velog.io
[SPRING]스프링 어노테이션 정리
@Configuration @Configuration: 이 어노테이션을 단 클래스는 빈 설정을 담당하는 클래스가 된다. 이 클래스 안에서 @Bean 어노테이션이 동봉된 메소드를 선언하면, 그 메소드를 통해 스프링 빈을 정의하
juntcom.tistory.com
[Spring] Spring Bean의 개념과 Bean Scope 종류 - Heee's Development Blog
Step by step goes a long way.
gmlwjd9405.github.io