Spring AOP Pointcut 표현식 - bean

목차

bean

bean 표현식은 스프링에서만 사용한 포인트컷 지시자로 Bean 이름으로 Advice 적용 여부를 판단합니다.

@Slf4j
@Import(BeanTest.BeanAspect.class)
@SpringBootTest
public class BeanTest {

@Autowired
OrderService orderService;

@Test
void success(){
orderService.orderItem("itemA");
}

@Aspect
static class BeanAspect{
// orderService 빈과 Repository 로 끝나는 빈들에 Advice 를 적용합니다.
@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] 실행
Share