내 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
에서) 주석 만 클래스와 일치 할 수 있습니까?
그건 내 첫 번째 생각이었습니다. transactionaClass는 명시 적 주석이있는 클래스뿐만 아니라 @Transactional 주석이있는 AbstractDao에서 상속하므로 모든 DAO 클래스에서 일치합니다. –
본 적이 없습니다. transactionaClass가 모든 DAO 객체에서 일치하지만, 최종 pointcut은 DAO의 하위 클래스 만 고려되도록 제한됩니다. 그러나 어쨌든, 나는 두 번째 대답을 선호한다. –