2014-07-21 4 views
1

내 DAO를 부모 클래스에 트랜잭션을 구성하고 구체적인 DAO가 자신의 트랜잭션 설정을 가지고 있음을 금지 할, 그래서 스프링스에게 @Transactional를 사용AspectJ를 매칭 클래스를 주석 아니라 자식 클래스

@Transactional(/* some transactionConfig */) 
abstract class AbstractDao 
{ 
    //... 
} 

// this is ok 
class UserDao extends AbstractDao 
{ 
    //... 
} 

@Transactional (/* some other config*/) // this should be illegal 
class DocumentDao extends AbstractDao 
{ 
    //... 
} 

내가 AspectJs를 사용 내 디자인 규칙을 시행하려면 @DeclareError. followint 오류와 pointcut 선언을 사용할 때 (합법적 인) UserDao의 코드에서도 오류가 발생합니다. 이는 AbstractDao에서 Annotation을 상속받습니다.

@Pointcut("call(*.new(..)) || call(* *(..)) || set (* *) || get (* *)") 
public void doAnything() 
{ 
    // no code 
} 

@Pointcut("within(@org.springframework.transaction.annotation.Transactional *) && doAnything()") 
public void transactionalClass() 
{ 
    // only pointcut, no code 
} 

@Pointcut("within(AbstractDao+) && !within(AbstractDao)") 
public void inDao() 
{ 
    // no code 
} 

@DeclareError("transactionalMethod() && doAnything() && inDao()") 
public static final String TRANSACTIONAL_NOT_ALLOWED = 
     "@Transactional of this method is not necesarry, we have it on class level in AbstractDao"; 

@DeclareError("transactionalClass() && inDao() && doAnything()") 
public static final String ERROR = "Transactional is not allowed on method level"; 

는 explicitelty (이 예제 DocumentDao에서) 주석 만 클래스와 일치 할 수 있습니까?

답변

-1

그럼, 하위 클래스 인 AbstractDao에있는 메소드 나 클래스에서 @Transactional을 사용하는 방법을 찾고 있다고 생각합니다. 이 경우 가정하면, 다음과 같은 작동합니다 :

@Pointcut("within(AbstractDao+) && !within(AbstractDao)") 
public void inDao() 
{ 
    // no code 
} 

@Pointcut("within(@org.springframework.transaction.annotation.Transactional *)") 
public void transactionalClass() 
{ 
    // no code 
} 

@Pointcut("withinCode(@org.springframework.transaction.annotation.Transactional *.*(..))") 
public void transactionalMethod() 
{ 
    // no code 
} 

@DeclareError("(transactionalMethod() || transactionalClass()) && inDao()") 
public static final String TRANSACTIONAL_NOT_ALLOWED = "NOT ALLOWED!"; 

을 실제로 이것을 시도하지 않은, 그래서 당신은 포인트 컷의 일부 조정이 필요할 수 있습니다,하지만이 작업을해야 물건의 종류이다.


편집 : 더 간결 대답은 이것이다는 :

pointcut transactionsNotAllowed() : (within(@Transactional AbstractDao+) || 
    execution(@Transactional * AbstractDao+.*(..))) && !within(AbstractDao); 

이 당신을 위해 작동합니다. 그것은 훨씬 더 직관적 인 코드 스타일 aspectj 구문을 사용합니다.

+0

그건 내 첫 번째 생각이었습니다. transactionaClass는 명시 적 주석이있는 클래스뿐만 아니라 @Transactional 주석이있는 AbstractDao에서 상속하므로 모든 DAO 클래스에서 일치합니다. –

+0

본 적이 없습니다. transactionaClass가 모든 DAO 객체에서 일치하지만, 최종 pointcut은 DAO의 하위 클래스 만 고려되도록 제한됩니다. 그러나 어쨌든, 나는 두 번째 대답을 선호한다. –

관련 문제