오픈강의
어노테이션개발
류지만
ryu.jiman@gmail.com
016-9373-1732
Java에서 어노테이션 사용
java.lang.annotation
package java.lang.annotation;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANN...
java.lang.annotation
package java.lang.annotation;
public enum ElementType {
TYPE,
FIELD,
METHOD,
PARAMETER,
CONSTRUCTOR,
...
java.lang.annotation
package java.lang.annotation;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANN...
spring-framework 에서
어노테이션 사용
Spring-context
package org.springframework.stereotype;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Do...
Spring-context
package org.springframework.stereotype;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Do...
Spring-context
package org.springframework.stereotype;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Do...
Spring-context
package org.springframework.context.annotation;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYP...
Spring-beans
package org.springframework.beans.factory.annotation;
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, El...
Spring-web
package org.springframework.web.bind.annotation;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retentio...
Spring-web
package org.springframework.web.bind.annotation;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME...
Spring-web
package org.springframework.web.bind.annotation;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
...
Spring-web
package org.springframework.web.bind.annotation;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(Ret...
Spring-web
package org.springframework.web.bind.annotation;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNT...
Spring-core
package org.springframework.core.annotation;
public abstract class AnnotationUtils {

AnnotationMethodHandlerA...
Spring-context
프로젝트에서 사용 예
시
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Cloneable {
}
@Cloneable...
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface RuntimeOnlyComp...
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
public @interface TestOnlyContro...
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DoNotLoginCheck {
String[] targetURI();
bo...
@Target( { ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface GuardedByCircuitBreaker...
/**
* Bean Validation을 수행하고자하는 메소드에서 선언해야 하는 annotat
ion
*
* @author
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(Elem...
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Ems {
String afterEmsExecute() default "";...
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Mapper {
String value() defa...
framexpert.run.common.annotation.Key;
/**
* Framework Repository에 의해 관리되고 있는 Resource를 구분하기 위한 annotation
이다.
*
*/
@Docume...
/**
* Framework Repository를 대체하기 위한 ServiceInfo annotation이다.
*
*/
@Documented
@Target({ElementType.METHOD})
@Retention(Re...
// TransactionMode를 설정한다.
TransactionMode transactionMode = ServiceContext.getTransactionMode();
if (transactionMode == nu...
protected final boolean isRollback(TestContext testContext) throws Exception {
boolean rollback = isDefaultRollback(testCo...
// Get Request Body Element Java Class
Class<?> objVoClass;
if (objVo instanceof Collection) {
isArray = true;
objVoClass ...
Annotation Java API에 java.lang.annotation 페키지에 보면 'Annotation Types ’
은
Documented, Inherited, Retention, Target
1. 매개변수가 ...
1. 매개변수가 없는 Annotation 의 선언과 사용
선언 : public @interface MyAnnotation {}
사용 : @MyAnnotation
public void MyMethod() { ...... ...
public void executeAnnotation() {
// 해당 메서드에서 Annotation 을 얻어온다.
Method method = this.getClass().getMethod("execute");
MyA...
Upcoming SlideShare
Loading in …5
×

사내강의 2013 spring_어노테이션

517 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
517
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

사내강의 2013 spring_어노테이션

  1. 1. 오픈강의 어노테이션개발 류지만 ryu.jiman@gmail.com 016-9373-1732
  2. 2. Java에서 어노테이션 사용
  3. 3. java.lang.annotation package java.lang.annotation; @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); } @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Documented { }
  4. 4. java.lang.annotation package java.lang.annotation; public enum ElementType { TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE }
  5. 5. java.lang.annotation package java.lang.annotation; @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); } public enum RetentionPolicy { SOURCE, CLASS, RUNTIME }
  6. 6. spring-framework 에서 어노테이션 사용
  7. 7. Spring-context package org.springframework.stereotype; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Controller { String value() default ""; }
  8. 8. Spring-context package org.springframework.stereotype; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Service { String value() default ""; }
  9. 9. Spring-context package org.springframework.stereotype; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Repository { String value() default ""; }
  10. 10. Spring-context package org.springframework.context.annotation; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented public @interface ComponentScan { String[] value() default {}; String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class; ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT; String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN; boolean useDefaultFilters() default true; Filter[] includeFilters() default {}; Filter[] excludeFilters() default {}; @Retention(RetentionPolicy.RUNTIME) @Target({}) @interface Filter { FilterType type() default FilterType.ANNOTATION; Class<?>[] value() default {}; String[] pattern() default {}; } }
  11. 11. Spring-beans package org.springframework.beans.factory.annotation; @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Autowired { boolean required() default true; } @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Value { String value(); } @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Qualifier { String value() default ""; }
  12. 12. Spring-web package org.springframework.web.bind.annotation; @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ModelAttribute { String value() default ""; }
  13. 13. Spring-web package org.springframework.web.bind.annotation; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface SessionAttributes { String[] value() default {}; Class[] types() default {}; }
  14. 14. Spring-web package org.springframework.web.bind.annotation; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller @ResponseBody public @interface RestController { }
  15. 15. Spring-web package org.springframework.web.bind.annotation; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ResponseBody { }
  16. 16. Spring-web package org.springframework.web.bind.annotation; @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface PathVariable { String value() default ""; }
  17. 17. Spring-core package org.springframework.core.annotation; public abstract class AnnotationUtils { AnnotationMethodHandlerAdapter AnnotationUtils.findAnnotation(
  18. 18. Spring-context
  19. 19. 프로젝트에서 사용 예 시
  20. 20. @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Cloneable { } @Cloneable public void setTestName(String testName) { this.testName = testName; }
  21. 21. @Target({ ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface RuntimeOnlyComponent { /** * Spring component id. */ String value() default ""; }
  22. 22. @Target({ ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller public @interface TestOnlyController { /** * Spring component id. */ String value() default ""; }
  23. 23. @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface DoNotLoginCheck { String[] targetURI(); boolean doNotCheckAllMethods() default false; }
  24. 24. @Target( { ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface GuardedByCircuitBreaker { String value() default ""; }
  25. 25. /** * Bean Validation을 수행하고자하는 메소드에서 선언해야 하는 annotat ion * * @author * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface RequireValid { Class<?>[] groups() default Default.class; boolean notAllowNullParameter() default true; }
  26. 26. @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Ems { String afterEmsExecute() default ""; } @Ems public class MDHMEmsService impleme nts EmsAfterService { } if(ex.getClass().isAnnotationPresent(ResponseStatus.class)){ response.setStatus(ex.getClass().getAnnotation(ResponseStatus.class).value().value()); }else{ response.setStatus(exceptionStatusCode.value()); }
  27. 27. @Documented @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Mapper { String value() default ""; }
  28. 28. framexpert.run.common.annotation.Key; /** * Framework Repository에 의해 관리되고 있는 Resource를 구분하기 위한 annotation 이다. * */ @Documented @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Key { /** Resource Key */ long value(); } @Key(23510L) @Component("lottemembers.kr.ig.mt.cd.bc.CponAprBc") public class CponAprBcImpl implements CponAprBc { }
  29. 29. /** * Framework Repository를 대체하기 위한 ServiceInfo annotation이다. * */ @Documented @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Service { /** Service ID */ String value(); }
  30. 30. // TransactionMode를 설정한다. TransactionMode transactionMode = ServiceContext.getTransactionMode(); if (transactionMode == null) { ServiceContext.setTransactionMode(TransactionMode.Online); } // ServiceID를 얻는다. Method targetMethod = ((MethodSignature) pjp .getSignature()).getMethod(); Service service = targetMethod.getAnnotation(Service.class); String serviceId = (service == null ? "<Unknown>" : service.value());
  31. 31. protected final boolean isRollback(TestContext testContext) throws Exception { boolean rollback = isDefaultRollback(testContext); Rollback rollbackAnnotation = testContext.getTestMethod().getAnnotation(Rollback.class); if (rollbackAnnotation != null) { rollback = rollbackAnnotation.value(); if (logger.isDebugEnabled()) { logger.debug("Method-level @Rollback(" + rollbackOverride + ") overrides default rollback [" + rollback + "] for test context [" + testContext + "]"); } } else { rollback = true; } return rollback; }
  32. 32. // Get Request Body Element Java Class Class<?> objVoClass; if (objVo instanceof Collection) { isArray = true; objVoClass = CollectionUtils.findCommonElementType((Collection<?>) objVo); } else { objVoClass = objVo.getClass(); } ioKey = objVoClass.getAnnotation(Key.class).value(); if (log.isDebugEnabled()) { log.debug("ioKey - " + ioKey); } Type type = ((FrameworkRepositoryTypeLoader)typeLoader).getCompositeType(ioKey);
  33. 33. Annotation Java API에 java.lang.annotation 페키지에 보면 'Annotation Types ’ 은 Documented, Inherited, Retention, Target 1. 매개변수가 없는 Annotation 2. 매개변수가 하나 있는 Annotation 3. 매개변수가 여러개가 있는 Annotation
  34. 34. 1. 매개변수가 없는 Annotation 의 선언과 사용 선언 : public @interface MyAnnotation {} 사용 : @MyAnnotation public void MyMethod() { ...... } 2. 매개변수가 하나 있는 Annotation 의 선언과 사용 선언 : public @interface MyAnnotation { String value(); } 사용 : @MyAnnotation("아하하하") public void MyMethod() { ...... } 3. 매개변수가 여러개가 있는 Annotation 의 선언과 사용 선언 : public @interface MyAnnotation { String name() default ""; int age(); } 사용 : @MyAnnotation(name = "이대리", age = 27) publoic void MyMethod() { ...... }
  35. 35. public void executeAnnotation() { // 해당 메서드에서 Annotation 을 얻어온다. Method method = this.getClass().getMethod("execute"); MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); // 하고자 하는 로직 수행 System.out.println(annotation.name() + " : " + annotation.age()); } 위의 코드 처럼 수행하고자 하는 메서드에서 Annotation 을 얻어와서 Annotation 에 전달된 값을 이용하여 하고자 하는 로직을 수행하게끔 내용을 채워주면 된다. 1. Annotation Class 의 작성 2. 해당 Annotation 으로 하고자하는 로직을 구현 3. Annotation 을 사용하고자 하는 메서드에 Annotation 주석 작성 Annotation 주석을 단다고 해서 뭔가 실행이 되거나 하진 않는다.

×