Category: Pointcut

0

Spring AOP Pointcut 표현식 - bean

목차 Spring AOP Pointcut 표현식 - this, target Spring AOP Pointcut 표현식 - 매게변수 전달 Spring AOP Pointcut 표현식 - bean Spring AOP Pointcut 표현식 - @annotation, @args Spring AOP Pointcut 표현식 - @target, @within Spring AOP Pointcut 표현식 - args Spring AOP Pointcut 표현식 - within Spring AOP - Pointcut 표현식 execution Spring 핵심원리 고급편 - Pointcut @Slf4j@Import(BeanTest.BeanAspect.class)@SpringBootTestpublic class BeanTest { @Autowired OrderService orderService; @Test void success(){ orderService.orderItem("itemA"); } @Aspect static class BeanAspect{ @Around("bean(orderService) || bean(*Repository)") public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[bean] {}", joinPoint.getSignature()); return joinPoint.proceed(); } }} 2021-12-21 00:13:43.463 INFO 35836 --- [ Test worker] com.example.aop.pointcut.BeanTest : [bean] void com.example.aop.order.OrderService.orderItem(String)2021-12-21 00:13:43.474 INFO 35836 --- [ Test worker] com.example.aop.order.OrderService : [orderService] 실행2021-12-21 00:13:43.475 INFO 35836 --- [ Test worker] com.example.aop.pointcut.BeanTest : [bean] String com.example.aop.order.OrderRepository.save(String)2021-12-21 00:13:43.480 INFO 35836 --- [ Test worker] com.example.aop.order.OrderRepository : [orderRepository] 실행

0

Spring AOP Pointcut 표현식 - args

목차 Spring AOP Pointcut 표현식 - this, target Spring AOP Pointcut 표현식 - 매게변수 전달 Spring AOP Pointcut 표현식 - bean Spring AOP Pointcut 표현식 - @annotation, @args Spring AOP Pointcut 표현식 - @target, @within Spring AOP Pointcut 표현식 - args Spring AOP Pointcut 표현식 - within Spring AOP - Pointcut 표현식 execution Spring 핵심원리 고급편 - Pointcut Spring 핵심원리 고급편 - args execution 은 파라미터 타입이 정확하게 일치돼야 한다. execution 은 클래스에 선언된 정보를 기반으로 판단한다. args 는 부모타입을 허용한다. args 는 실제로 넘어온 파라미터 객체 인스턴스를 보고 판단한다. private AspectJExpressionPointcut pointcut(String expression) { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(expression); return pointcut;}@Testvoid args() { //hello(String)과 매칭 assertThat(pointcut("args(String)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args(Object)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args()") .matches(helloMethod, MemberServiceImpl.class)).isFalse(); assertThat(pointcut("args(..)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args(*)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args(String,..)") .matches(helloMethod, MemberServiceImpl.class)).isTrue();} /** * execution(* *(java.io.Serializable)): 메서드의 시그니처로 판단 (정적) * args(java.io.Serializable): 런타임에 전달된 인수로 판단 (동적) */@Testvoid argsVsExecution() { // Args // args 는 상위 타입도 허용한다. assertThat(pointcut("args(String)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args(java.io.Serializable)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args(Object)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); // Execution // execution 은 정확하게 매칭돼야 한다. assertThat(pointcut("execution(* *(String))") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("execution(* *(java.io.Serializable))") //매칭 실패 .matches(helloMethod, MemberServiceImpl.class)).isFalse(); assertThat(pointcut("execution(* *(Object))") //매칭 실패 .matches(helloMethod, MemberServiceImpl.class)).isFalse();}

0

Spring 핵심원리 고급편 - Pointcut

목차 Spring AOP Pointcut 표현식 - this, target Spring AOP Pointcut 표현식 - 매게변수 전달 Spring AOP Pointcut 표현식 - bean Spring AOP Pointcut 표현식 - @annotation, @args Spring AOP Pointcut 표현식 - @target, @within Spring AOP Pointcut 표현식 - args Spring AOP Pointcut 표현식 - within Spring AOP - Pointcut 표현식 execution Spring 핵심원리 고급편 - Pointcut @Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface ClassAop {} @Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MethodAop { String value();} public interface MemberService { String hello(String param);} @ClassAop@Componentpublic class MemberServiceImpl implements MemberService { @Override @MethodAop("test") public String hello(String param) { return null; } public String internal(String param){ return "ok"; }} AspectJExpressionPointcut 이 포인트 컷 표현식을 처리해주는 클래스 @Slf4jpublic 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); }}