[Spring] 스프링 AOP ( Aspect Oriented Programming )

[Spring] 스프링 AOP ( Aspect Oriented Programming )

AOP ( Aspect Oriented Programming ) : 관점 지향 프로그래밍

문제를 해결하기 위한 핵심적인 부분( CC : Core Concern )과, 전체에 적용되는 공통적인 부분( Cross Cutting Concern )을 나누어 프로그래밍함으로써 공통모듈을 여러 코드에 쉽게 적용할 수 있도록 지원하는 기술

공통적인 부분을 한데 모아 모듈화하여 핵심 로직으로부터 분리하고 해당 기능을 프로그램 코드에서 직접 명시하지 않고 선언하여 적용한다.

공통 관심사항을 분리하는 AOP를 적용

AOP 용어

CC (Core Concern) : 핵심 관심사항 ( = target )

CCC (Cross Cutting Concern) : 공통 관심사항

Advice : CCC의 실제구현체, 코드 ( Pointcut에서 지정한 JoinPoint에 삽입되어야 하는 코드 )

JoinPoint : CC를 호출하는 모든 시점, Advice가 연결될 수 있는 모든 시점

Pointcut : Advice가 어떤 JoinPoint에 적용되어야 하는지 정의 ( 어떤 CC를 호출할 때 묶어줄건지 알려주는 애 )

Aspect ( Advisor ) : 흩어진 관심사항들을 모듈화한 것 ( Advice + Pointcut )

proxy : CC(target)을 호출할 때 target인 척 하여 요청을 대신 받는 애

Weaving : Advice를 비즈니스 로직 코드에 삽입하는 것, Aspect를 대상 객체에 적용하여 새로운 프록시 객체를 생성하는 과정, 엮여서 실제로 실행된 것

예제로 이해하기

pom.xml : maven dependency 추가

org.springframework spring-core 5.2.4.RELEASE org.springframework spring-context 5.2.4.RELEASE org.springframework spring-aop 5.2.4.RELEASE org.aspectj aspectjweaver 1.9.2 org.aspectj aspectjrt 1.9.2

Human.java 인터페이스

package com.test01; public interface Human { String sayName(String name); String sayJob(String job); }

Human을 상속받은 Person.java Class

package com.test01; import org.springframework.stereotype.Component; @Component public class Person implements Human { private String name; private String job; public void setName(String name) { this.name = name; } public void setJob(String job) { this.job = job; } @Override public String sayName(String name) { System.out.println("나의 이름은 " + name + " 입니다."); return name; } @Override public String sayJob(String job) { System.out.println("나의 직업은 " + job + " 입니다."); return job; } }

MyAdvice.java

package com.test01; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class MyAdvice { public MyAdvice() { } @Before("execution(public String sayName(..))") public void beforeSaying(JoinPoint join) { System.out.println("당신의 이름은 무엇입니까?"); } @After("execution(public String sayName(..))") public void afterSaying(JoinPoint join) { System.out.println("이름이 멋지시네요."); } @AfterReturning("execution(public String sayName(..))") public void afterReturnSaying(JoinPoint join) { System.out.println("직업이 무엇입니까?"); } }

MTest.java

package com.test01; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MTest { public static void main(String[] args) { ApplicationContext factory = new ClassPathXmlApplicationContext("com/test01/aopAppcontext.xml"); Human person01 = factory.getBean("person", Human.class); person01.sayName("뽀로로"); person01.sayJob("개발자"); System.out.println("------------------------"); person01.sayName("둘리"); person01.sayJob("개그맨"); } }

aopAppcontext.xml ( Spring Bean Configuration File ) : namespace에 context, aop 추가하기

실행결과

from http://printf100.tistory.com/39 by ccl(A) rewrite - 2020-03-24 21:54:31

댓글

이 블로그의 인기 게시물

[Linux] 파일 로그 보기(tail)

[샤니마스 SPRING PARTY2020] THE IDOLM @ STER SHINY COLORS SPRING...

2020 LCK 롤챔스 Spring 경기 재개 및 일정