Category: Spring

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를 적용할 수 있습니다. 테스트를 위한 객체@ClassAop@Componentpublic class MemberServiceImpl implements MemberService { @Override @MethodAop("test value") public String hello(String param) { return "ok"; } private String internal(String param) { return "ok"; }} args 를 이용해 적용 여부 판단@BeforeEachpublic void init() throws NoSuchMethodException { helloMethod = MemberServiceImpl.class.getMethod("hello", String.class);}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(); // Object 는 String 의 상위 타입 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();}

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] 포인트컷 표현식 - 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] 포인트컷 포인트컷 표현식 - execution execution( [접근제어자] 리턴타입 [선언타입] 메소드이름(파라미터) [예외] ) execution 는 포인트컷 표현식에서 가장 많이 사용되는 명시자 입니다. execution 는 메소드의 접근 제어자, 리턴 타입, 메소드가 선언된 패키지, 클래스 정보, 메소드 파라미터, 예외처리 정보를 이용해 다양한 조건으로 포인트컷 을 적용할 수 있도록 제공합니다. 접근제어자, 선언타입, 예외의 경우는 생각이 가능합니다. execution 사용 예시execution(public String com.example.aop.member.MemberServiceImpl.hello(String))

0

[Spring AOP] 포인트컷

목차 [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(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); }}

0

[Spring AOP] - Advice 종류

