@Retention
@Retention ์ด๋
ธํ
์ด์
์ ์ด๋
ธํ
์ด์
์ ๋ผ์ดํ ์ฌ์ดํด์ ์ค์ ํ๋ ๊ฒ์ผ๋ก, ์ฆ, ์ด๋
ธํ
์ด์
์ด ์ธ์ ๊น์ง ์ด์ ์์์ง๋ฅผ ์ ํ๋ ๊ฒ์ด๋ค.
package java.lang.annotation;
/**
* Indicates how long annotations with the annotated type are to
* be retained. If no Retention annotation is present on
* an annotation type declaration, the retention policy defaults to
* {@code RetentionPolicy.CLASS}.
*
* <p>A Retention meta-annotation has effect only if the
* meta-annotated type is used directly for annotation. It has no
* effect if the meta-annotated type is used as a member type in
* another annotation type.
*
* @author Joshua Bloch
* @since 1.5
* @jls 9.6.4.2 @Retention
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}RetentionPolicy
@Retention ์ด๋
ธํ
์ด์
์ ์์ฑ์ผ๋ก, SOURCE, CLASS, RUNTIME 3๊ฐ์ง ์ข
๋ฅ๊ฐ ์๋ค.

RetentionPolicy.SOURCE: ์์ค์ฝ๋(.java)๊น์ง ์ ์ง(์ปดํ์ผ ๊ณผ์ ์์ ์ด๋ ธํ ์ด์ ์ ๋ณด ์ฌ๋ผ์ง)RetentionPolicy.CLASS: ํด๋์คํ์ผ(.class)๊น์ง ์ ์ง(๋ฐํ์์ ์ ์ง์๋จ)RetentionPolicy.RUNTIME: ๋ฐํ์์์ ๊น์ง ์ ์ง (Reflection API๋ก ์ด๋ ธํ ์ด์ ์ ๋ณด ์กฐํ ๊ฐ๋ฅ)
๊ฐ ์ข ๋ฅ๋ณ๋ก ์์ ๋ฅผ ๋ณด๋ฉฐ, ์์ธํ ์ดํด๋ณผ ๊ฒ์ด๋ค.
RetentionPolicy.SOURCE
RetentionPolicy.SOURCE ๋ฅผ ๋ฉํ ์ด๋
ธํ
์ด์
์ผ๋ก ์ ์ธํ ์ด๋
ธํ
์ด์
์ค lombok์ @Getter๋ฅผ ์ดํด๋ณผ ๊ฒ์ด๋ค.
Item.java: lombok์ ์ฌ์ฉํ POJO ํด๋์ค@Getter@Getter์ด๋ ธํ ์ด์ ๋ด๋ถ์Retention(RetentionPolicy.SOURCE)๋ฉํ ์ด๋ ธํ ์ด์ ์ด ๋ถ์ด ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์ฌ๊ธฐ์RetentionPolicy.SOURCE์ด๋ฏ๋ก ์์ค์ฝ๋(.java)๊น์ง ์ด๋ ธํ ์ด์ ์ด ๋จ์ ์๊ณ , ์ปดํ์ผ ์ดํ์๋ ์ฌ๋ผ์ง ๊ฒ์ผ๋ก ์์ํ ์ ์๋ค.Item.classํด๋์ค ํ์ผ์ ๋์ปดํ์ผ ํ์ ๋,
@Getter,@Setter์ด๋ ธํ ์ด์ ์ ์ฌ๋ผ์ง ๊ฒ์ ๋ณผ ์ ์์ผ๋ฉฐ, ์์ค์ฝ๋์ ์์ฑํ์ง ์์๋ getter, setter ๋ฉ์๋๊ฐ ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
RetentionPolicy.RUNTIME
๋ฐํ์์ ์ด๋
ธํ
์ด์
์ ๋ณด๋ฅผ ๋ฝ์ ์ธ์ ์๋ค๋ ์๋ฏธ๋ก ์ฆ, Reflection API ๋ฑ์ ์ฌ์ฉํ์ฌ ์ด๋
ธํ
์ด์
์ ๋ณด๋ฅผ ์์ ์๋ค. ์คํ๋ง์์๋ @Controller, @Service, @Autowired, @Component ๋ฑ์ด ์๋ค.
ํด๋น ์ด๋ ธํ ์ด์ ๋ค์ ์คํ๋ง์ด ์ฌ๋ผ์ค๋ ์คํ ์ค์ธ ์์ ์ ์ปดํฌ๋ํธ ์ค์บ์ด ๊ฐ๋ฅํด์ผํ๊ธฐ ๋๋ฌธ์ RUNTIME ์ ์ฑ ์ด ํ์ํ ๊ฒ์ด๋ค. ( ์คํ๋ง๋ ๋ด๋ถ์ ์ผ๋ก Reflection ๋ฑ์ ํ์ฉํ์ฌ ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ง ๊ฐ์ ธ์ด )
RetentionPolicy.CLASS
๊ทธ๋ ๋ค๋ฉด, CLASS ์ ์ฑ
์ ์ ํ์ํ๊ฑธ๊น? class ํ์ผ๋ง ์กด์ฌํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ์ ๊ฒฝ์ฐ์๋ ํ์
์ฒด์ปค, IDE ๋ถ๊ฐ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ CLASS ์ ์ฑ
์ด ํ์ํ๋ค. SOURCE ์ ์ฑ
์ผ๋ก ์ฌ์ฉํ๋ฉด ์ปดํ์ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ jar ํ์ผ์๋ ์ด๋
ธํ
์ด์
์ ๋ณด๊ฐ ๋จ์์์ง ์๊ธฐ ๋๋ฌธ์ด๋ค. ํน์, ํด๋์ค ๋ก๋ฉ์์ ์ ์ถ๊ฐํ์(?)๊ฐ ํ์ํ ๊ฒฝ์ฐ์๋ ์ฌ์ฉํ ์ ์๋ค.
@NonNull
์ฐธ๊ณ
Last updated
Was this helpful?