AOP(Cross-Cutting)
핵심적인 기능에서 부가적인 기능을 분리한다. 분리한 부가기능을 Aspect라는 모듈 형태로 만들어서 설계하고 개발하는 방법
Aspect 구성
Aspect: 부가기능을 담고 있는 객체
Advise: 실질적인 부가기능을 담은 구현체, Aspect는 '무엇'을 '언제' 할지를 정의
- @Around, @Before, @After, @AfterReturning, @AfterThrowing
PointCut: 부가 기능이 적용되어야 할 대상(메소드)을 선정하는 방법
- execution(), @annotion, 등등
- execution(리턴 타입 타겟이 되는 메소드 argument-매개변수)
- ex) execution(* com.dashboard.service.DashboardService(..))
JointPoint: Advise가 적용될 위치, controller에서 정의된 method들의 args(매개변수)를 담고 있는 객체
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @Aspect public class UserAuthAspect { private static final Logger logger = LoggerFactory.getLogger(AccountAuthAspect.class); @Autowired private UserAuthService userAuthService; @Pointcut("execution(* com.demo.dashboard.service.DashboardService.getBoard(..))") public void dashBoardAccountAuth() {} @Pointcut("execution(* com.demo.billing.service.BillingService.*(..))") public void billingAccountAuth() {} @Before("dashBoardAccountAuth() || billingAccountAuth()") public void before(JoinPoint joinPoint) { Object[] obj = joinPoint.getArgs(); CommonReqModel model = (CommonReqModel) obj[0]; boolean hasDefaultAuth = userAuthService.getAuthInfo(model); model.setHasDefaultAuth(hasDefaultAuth);
} }
评论
发表评论