티스토리 뷰

728x90

이전에 step1에서 코드로 만든 어드바이스를 제외한 자동 프록시 생성기(DefaultAdvisorAutoproxyCreator), 포인트 컷(AspectJExpressionPointcut), 어드바이저(DefaultPointcutAdvisor)는 모두 스프링이 직접 제공하는 클래스를 빈으로 등록하고 프로퍼티를 설정해줬다. 이 세가지 클래스를 이용해 선언하는 빈은 AOP를 적용하면 반복적으로 등장하게 된다.

 

스프링에서는 AOP를 위해 기계적으로 적용하는 빈들을 간편한 방법으로 등록할 수 있다. AOP와 관련된 태그를 정의해둔 aop 스키마를 제공한다.

더보기

네임스페이스(Namespace)는 XML 문서에서 요소, 속성 등을 구분하기 위한 식별자(identifier)이다. XML 문서에서는 각 요소나 속성이 어떤 이름을 가지는지 정의한다.

스키마(Schema)는 XML 문서의 유효성 검사(validation)를 위한 규칙 집합이다. 스키마는 문서의 구조, 데이터 타입, 값을 제한하는 등 다양한 제약 조건을 정의할 수 있다.

xmlns : XML 파일에서 사용되는 요소들의 네임스페이스(namespace)를 정의한다. 이를 통해 XML 파일에 선언된 요소들이 서로 충돌하지 않도록 구분할 수 있다.

xsi : XML Schema Instance 네임스페이스로, 스키마 기반의 데이터 유효성 검사를 수행할 때 사용된다.

<?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.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-apo-3.0.xsd">
...
</beans>

그 다음 이전에 트랜잭션 AOP 관련 빈 설정을 변경해보자. 

참고: https://sh970901.tistory.com/121

//AOP 설정을 담는 부모 태그. 필요에 따라 AspectJAdvisorAutoProxyCreator를 빈으로 등록해준다.
<aop:config>
    	//expression의 표현식을 프로퍼티로 가진 AspectJExprssionPointcut을 빈으로 등록해준다
	<aop:pointcut id="transactionPointcut"
        			expression="execution(* *..*ServiceImpl.upgrade*(..))" />
        //advice와 pointcut의 ref를 프로퍼티로 갖는 DefaultBeanFactoryPointcutAdvisor를 등록해준다
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut" />
        
</aop:config>

<aop:config>, <aop:pointcut>, <aop:advisor> 세 가지 태그를 정의해두면 그에 따라 세 개의 빈이 등록된다.

 

트랜잭션의 경우는 이미 스프링에서 편리하게 트랜잭셩 경계설정 어드바이스로 사용할 수 있도록 만들어진 TransactionInterceptor를 사용하면된다. 기능은 이전에 만들어 사용한 transactionAdvice와 거의 동일하다. 

TransactionInterceptor 타입의 어드바이스 빈과 TransactionAttribute 타입의 속성 정보도 tx 스키마의 전용 태그를 이용해 정의할 수 있다. tx 스키마의 태그는 다음과 같이 간단하게 정의할 수 있다.

<?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"
       xmlns:tx="http://www.springframework.org/schema/tx" -> tx 네임스페이스 선언
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
...

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
	<tx:attributes>
    	<tx:method name="get*" propagation="REQUIRED" read-only="true" timeout="30" />
        <tx:method name="upgrade*" propagation="REQUIRES_NEW" isolation="SERIALIZABLE" />
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

</beans>

propagation(전파속성), isolation(격리수준), read-only(읽기 전용 성능 향상), timeout 등의 옵션을 추가하여 advice를 설정할 수 있다.

 

 

 

 

 

 

 

참고 자료 - 토비의 스프링 3.1 | 저자 이일민

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함