본문 바로가기
▶ Back-End/Java

Custom Annotation 생성 Example

by 오늘도 코딩 2023. 7. 26.
728x90
반응형

간단한 Custom Annotation 생성, 확인

*메타 어노테이션 관련 자세한 설명 생략

*Reflection 설명 생략

 

 

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 사용자 정의 어노테이션 생성
 */
@Target(ElementType.TYPE) // @interface 어노테이션의 적용 위치를 설정하는 옵션
@Retention(RetentionPolicy.RUNTIME) // @interface 어노테이션의 적용범위로 어떤 시점까지 사용될 지 결정하는 옵션
@interface CustomAnnotation { // @interface를 통해 Custom Annotation 생성
	String value() default "Hello World";
}

@CustomAnnotation
public class Test {

	public static void main(String[] args) {
		// getAnnotation : 지정한 Annotation의 값을 반환
		System.out.println(Test.class.getAnnotation(CustomAnnotation.class).value());
	}
}

/*
	결과 : 
    Hello World

*/

 

 

▷ 참고

 

[Java] 커스텀 @Interface 사용 방법

1\. @interface 어노테이션의 정의@interface의 기본 포맷과 함께 사용되는 @Retention과@Target 어노테이션 옵션들의 정의는 다음과 같다. 아래 포맷으로 어노테이션 클래스가 된다!@Retention : @interface 어노

velog.io

 

 

▷ 관련 글

 

Lombok이란?

간편한 코드 작성, 가독성, 유지보수를 편리하게 해주는 Lombok *자세한 설명 생략 ▷ Lombok 이란? Java의 라이브러리로 반복되는 메소드를 Annotation을 사용해서 자동으로 작성해 주는 라이브러리 *Ann

coding-today.tistory.com

 

 

728x90
728x90

댓글