@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 ํด๋์คimport lombok.Getter; import lombok.Setter; @Getter @Setter public class Item { private long Id; private String name; }
@Getter
package lombok; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.FIELD, ElementType.TYPE}) @Retention(RetentionPolicy.SOURCE) public @interface Getter { AccessLevel value() default AccessLevel.PUBLIC; Getter.AnyAnnotation[] onMethod() default {}; boolean lazy() default false; /** @deprecated */ @Deprecated @Retention(RetentionPolicy.SOURCE) @Target({}) public @interface AnyAnnotation { } }
@Getter
์ด๋ ธํ ์ด์ ๋ด๋ถ์Retention(RetentionPolicy.SOURCE)
๋ฉํ ์ด๋ ธํ ์ด์ ์ด ๋ถ์ด ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์ฌ๊ธฐ์RetentionPolicy.SOURCE
์ด๋ฏ๋ก ์์ค์ฝ๋(.java
)๊น์ง ์ด๋ ธํ ์ด์ ์ด ๋จ์ ์๊ณ , ์ปดํ์ผ ์ดํ์๋ ์ฌ๋ผ์ง ๊ฒ์ผ๋ก ์์ํ ์ ์๋ค.Item.class
public class Item { private long Id; private String name; public Item() { } public long getId() { return this.Id; } public String getName() { return this.name; } public void setId(long Id) { this.Id = Id; } public void setName(String name) { this.name = name; } }
ํด๋์ค ํ์ผ์ ๋์ปดํ์ผ ํ์ ๋,
@Getter
,@Setter
์ด๋ ธํ ์ด์ ์ ์ฌ๋ผ์ง ๊ฒ์ ๋ณผ ์ ์์ผ๋ฉฐ, ์์ค์ฝ๋์ ์์ฑํ์ง ์์๋ getter, setter ๋ฉ์๋๊ฐ ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
RetentionPolicy.RUNTIME
๋ฐํ์์ ์ด๋
ธํ
์ด์
์ ๋ณด๋ฅผ ๋ฝ์ ์ธ์ ์๋ค๋ ์๋ฏธ๋ก ์ฆ, Reflection API ๋ฑ์ ์ฌ์ฉํ์ฌ ์ด๋
ธํ
์ด์
์ ๋ณด๋ฅผ ์์ ์๋ค. ์คํ๋ง์์๋ @Controller
, @Service
, @Autowired
, @Component
๋ฑ์ด ์๋ค.
package org.springframework.stereotype;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* Indicates that an annotated class is a "Service", originally defined by Domain-Driven
* Design (Evans, 2003) as "an operation offered as an interface that stands alone in the
* model, with no encapsulated state."
*
* <p>May also indicate that a class is a "Business Service Facade" (in the Core J2EE
* patterns sense), or something similar. This annotation is a general-purpose stereotype
* and individual teams may narrow their semantics and use as appropriate.
*
* <p>This annotation serves as a specialization of {@link Component @Component},
* allowing for implementation classes to be autodetected through classpath scanning.
*
* @author Juergen Hoeller
* @since 2.5
* @see Component
* @see Repository
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
ํด๋น ์ด๋ ธํ ์ด์ ๋ค์ ์คํ๋ง์ด ์ฌ๋ผ์ค๋ ์คํ ์ค์ธ ์์ ์ ์ปดํฌ๋ํธ ์ค์บ์ด ๊ฐ๋ฅํด์ผํ๊ธฐ ๋๋ฌธ์ RUNTIME ์ ์ฑ ์ด ํ์ํ ๊ฒ์ด๋ค. ( ์คํ๋ง๋ ๋ด๋ถ์ ์ผ๋ก Reflection ๋ฑ์ ํ์ฉํ์ฌ ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ง ๊ฐ์ ธ์ด )
RetentionPolicy.CLASS
๊ทธ๋ ๋ค๋ฉด, CLASS
์ ์ฑ
์ ์ ํ์ํ๊ฑธ๊น? class
ํ์ผ๋ง ์กด์ฌํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ์ ๊ฒฝ์ฐ์๋ ํ์
์ฒด์ปค, IDE ๋ถ๊ฐ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ CLASS
์ ์ฑ
์ด ํ์ํ๋ค. SOURCE
์ ์ฑ
์ผ๋ก ์ฌ์ฉํ๋ฉด ์ปดํ์ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ jar ํ์ผ์๋ ์ด๋
ธํ
์ด์
์ ๋ณด๊ฐ ๋จ์์์ง ์๊ธฐ ๋๋ฌธ์ด๋ค. ํน์, ํด๋์ค ๋ก๋ฉ์์ ์ ์ถ๊ฐํ์(?)๊ฐ ํ์ํ ๊ฒฝ์ฐ์๋ ์ฌ์ฉํ ์ ์๋ค.
@NonNull
package lombok; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE}) @Retention(RetentionPolicy.CLASS) @Documented public @interface NonNull { }
์ฐธ๊ณ
Last updated
Was this helpful?