목차 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 Spring 핵심원리 고급편 - 어드바이스 종류 종류 설명 @Around 메서드 호출 전 후에 실행, 가장 강력한 어드바이스, 조인 포인트 실행 여부 선택, 반환 값 변환, 예외 변환 등이 가능 @Before 조인 포인트 실행 이전에 실행 @AfterReturning 조인 포인트가 정상 완료 후 실행 @AftThrowing 메서드가 예외를 던지는 경우 실행 @After 조인 포인트가 정상 또는 예외에 관계 없이 실행 @Slf4j@Aspectpublic class AspectV6Advice { // com.example.aop.order 하위 패키지면서 클래스 이름 패턴이 *Service 인 것 @Around("com.example.aop.order.aop.Pointcuts.orderAndService()") public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable{ try{ // @Before log.info("[트랜잭션 시작] {}", joinPoint.getSignature()); Object result = joinPoint.proceed(); // @AfterReturning log.info("[트랜잭션 커밋] {}", joinPoint.getSignature()); return result; }catch (Exception ex){ // @AfterThrowing log.info("[트랜잭션 롤백] {}", joinPoint.getSignature()); throw ex; }finally { // @After log.info("[리소스 릴리즈] {}", joinPoint.getSignature()); } }} @Slf4j@Aspectpublic class AspectV6Advice { // com.example.aop.order 하위 패키지면서 클래스 이름 패턴이 *Service 인 것 @Around("com.example.aop.order.aop.Pointcuts.orderAndService()") public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable{ try{ // @Before log.info("[트랜잭션 시작] {}", joinPoint.getSignature()); Object result = joinPoint.proceed(); // @AfterReturning log.info("[트랜잭션 커밋] {}", joinPoint.getSignature()); return result; }catch (Exception ex){ // @AfterThrowing log.info("[트랜잭션 롤백] {}", joinPoint.getSignature()); throw ex; }finally { // @After log.info("[리소스 릴리즈] {}", joinPoint.getSignature()); } } // @Before 은 그냥 실행해준다. @Before("com.example.aop.order.aop.Pointcuts.orderAndService()") public void doBefore(JoinPoint joinPoint){ log.info("[before] {}", joinPoint.getSignature()); } // return 값을 조작을 할 수는 있지만 바꿀 수 는 없다. @AfterReturning(value = "com.example.aop.order.aop.Pointcuts.orderAndService()", returning = "result") public void doReturn(JoinPoint joinPoint, Object result){ log.info("[return] {} return = {}", joinPoint.getSignature(), result); } // 자동으로 throw ex 를 해준다. @AfterThrowing(value = "com.example.aop.order.aop.Pointcuts.orderAndService()", throwing = "ex") public void doThrowing(JoinPoint joinPoint, Exception ex){ log.info("[ex] {} message = {}", ex); } @After(value = "com.example.aop.order.aop.Pointcuts.orderAndService()") public void doAfter(JoinPoint joinPoint){ log.info("[after] {}", joinPoint.getSignature()); }} 모든 어드바이스는 JoinPoint 를 첫번째 파라미터에 사용할 수 있다. 단 @Around 는 ProceedingJoinPoint 를 사용해야 한다.ProceedingJoinPoint는 JoinPoint 의 하위 타입니다. JoinPoint 인터페이스의 주요 기능 Method 설명 getArgs 메서드 인수를 반환한다. getThis 프록시 객체를 반환한다. getTarget 대상 객체를 반환한다. getSignature 조언되는 메서드에 대한 설명을 반환한다. toString 조언되는 방법에 대한 유용한 설명을 인쇄한다.

0

[Spring AOP] - 트랜잭션 순서

목차 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 Spring 핵심원리 고급편 - 트랜잭션 순서@Slf4jpublic class AspectV5Order { @Aspect @Order(2) public static class LogAspect{ @Around("com.example.aop.order.aop.Pointcuts.allOrder()") public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable{ log.info("[log] {}", joinPoint.getSignature()); // join point 시그니처 return joinPoint.proceed(); } } @Aspect @Order(1) public static class TxAspect{ // com.example.aop.order 하위 패키지면서 클래스 이름 패턴이 *Service 인 것 @Around("com.example.aop.order.aop.Pointcuts.orderAndService()") public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable{ try{ log.info("[트랜잭션 시작] {}", joinPoint.getSignature()); Object result = joinPoint.proceed(); log.info("[트랜잭션 커밋] {}", joinPoint.getSignature()); return result; }catch (Exception ex){ log.info("[트랜잭션 롤백] {}", joinPoint.getSignature()); throw ex; }finally { log.info("[리소스 릴리즈] {}", joinPoint.getSignature()); } } }}

0

[Spring AOP] - 포인트컷 참조

목차 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 Pointcutpublic class Pointcuts { @Pointcut("execution(* com.example.aop.order..*(..))") public void allOrder(){} // 클래스 이름 패턴이 *Service @Pointcut("execution(* *..*Service.*(..))") public void allService(){} // allOrder && allService @Pointcut("allOrder() && allService()") public void orderAndService(){}} 외부 Pointcut 참조@Slf4j@Aspectpublic class AspectV4PointCut { @Around("com.example.aop.order.aop.Pointcuts.allOrder()") public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable{ log.info("[log] {}", joinPoint.getSignature()); // join point 시그니처 return joinPoint.proceed(); } // com.example.aop.order 하위 패키지면서 클래스 이름 패턴이 *Service 인 것 @Around("com.example.aop.order.aop.Pointcuts.orderAndService()") public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable{ try{ log.info("[트랜잭션 시작] {}", joinPoint.getSignature()); Object result = joinPoint.proceed(); log.info("[트랜잭션 커밋] {}", joinPoint.getSignature()); return result; }catch (Exception ex){ log.info("[트랜잭션 롤백] {}", joinPoint.getSignature()); throw ex; }finally { log.info("[리소스 릴리즈] {}", joinPoint.getSignature()); } }} @Slf4j@SpringBootTest@Import(AspectV4PointCut.class)public class AopTest { @Autowired OrderService orderService; @Autowired OrderRepository orderRepository; @Test public void aopTest(){ log.info("isAopProxy, orderService = {}", AopUtils.isAopProxy(orderService)); log.info("isAopProxy, orderRepository = {}", AopUtils.isAopProxy(orderRepository)); } @Test public void success(){ orderService.orderItem("itemA"); } @Test public void exception(){ assertThatThrownBy(() -> orderService.orderItem("ex")) .isInstanceOf(IllegalStateException.class); }}

0

[Spring AOP] - 어드바이스 추가

목차 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 Advice 추가 기존에 로그를 찍은 기능에서 트랜잭션 기능 추가 allOrder com.example.aop.order 하위 패키지를 대상으로 포인트 컷 적용 allService 타입 이름 패턴이 *Service 를 대상으로 포인트 컷 적용 타입 이름 패턴 : 클래스, 인터페이스 모두 적용 @Around(“allOrder() && allService()”) 포인트 컷은 조합이 가능하다 && : AND || : OR ! : NOT @Slf4j@Aspectpublic class AspectV3 { @Pointcut("execution(* com.example.aop.order..*(..))") private void allOrder(){} // 클래스 이름 패턴이 *Service @Pointcut("execution(* *..*Service.*(..))") private void allService(){} @Around("allOrder()") public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable{ log.info("[log] {}", joinPoint.getSignature()); // join point 시그니처 return joinPoint.proceed(); } // com.example.aop.order 하위 패키지면서 클래스 이름 패턴이 *Service 인 것 @Around("allOrder() && allService()") public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable{ try{ log.info("[트랜잭션 시작] {}", joinPoint.getSignature()); Object result = joinPoint.proceed(); log.info("[트랜잭션 커밋] {}", joinPoint.getSignature()); return result; }catch (Exception ex){ log.info("[트랜잭션 롤백] {}", joinPoint.getSignature()); throw ex; }finally { log.info("[리소스 릴리즈] {}", joinPoint.getSignature()); } }}

0

[Spring AOP] - @Pointcut 포인트컷 분리

목차 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 ✅ Spring AOP - 포인트컷 분리 하나의 Pointcut 표현식을 여러 어드바이스에서 함께 사용하는 것이 가능하다. 스프링 AOP 에서는 Advice 를 적용하기 위한 포인트컷을 별로로 분리해 생성할 수 있습니다. 덕분에 같은 로직을 여러번 작성해야 하는 코드의 중복을 방지해 코드의 재사용성과 유지보수성을 향상시킬 수 있습니다. 포인트컷은 크게 두가지 구성요소로 이루어집니다. 첫번째는 어디에 Advice 를 적용할지 정의하는 포인트컷 표현식 과 두번째는 포인트컷을 정의하는 메서드의 이름과 매개변수를 의미하는 포인트컷 시그니처 로 이루어져 있습니다. ✅ @Pointcut - 포인트컷 생성하기포인트컷을 별도로 생성하기 위해서는 @Pointcut 을 사용하여 포인트컷 표현식을 메서드로 정의합니다. 이때, 메서드의 반환 타입은 void 여야 하고 코드 내용은 비어있어야 합니다. @Pointcut 이 붙은 메서드를 Advice 가 참조하면 포인트컷 표현식에 따라 부가기능 적용 여부를 판단하게 됩니다. AspectV2@Slf4j@Aspectpublic class AspectV2 { @Pointcut("execution(* com.example.aop.order..*(..))") private void allOrder(){ } @Around("allOrder()") // 포인트컷 표현식이 작성된 포인트컷 시그니처를 지정합니다. public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable{ log.info("[log] {}", joinPoint.getSignature()); // join point 시그니처 return joinPoint.proceed(); }}

0

[Spring AOP] - @Aspect

목차 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 ✅ @Aspect 작동방식스프링 애플리케이션은 로딩 시점에 자동 프록시 생성기 를 호출되며 스프링 컨테이너에서 @Aspect 가 붙은 Bean 들을 검색합니다. 검색된 각 @Aspect 빈에 대해, @Aspect 어드바이저 빌더를 사용하여 어드바이저를 생성합니다. 생성된 어드바이저는 @Aspect 어드바이저 빌더 내부에 저장됩니다. 1. 자동 프록시 생성기 역할 - @Aspect 어드바이저 생성@Aspect 가 적용된 Bean 들을 조회합니다. 해당 Bean 내 정의된 Advice 와 Pointcut 표현식을 이용해 Advisor 를 생성 후 저장합니다. 2. 자동 프록시 생성기 역할 - @Aspect 어드바이저 적용Bean 객체 생성시 Bean 후처리기에서 Advisor 객체를 조회해 Pointcut 표현식에 해당되는 Bean 이 있을 경우 자동 프록시 생성기에서 Advice(부가기능) 가 적용된 프록시 객체를 생성합니다.

0

[Spring AOP] - 용어 정리

목차 [Spring AOP] - Advice 종류 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 [Spring AOP] - Aspect 참고본 포스트는 김영한의 스프링 핵심 원리 - 고급편 내용을 참고해 만들었습니다. ✅ 조인 포인트 (JoinPoint) 어드바이스가 적용될 수 있는 위치 메소드 실행, 생성자 호출, 필드 값 접근, static 메서드 접근 같은 프로그램 실행 중 지점 AOP 를 적용할 수 있는 모든 지점(추상적인 개념) Spring AOP 는 프록시 방식을 사용하므로 조인 포인트는 항상 메소드 실행 시점으로 제한된다. ✅ 포인트 컷 (Pointcut) 어드바이스가 적용될 위치를 선별하는 기능

0

[Spring AOP] - 적용 방식

목차 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 [Spring AOP] - Aspect 참고본 포스트는 김영한의 스프링 핵심 원리 - 고급편 내용을 참고해 만들었습니다. 횡단 관심 사항횡단 관심 사항(Cross-cutting Concern)은 애플리케이션 전반에서 공통적으로 사용되는 기능입니다. 즉, 여러 모듈에서 공통으로 사용되는 기능으로서, 여러 코드에 걸쳐 분산되어 있을 수 있습니다. 이러한 횡단 관심 사항은 핵심 비즈니스 로직과 분리되어 있기 때문에 애플리케이션의 유지 보수성과 확장성에 영향을 미칩니다. 횡단 관심 사항의 예로는 로깅, 보안, 트랜잭션 처리 등이 있습니다. 예를 들어, 여러 모듈에서 공통적으로 로그를 출력해야 한다면, 모든 코드에 로그를 출력하는 코드를 추가해야 합니다. 하지만 이러한 방식은 유지 보수성이 떨어지고 코드 중복이 발생합니다. 이를 해결하기 위해 로그 출력과 같은 공통 기능을 모듈화하고 재사용 가능한 코드로 만들어주는 것이 바로 AOP의 역할입니다. AOP를 사용하면 핵심 로직 코드를 수정하지 않고도 횡단 관심 사항을 처리할 수 있습니다. 즉, 공통 기능을 모듈화하여 애플리케이션 전반에서 쉽게 사용할 수 있게 됩니다. 이를 통해 애플리케이션의 유지 보수성과 확장성이 향상되며, 코드의 가독성도 높아지는 등의 장점을 가집니다. Spring AOP AOP 를 사용하면 핵심기능 과 부가기능 이 코드상 완전히 분리 된다.

0

[Spring AOP] - Aspect

목차 [Spring AOP] - Advice 종류 [Spring AOP] - 트랜잭션 순서 [Spring AOP] - 포인트컷 참조 [Spring AOP] - 어드바이스 추가 [Spring AOP] - @Pointcut 포인트컷 분리 [Spring AOP] - @Aspect [Spring AOP] - 용어 정리 [Spring AOP] - 적용 방식 [Spring AOP] - Aspect 참고본 포스트는 김영한의 스프링 핵심 원리 - 고급편 내용을 참고해 만들었습니다. Aspect 부가 기능 과 부가 기능 을 어디에 적용할지 적용할지를 정의 Aspect 는 애플리케이션 전반에서 공통적으로 사용되는 기능들을 모듈화하고, 재사용 가능한 코드로 만들어주는 모듈화 기능입니다. Aspect 를 사용한 프로그래밍을 관점 지향 프로그래밍 AOP (Aspect-Oriented Programming) 이라 합니다. AOP 를 적용함으로써 핵심 로직 코드를 수정하지 않고도 횡단 관심 사항(cross-cutting concern) 을 처리할 수 있습니다. Spring에서는 Aspect를 정의하기 위해 @Aspect 어노테이션을 사용하며, Aspect를 구현하기 위해 @Before, @After, @Around 등의 Advice 어노테이션을 사용합니다. 이러한 어노테이션을 사용하여 Advice를 구현하고, @Pointcut 어노테이션을 사용하여 Pointcut을 정의합니다. Aspect를 구현하여 애플리케이션 전반에 걸쳐 공통 기능을 모듈화하고 재사용 가능한 코드로 만들 수 있습니다. 이를 통해 애플리케이션의 유지 보수성과 확장성을 향상시킬 수 있습니다.

0

BeanPostProcessor - 하나의 프록시에 여러개의 Advisor 적용

목차 BeanPostProcessor - 하나의 프록시에 여러개의 Advisor 적용 BeanPostProcessor - AutoProxyCreator BeanPostProcessor 를 이용한 프록시 객체 생성 BeanPostProcessor Spring 핵심원리 고급편 - ProxyFactory 적용 Spring 핵심원리 고급편 - 여러 Advisor 와 함께 적용 Spring 핵심원리 고급편 - Spring 에서 제공하는 Pointcut Spring 핵심원리 고급편 - Pointcut 만들기 Spring 핵심원리 고급편 - Advisor Spring 핵심원리 고급편 - MethodInterceptor Spring 핵심원리 고급편 - ProxyFactory 참고본 포스트는 김영한의 스프링 핵심 원리 - 고급편 내용을 참고해 만들었습니다. 여러개의 Advisor 적용빈 후처리기에서 프록시 객체 생성시 여러개의 포인트컷에 해당되는 객체가 있습니다. 객체가 여러개의 포인트 컷에 해당되더라도 프록시 객체는 한개만 생성됩니다. Advisor1 의 포인트 컷만 만족 프록시 객체 1개 생성, advisor1 만 포함 Advisor1, Advisor2 의 포인트 컷 모두 만족 프록시 객체 1개 생성, advisor1, advisor2 모두 포함 Advisor1, Advisor2 의 포인트 컷 모두 만족하지 않음 프록시 객체를 생성하지 않음 하나의 프록시에 여러개의 Advisor하나의 프록시 객체에 여러개의 Advisor 를 포함하는 형태로 프록시 객체가 생성됩니다.