```
Annotation의 정의
Annotation의 종류
Annotation의 사용 예시
```

이번 글을 통해 배워갈 내용
- Annotation의 정의
- Annotation의 종류
- Annotation의 사용 예시
Annotation의 정의
Annotation은 메타데이터의 한 형태로서 프로그램에 대한 정보를 제공하며 이를 통해서
1. 컴파일러에게 정보를 제공해 에러를 찾거나 경고를 무시(Suppress warning) 할 수 있게 정보를 제공합니다.
2. Compile time, Deployment time Processing이 가능하며
툴과 함께 사용해서 코드, XML 파일 등을 생성하게 정보 제공을 합니다.
3. Runtime Processing이 가능하게 정보 제공을 합니다.
Annotation의 종류
Marker Annotation
Method가 없는 Annotation
Single-Value Annotation
Method가 하나인 Annotation
Multi-Value Annotation
Method가 둘 이상인 Annotation
Annotation의 계층구조
Java.lang.annotation.Annotation의 경우
Standard (Built-in) Annotations와 Custom Annotations을 자식으로 가지며
Standard (Built-in) Annotations은
General Purpose Annotations과 Meta Annotations을 자식으로 가집니다.
Annotation의 사용 예시
General Purpose Annotations의 경우 대표적으로 아래와 같으며
@Override
@Deprecated
@SuppressWarnings
@FunctionalInterface
@SafeVarArgs
아래 예시를 참조해주시면 됩니다.
https://codemasterkimc.tistory.com/398
자바에서 @Override 사용하는 한 가지 방법
``` 자바에서 @Override 사용하는 한 가지 방법 ``` 이번 글을 통해 배워갈 내용 Method Overriding 정의 @Overriding Annotation 예시 Annotation이란 https://codemasterkimc.tistory.com/397 자바 Annota..
codemasterkimc.tistory.com
https://codemasterkimc.tistory.com/399
자바에서 @Deprecated 사용하는 한 가지 방법
``` 자바에서 @Deprecated 사용하는 한 가지 방법 ``` 이번 글을 통해 배워갈 내용 @Deprecated 정의 @Deprecated Annotation 예시 Annotation이란 https://codemasterkimc.tistory.com/397 자바 Annotatio..
codemasterkimc.tistory.com
https://codemasterkimc.tistory.com/400
자바에서 @SupressWarnings 사용하는 한 가지 방법
``` 자바에서 @SupressWarnings 사용하는 한 가지 방법 ``` 이번 글을 통해 배워갈 내용 @SupressWarnings 정의 @SupressWarnings Annotation 예시 Annotation이란 https://codemasterkimc.tistory.com/39..
codemasterkimc.tistory.com
https://codemasterkimc.tistory.com/401
자바에서 @FunctionalInterface 사용하는 한 가지 방법
``` 자바에서 @FunctionalInterface 사용하는 한 가지 방법 ``` 이번 글을 통해 배워갈 내용 @FunctionalInterface 정의 @FunctionalInterface Annotation 예시 Annotation이란 https://codemasterkimc.tistor..
codemasterkimc.tistory.com
https://codemasterkimc.tistory.com/402
자바에서 @SafeVarargs사용하는 한 가지 방법
``` 자바에서 @SafeVarargs 사용하는 한 가지 방법 ``` 이번 글을 통해 배워갈 내용 @SafeVarargs 정의 @SafeVarargs Annotation 예시 Annotation이란 https://codemasterkimc.tistory.com/397 자바 Annota..
codemasterkimc.tistory.com
Meta Annotations의 경우 대표적으로 아래와 같으며
@Inherited
어노테이션을 상속 가능하게 함
@Documented
어노테이션을 JavaDoc에 포함시켜서 문서화함
@Target
어느 맥락에서 어노테이션이 사용 가능할지 알려주며
ElementType.TYPE, ElementType.FIELD, ElementType.LOCAL_VARIABLE 등 상황에 따라 적용할 위치 선택이 가능함
@Repeatable
같은 어노테이션을 중복 정의할 때 사용
@Retention
어느 시점까지 Annotation이 효력을 유지하는지 표시
RetentionPolicy.SOURCE / 런타임에 효력상실
RetentionPolicy.CLASS/ 자바의 기본 retention이며. class 파일에 기록되나 런타임에 효력상실
RetentionPolicy.RUNTIME/ 런타임에도 효력이 있으며 접근 가능
Meta Annotation을 사용해서 Custom Annotations을 만들 수 있습니다.
저는 스프링 부트에서 제공하는 @Service @Repository @RestController,
Java.validation의 @Valid에 있는 @Min @Max @Size,
Lombok의 @Getter, @Setter @Builder 등을 쓸 때 다른 사람들이 만든 어노테이션을 많이 쓰는 편이며
제가 직접 보안 관련해서 토큰 검증이나 권한을 확인할 때 직접 만들어서 쓰기도 합니다.
결론으로
어노테이션은 코드를 간결하게 해 주고
로직 흐름에 대한 맥락을 압축해서
반복되는 코드를 줄이고
비즈니스 로직을 더욱더 매끄럽게 해 줍니다.
다만 어노테이션의 경우 제목이 명확하지 않다면
내부적으로 어떤 동작을 하는지 명확하지 않게 되고
이 때문에 로직 플로우를 이해하기 어렵게 되며
코드의 이해도가 떨어지게 됩니다.
3줄 요약하자면
어노테이션은
편하고 간결하나
잘못 쓰면 코드를 이해하기 힘들게 한다입니다.
참조
https://docs.oracle.com/javase/tutorial/java/annotations/
Lesson: Annotations (The Java™ Tutorials > Learning the Java Language)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
https://www.baeldung.com/java-custom-annotation
Creating a Custom Annotation in Java | Baeldung
Learn how to create and process a custom annotation in Java
www.baeldung.com
https://www.geeksforgeeks.org/annotations-in-java/
Annotations in Java - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
https://www.javatpoint.com/java-annotation
Java Annotations - javatpoint
Java annotations with custom annotation and built-in annotations in java such as @Override, @Deprecated, @SuppresssWarnings, @Target, @Retention etc.
www.javatpoint.com
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)