Search

79. 스프링부트 포토그램 유효성검사 자동화 AOP처리 1탄

AOP 처리

유효성 검사 AOP 처리

AOP란
Aspect Drientied Programming
관점 지향 프로그래밍
객체 지향 프로그래밍 + 관점 지향 프로그래밍
핵심기능은 다르지만 전처리 후처리는 같기 때문에 공통기능으로 둔다
공통기능 → 필터처리
핵심기능
aop 검색 후
pom.xml에 추가
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> <version>3.1.2</version> </dependency>
XML
복사
handler 폴더 안에 aop 폴더 생성 후 ValidationAdvice.java
package com.cos.photogramstart.handler.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Component //RestController, Service 모든 것들이 Compent를 상속해서 만들어져 있음. @Aspect public class ValidationAdvice { @Around("execution(* com.cos.photogramstart.web.api.*Controller.*(..))") public Object apiAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("web api 컨트롤러 ==========================="); // proceedingJoinPoint => profile 함수의 모든 곳에 접근할 수 있는 변수 // profile 함수보다 먼저 실행 return proceedingJoinPoint.proceed(); } @Around("execution(* com.cos.photogramstart.web.*Controller.*(..))") public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("web 컨트롤러 ==============================="); return proceedingJoinPoint.proceed(); } }
Java
복사
로그인 창만 가도 이렇게 로그가 찍혀서 나온다
api 컨트롤러만 로그가 찍힌다
인기페이지는 api가 없어서
web 컨트롤러만 찍힌다

*참고