Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[스프링AOP 포인트컷] 예제코드 본문

인프런/[인프런] 스프링 핵심 원리 - 고급

[스프링AOP 포인트컷] 예제코드

Seung__ 2022. 1. 5. 23:44

1. Annotation


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassAop {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) //Runtime때까지 annotation유지
public @interface MethodAop {
    String value();
}
  • @Target: Indicates the contexts in which an annotation type is applicable. 
  • @Retention: Indicates how long annotations with the annotated type are to be retained.

 

 

2. MemberService


public interface MemberService {
    String hello(String param);
}
@ClassAop
@Component
public class MemberServiceImpl implements MemberService{

    @Override
    @MethodAop("test value")
    public String hello(String param) {
        return "ok";
    }

    public String internal(String param){
        return "ok";
    }
}

 

3. Test


@Slf4j
public class ExecutionTest {

    AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    Method helloMethod;

    @BeforeEach
    public void init() throws NoSuchMethodException {
        helloMethod = MemberServiceImpl.class.getMethod("hello", String.class);
    }

    @Test
    void printMethod(){
        log.info("helloMethod={}", helloMethod);
    }
}
  • AspectJExpressionPointcut 이 바로 포인트컷 표현식을 처리해주는 클래스다. 
  • 여기에 포인트컷 표현식을 지정하면 된다. 
  • AspectJExpressionPointcut 는 상위에 Pointcut 인터페이스를 가진다.
  • printMethod() 테스트는 MemberServiceImpl.hello(String) 메서드의 정보를 출력해준다.
helloMethod=public java.lang.String hello.aop.member.MemberServiceImpl.hello(java.lang.String)
  • 이번에 알아볼 execution 으로 시작하는 포인트컷 표현식은 
  • 이 메서드 정보를 매칭해서 포인트컷 대상을 찾아낸다.

 

 

4. GitHub: 220105 SpringAop - Pointcut


 

GitHub - bsh6463/SpringAOP

Contribute to bsh6463/SpringAOP development by creating an account on GitHub.

github.com

 

Comments