[SPRING] aspect 환경설정와 AOP
oop(object oriented programming) 와 aop(aspect oriented programming)
AOP (Aspect Oriented Programming) : 스프링 핵심 (IoC, DI(결합도 관련) 그리고 AOP) 중 하나. 관점 지향 프로그래밍이다.
흩어진 관심사를 Aspect로 모듈화(어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각 모듈화) 하고 핵심적인 비즈니스 로직에서 분리하여 재사용하는 것이다. 응집도를 높인다.
JoinPoint : 호출하는 모든 비즈니스 메소드이다. joinpoint 중 pointcut 이 선택된다. (ServiceImpl 클래스의 모든 메소드들을 조인 포인트라고 생각하면 된다.)
Pointcut : 필터링된 joinpoint이다. 수 많은 비즈니스 메소드들 중 원하는 특정 메소드에서만 횡단 관심에 해당하는 공통 기능을 수행시키기 위한 것이다.
Advice : 횡단 관심에 해당하는 공통기능의 코드. 독립된 클래스의 메소드로 작성.
Weaving : pointcut 으로 지정한 핵심 관심 메소드가 호출될 때 어드바이스에 해당하는 횡단관심 메소드가 삽입되는 과정. 위빙을 통해 비즈니스 메소드를 수정하지 않고 횡단관심에 해당하는 기능을 추가 변경할 수 있다.
Aspect or Advisor : pointcut 메소드에 대해 어떤 advice 메소드를 실행할지 결정. aspect 설정에 따라 aop 방식이 결정된다.
포인트컷 표현식은 따로 포스팅 하겠다.
aspect 기능 사용하기 위한 환경설정
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.springbook.biz"></context:component-scan>
<bean id="log" class="com.springbook.biz.common.Log4jAdvice"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.springbook.biz..*Impl.*(..))" id="allPointcut"/>
<aop:aspect ref="log">
<aop:before method="printLogging" pointcut-ref="allPointcut"/>
</aop:aspect>
</aop:config>
</beans>