JAVA/SPRING 2009. 5. 6. 11:15

AOP설정 엘리먼트 (스프링 인 액션 참고)
 AOP 설정 엘리먼트 목적 
 <aop:advisor>  AOP 어드바이저를 정의 한다 
 <aop:after>  After어드바이스를 정의한다.(이 어드바이스는 메서드의 예외발생 여부와 관계 없다.) 
 <aop:after-returning>  After-returning 어드바이스를 정의한다. 
 <aop:after-throwing>  After-throwing 어드바이스를 정의한다. 
 <aop:around>  Around 어드바이스를 정의한다. 
 <aop:aspect>  애스펙트를 정의한다. 
 <aop:before>  Before 어드바이스를 정의한다. 
 <aop:config>  최상위 AOP엘리번트. 대부분의 <aop:*> 엘리먼트는 이 <aop:config>안에 포함되야 한다. 
 <aop:pointcut>  포인트컷을 정의한다. 


스프링 AOP 설정 엘리먼트를 이용 예

<?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:aop="http://www.springframework.org/schema/aop"

  xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

         http://www.springframework.org/schema/aop 

         http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">



    <bean id="audience" class="chapter4.springidol.Audience" />

    

    <aop:config>

    

      <aop:aspect ref="audience">

      

        <aop:before

          method="takeSeats"

          pointcut="execution(* *.perform(..))" />


        <aop:before

          method="turnOffCellPhones"

          pointcut="execution(* *.perform(..))" />

          

        <aop:after-returning

          method="applaud"

          pointcut="execution(* *.perform(..))" />

          

        <aop:after-throwing

          method="demandRefund"

          pointcut="execution(* *.perform(..))" />

          

      </aop:aspect>

      

    </aop:config>

</beans>


대부분의 스프링 AOP설정 엘리먼트는 <aop:config>엘리먼트 내에 사용됩니다. 이 안에는 하나 이상의 어드바이저, 애스펙트, 포인트컷을 선언할 수 있습니다. 

 헌데 여기서 중복된 코드를 많이 볼수있습니다. 각 어드바이스의 pointcut 애트리뷰트들이 같은 값을 가지고 있는데요. 이는 DRY(don't repeat yourself)의 원칙 위반입니다; 이래서~ 바꾸는 방법은 밑에 소스에서 처럼 <aop:aspect>안에 <aop:pointcut> 엘리먼트를 이용해서 포인트컷에 이름을 부여하고 이를 다른 어드바이스에서 참조하는 방법입니다.

       ...

    <aop:config>

    

      <aop:aspect ref="audience">

        <aop:pointcut 

          id="performance"

          expression="execution(* *.perform(..))" />

      

        <aop:before

          method="takeSeats"

          pointcut-ref="performance" />


        <aop:before

          method="turnOffCellPhones"

          pointcut-ref="performance" />

          

        <aop:after-returning

          method="applaud"

          pointcut-ref="performance" />

          

        <aop:after-throwing

          method="demandRefund"

          pointcut-ref="performance" />

      </aop:aspect>

      

    </aop:config>

    ...


<aop:pointcut>은 같은 <aop:aspect>내부의 모든 어드바이스가 참조할 수있는 포인트컷으로 정의를 하였는데, 
이 <aop:pointcut>을 <aop:config>의 바로 하위에 정의하게 되면 다른 애스펙트에서도 이 포인트 컷을 참조할 수가 있습니다.

posted by 나는너의힘
: