본문 바로가기
Spring|Spring-boot/Spring AOP

Pointcut - within, args

by oncerun 2022. 1. 29.
반응형

within

 

within 지시자는 특정 타입 내의 조인 포인트에 대한 매칭을 제한한다.

해당 타입이 매칭 되면 그 안의 메서드들이 자동적으로 매칭 된다.

즉 execution에서 타입 부분만 사용한다.

 

AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    pointcut.setExpression("within(hello.aop.example.Example)");
    Method exampleMethod = Example.class.getMethod("test", String.class);
    assertThat(pointcut.matches(exampleMethod, Example.class)).isTrue();

AspectJExpressionPointcut pointcut2 = new AspectJExpressionPointcut();
    pointcut2.setExpression("within(hello.aop.example.Example)");
    Method exampleMethod2 = Example.class.getMethod("internal", String.class);
    assertThat(pointcut2.matches(exampleMethod2, Example.class)).isTrue();
    
AspectJExpressionPointcut pointcut3 = new AspectJExpressionPointcut();
    pointcut3.setExpression("within(hello.aop..*)");
    Method exampleMethod3 = Example.class.getMethod("internal", String.class);
    assertThat(pointcut3.matches(exampleMethod3, Example.class)).isTrue();

* pointcut의 Expression은 한번 지정하면 변경할 수 없다. 따라서 point컷을 매번 새로 만들어야 한다.

 

* 주의할 점은 부모 타입을 지원하지 않아 인터페이스로 지정하면 안 된다.

@Test
void withinSuperType() throws NoSuchMethodException {
    AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    pointcut.setExpression("within(hello.aop.example.ExampleInterface)");
    Method exampleMethod = Example.class.getMethod("test", String.class);
    assertThat(pointcut.matches(exampleMethod, Example.class)).isFalse();
}

 

 

args

 

args지시자도 within과 마찬가지로 execution의 파라미터 부분을 추출한 것이다.

다만 execution은 파라미터 타입이 정확하게 매칭 되어야 하는데,

이는 execution은 클래스에 선언된 정보를 기반으로 판단하기 때문이다.

하지만 args는 부모 타입을 허용한다. args는 실제 넘어온 파라미터 객체 인스턴스를 보고 판단한다. 

 

Example 메소드 시그니처.

 exampleMethod = public java.lang.String hello.aop.example.Example.test(java.lang.String)

 

Args 테스트 코드


@BeforeEach
public void init() throws NoSuchMethodException {
    testMethod = Example.class.getMethod("test", String.class);
}

private AspectJExpressionPointcut pointcut(String expression) {
    AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    pointcut.setExpression(expression);
    return pointcut;
}
@Test
void args() {
    //test(String)과 매칭
    assertThat(pointcut("args(String)")
            .matches(testMethod, Example.class)).isTrue();
    assertThat(pointcut("args(Object)")
            .matches(testMethod, Example.class)).isTrue();
    assertThat(pointcut("args()")
            .matches(testMethod, Example.class)).isFalse();
    assertThat(pointcut("args(..)")
            .matches(testMethod, Example.class)).isTrue();
    assertThat(pointcut("args(*)")
            .matches(testMethod, Example.class)).isTrue();
    assertThat(pointcut("args(String,..)")
            .matches(testMethod, Example.class)).isTrue();
}

 

보통 args 지시자는 단독으로 사용되기보다 파라미터 바인딩에서 주로 사용된다.

 

 

반응형

'Spring|Spring-boot > Spring AOP' 카테고리의 다른 글

Spring AOP 매개변수 활용  (0) 2022.01.30
Spring AOP @target, @within  (0) 2022.01.29
Pointcut - execution  (0) 2022.01.29
Spring AOP (2)  (0) 2022.01.29
Spring AOP  (0) 2022.01.27

댓글