15. AOP (Aspect-Oriented Programming) 관점 지향 프로그래밍
- AOP는 OOP(객체 지향 프로그래밍)를 대체하기 위한 것이 아니라 횡단 관심사를 깔끔하게 처리하기 어려운 OOP의 부족한 부분을 보조하는 목적으로 개발되었다.
- 코드를 작성하다보니 부가 기능 코드가 중복되어 사용되었다. 이걸 어떻게 분리할까? 하는 생각에 만들어졌다.
- 핵심 기능과 부가 기능을 분리하는게 목적이다.
- 대표적인 구현으로 ASPECTJ (JAVA)프레임워크를 사용한다. 스프링도 aop지원 기능을 가지고있는데 대부분 aspectj를 사용한다. 근데 aspectj는 너무 어렵고 공부할게 많다. 반면에 스프링 aop는 별도의 추가 자바 설정이 없이 스프링만 있으면 편리하게 AOP를 사용할 수 있다. 그리고 실무에서는 스프링이 지원하는 AOP만 써도 99%는 해결된다.
- 스프링 AOP는 런타임 시점에 프록시를 통해서 부가 기능을 적용하는 방식을 사용한다.
- 프록시를 사용하는 스프링 AOP의 조인 포인트는 메서드 실행으로 제한된다.
- 스프링 AOP는 스프링 컨테이너가 관리할 수 있는 스프링 빈에만 AOP를 적용할 수 있다.
- AOP의 3가지 적용 시점
- 용어
- 조인 포인트
- 포인트컷
- 조인 포인트 중에서 어드바이스가 적용될 위치를 선별하는 기능이다.
- 타켓
- 어드바이스를 받을 객체, 포인트컷으로 설정한다.
- 어드바이스
- 에스펙트
- 어드바이저
- 위빙