JAVA/Practice

[Spring] .xml (설정파일)의 구조

ITs Min 2024. 2. 29.
<context:component-scan base-package="com.spring.biz" />

   <bean class="com.spring.biz.common.LogAdvice" id="logAdvice" />
   <aop:config>
      <aop:pointcut id="aPointcut" expression="execution(* com.spring.biz..*Impl.*(..))" />
      <aop:aspect ref="logAdvice">
         <aop:before pointcut-ref="aPointcut" method="printLog" />
      </aop:aspect>
   </aop:config>
   
   <bean class="com.spring.biz.common.LogAdvice02" id="logAdvice02" />
   <aop:config>
	   <aop:pointcut id="bPointcut" expression="execution(* com.spring.biz..*Impl.select*(..))" />
	   <aop:aspect ref="logAdvice02">
		   <aop:before pointcut-ref="bPointcut" method="printLog02" />
	   </aop:aspect>
   </aop:config>

 

<context:component-scan base-package="com.spring.biz" />: 이 부분은 com.spring.biz 패키지 및 하위 패키지에서 @Component, @Service, @Repository, @Controller 등과 같은 어노테이션이 지정된 클래스들을 스캔하여 스프링 빈으로 등록한다.

 

<bean class="com.spring.biz.common.LogAdvice" id="logAdvice" />: LogAdvice 클래스를 스프링 빈으로 등록하는 부분이니다. 이 빈은 AOP에서 Advice 역할을 수행한다.

 

<aop:config>: AOP 설정을 시작하는 태그이다

 

<aop:pointcut id="aPointcut" expression="execution(* com.spring.biz..*Impl.*(..))" />: aPointcut이라는 이름의 포인트컷을 정의하는 부분이다. 이 포인트컷은 com.spring.biz 패키지 및 하위 패키지에서 *Impl로 끝나는 모든 클래스의 모든 메서드를 대상으로 한다.

 

<aop:aspect ref="logAdvice">: logAdvice 빈을 참조하는 Aspect를 정의하는 부분이다. 이 Aspect는 Advice와 Pointcut을 결합하여 Advice가 실행될 지점을 정의한다.

 

<aop:before pointcut-ref="aPointcut" method="printLog" />: 이 부분은 aPointcut 포인트컷을 참조하여 해당 지점에서 Advice를 실행하기 전에 printLog 메서드를 호출하도록 설정한다. 따라서 printLog 메서드는 *Impl로 끝나는 클래스의 메서드가 호출되기 전에 실행된다.

 

<bean class="com.spring.biz.common.LogAdvice02" id="logAdvice02" />와 이후의 <aop:config> 부분은 두 번째 Advice인 LogAdvice02와 이를 적용하는 구성이다. 앞서 설명한 것과 유사하게 동작한다.


 

댓글

TOP

늦었다고 생각할 땐 너무 늦은 거다.