스프링 AOP 란

스프링 AOP 란

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

@Slf4j @Aspect public class Aspect { //hello.aop.order 패키지와 하위 패키지 @Around( "execution(* hello.aop.order..*(..))" ) public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable { log.info( "[log] {}" , joinPoint.getSignature()); //join point 시그니처 return joinPoint.proceed(); } } @Slf4j @Repository public class OrderRepository { public String save( String itemId) { log.info( "[orderRepository] 실행" ); //저장 로직 if (itemId. equals ( "ex" )) { throw new IllegalStateException( "예외 발생!" ); } return "ok" ; } } @Slf4j @Service public class OrderService { private final OrderRepository orderRepository; public OrderService(OrderRepository orderRepository) { this .orderRepository = orderRepository; } public void orderItem( String itemId) { log.info( "[orderService] 실행" ); orderRepository.save(itemId); } } // 테스트 @Slf4j @SpringBootTest @Import(Aspect. class ) public class AopTest { @Autowired OrderService orderService; @Autowired OrderRepository orderRepository; @Test void aopInfo() { log.info( "isAopProxy, orderService={}" ,AopUtils.isAopProxy(orderService)); log.info( "isAopProxy, orderRepository={}" ,AopUtils.isAopProxy(orderRepository)); } @Test void success() { orderService.orderItem( "itemA" ); } @Test void exception() { assertThatThrownBy(() - > orderService.orderItem( "ex" )) .isInstanceOf(IllegalStateException. class ); } } Colored by Color Scripter

from http://katastrophe.tistory.com/100 by ccl(A) rewrite - 2021-12-31 16:27:09