Category: 포인트컷 표현식

0

[Spring AOP] 포인트컷 표현식 - this, target

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 포인트컷 표현식 - this, targetthis 와 target 은 객체를 대상으로 AOP 를 적용할 때 사용하는 포인트컷 표현식입니다. 두 표현식 모두 상위 타입이나 인터페이스를 이용해 AOP 를 적용할 수 있습니다. this 는 스프링 빈으로 등록돼 있는 프록시 객체를 대상으로 Advice 를 적용합니다. 때문에 스프링에서 프록시 객체를 생성하는 전략에 따라 AOP 가 다르게 적용될 수 있습니다. target 은 실제 객체를 대상으로 Advice 를 적용합니다. this 와 target 작동방식 확인을 위한 테스트 코드@AutowiredMemberService memberService;@Testvoid success() { log.info("memberService Proxy={}", memberService.getClass()); memberService.hello("helloA");}@Aspectstatic class ThisTargetAspect { //부모 타입 허용 @Around("this(hello.aop.member.MemberService)") public Object doThisInterface(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[this-interface] {}", joinPoint.getSignature()); return joinPoint.proceed(); } //부모 타입 허용 @Around("target(hello.aop.member.MemberService)") public Object doTargetInterface(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[target-interface] {}", joinPoint.getSignature()); return joinPoint.proceed(); } @Around("this(hello.aop.member.MemberServiceImpl)") public Object doThis(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[this-impl] {}", joinPoint.getSignature()); return joinPoint.proceed(); } @Around("target(hello.aop.member.MemberServiceImpl)") public Object doTarget(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[target-impl] {}", joinPoint.getSignature()); return joinPoint.proceed(); }} JDK 동적 프록시를 이용한 프록시 객체 생성

0

[Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 Advice 에 매게변수 전달포인트 컷 포현식을 사용해서 AOP 가 적용되는 객체나 메소들의 정보를 Advice 내 매개변수로 정보들을 전달 할 수 있습니다. Advice 로 매개변수로 정보들을 넘기기 위해서는 포인트컷의 이름과 매개변수 이름이 같아야 합니다. 또한, 값이 들어올때 타입이 매개변수에서 정의한 타입으로 제한이 됩니다. AOP 적용 확인을 위한 테스트 코드@AutowiredMemberService memberService;@Testvoid success() { log.info("memberService Proxy={}", memberService.getClass()); memberService.hello("helloA");} 메소드 매개변수값 정보를 Advice 에 전달args 를 이용하면 메소드에 전달된 매개변수의 값 정보를 가져올 수 있습니다.

0

[Spring AOP] 포인트컷 표현식 - bean

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 bean bean 표현식은 스프링 컨테이너에 등록된 Bean 이름을 기준으로 Advice 적용 여부를 판단합니다. bean은 AspectJ 표준 지시자가 아닌 스프링 AOP에서만 사용 가능한 지시자입니다. 패키지나 타입 대신 Bean 이름을 기준으로 Advice를 적용하고 싶을 때 유용합니다. bean(빈 이름 패턴) 와일드카드 *를 사용해 이름 패턴으로 여러 Bean을 한 번에 지정할 수 있습니다. 기본 사용 예시특정 Bean 이름 지정

0

[Spring AOP] 포인트컷 표현식 - @args

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 @args 메서드내 매개변수에 특정 어노테이션을 가지고 있는 경우 조인 포인트를 매칭 @args 는 메서드내 매개변수가 특정 어노테이션을 가지고 있는 경우 Advice 를 적용합니다. 런타임 시점에서 해당 어노테이션이 실제로 존재하는지 확인합니다. 커스텀 어노테이션@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {} Advisor 생성매개변수가 @MyAnnotation 를 가질 경우 advice 를 적용하기 위해 Adivsor 를 생성합니다.

0

[Spring AOP] 포인트컷 표현식 - @annotation

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 @annotation 포인트컷 메서드가 주어진 어노테이션을 가지고 있는 조인 포인트를 매칭 @annotation 은 특정 어노테이션이 적용된 메소드 를 기준으로 Advice 를 적용할때 사용하는 표현식입니다. @annotation(어노테이션_클래스) 위와 같이 @annotation 에서 정의된 어노테이션에 Advice 를 적용합니다. @annotation 포인트컷 사용하기1. Custom Annotation 생성

0

[Spring AOP] 포인트컷 표현식 - @target, @within

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 @target 과 @within 클래스에 특정 어노테이션이 적용된 경우 조인 포인트를 매칭 @target 과 @within 은 어노테이션을 기준으로 AOP를 적용하기 위해 사용되는 포인트컷 표현식입니다. 특정 어노테이션이 적용된 클래스내 모든 메서드에 AOP 를 적용합니다. @target 은 런타임 객체의 실제 타입 을 기준으로 매칭합니다. 이 과정에서 해당 객체의 부모 클래스나 인터페이스에 선언된 어노테이션도 포함하여 검사하기 때문에 부모 타입의 메서드까지 Advice 를 다 적용합니다. @within 경우 컴파일 시점 에 특정 클래스 에 어노테이션이 선언되었는지를 기준으로 확인하기 때문에 정의된 클래스내 메서드에만 Advice 를 적용합니다. Custon Annotation 생성

0

[Spring AOP] 포인트컷 표현식 - args

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 포인트컷 표현식 - args args 는 메서드의 매개변수를 기준으로 AOP 를 적용 args는 메서드의 매개변수를 기준으로 매칭하기 위한 표현식입니다. 메서드의 매개변수 타입이나 값을 기준으로 특정 메서드에만 AOP를 적용할 수 있습니다. 기본 문법args(타입1, 타입2, ...) 표현식 설명 args() 파라미터가 없는 메서드 args(*) 파라미터가 정확히 1개인 메서드 args(..) 파라미터 개수 및 타입 제한 없음 args(String) 파라미터가 String 타입 1개인 메서드 args(String,..) 첫 번째 파라미터가 String 이고 나머지는 제한 없음 args(Object) 파라미터가 Object 또는 그 하위 타입 1개인 메서드 (런타임 판단) 테스트를 위한 객체

0

[Spring AOP] 포인트컷 표현식 - within

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 within within 은 특정 타입 에 대해 Advice 를 적용합니다. 특정 타입이 within 을 만족 하면 해당 타입내 모든 메소드는 Advice 가 적용됩니다. // com.example.aop.member.MemberServiceImpl 타입을 대상으로 Advice 를 적용합니다.within(com.example.aop.member.MemberServiceImpl)// com.example.aop.member 패키지내 타입 이름에 Service 가 들어가면 Advice 를 적욯합니다.within(com.example.aop.member.*Service*)// com.example.aop 패키지와 하위 패키지내 모든 타입에 Advice 를 적용합니다.within(com.example.aop..*) execution 과 within 의 차이within 은 표현식은 execution 과 다르게 부모 타입을 지정했을 경우 자식 타입에는 Advice 가 적용되지 않습니다. 즉, 상속이나 구현을 통해 생성된 객체에는 Advice 가 적용되지 않고 정확하게 지정된 타입에만 적용되는 점에서 execution 과 차이가 있습니다. @Test@DisplayName("타겟의 타입에만 직접 적용, 인터페이스를 선정하면 안된다.")void withinSuperTypeFalse() { pointcut.setExpression("within(com.example.aop.member.MemberService)"); assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isFalse();}@Test@DisplayName("execution은 타입 기반, 인터페이스 선정 가능")void executionSuperTypeTrue() { pointcut.setExpression("execution(* com.example.aop.member.MemberService.*(..))"); assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isTrue();}

0

[Spring AOP] 포인트컷 표현식 - execution

목차 [Spring AOP] 포인트컷 [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @target, @within 포인트컷 표현식 - execution execution( [접근제어자] 리턴타입 [선언타입] 메소드이름(파라미터) [예외] ) execution은 포인트컷 표현식에서 가장 많이 사용되는 지시자입니다. 메소드의 접근 제어자, 리턴 타입, 선언된 패키지 및 클래스, 파라미터, 예외 정보를 조합해 다양한 조건으로 포인트컷을 정의할 수 있습니다. 대괄호([ ])로 표시된 접근제어자, 선언타입, 예외는 생략이 가능합니다. 기본 사용 예시execution(public String com.example.aop.member.MemberServiceImpl.hello(String))