Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AspectJ匹配带注释的类,但不匹配其子类_Java_Annotations_Aop_Aspectj - Fatal编程技术网

Java AspectJ匹配带注释的类,但不匹配其子类

Java AspectJ匹配带注释的类,但不匹配其子类,java,annotations,aop,aspectj,Java,Annotations,Aop,Aspectj,我想在DAOs父类上配置事务,并禁止具体DAO具有自己的事务配置,因此我使用Springs@Transactional: 我使用AspectJs@DeclareError来强制执行我的设计规则。当我使用followint错误和切入点声明时,在合法UserDao中的代码上也会出现错误,因为它继承了AbstractDao的注释 @Pointcut("call(*.new(..)) || call(* *(..)) || set (* *) || get (* *)") public void doA

我想在DAOs父类上配置事务,并禁止具体DAO具有自己的事务配置,因此我使用Springs@Transactional:

我使用AspectJs@DeclareError来强制执行我的设计规则。当我使用followint错误和切入点声明时,在合法UserDao中的代码上也会出现错误,因为它继承了AbstractDao的注释

@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";

是否可以只匹配在本例中被显式注释的类DocumentDao?

因此,我认为您正在寻找@Transactional在AbstractDao的方法或子类中的用法。假设是这种情况,以下各项应起作用:

@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语法,我觉得更直观。

这是我的第一个想法,所有DAO类上都有位transactionaClass匹配,因为它们继承自具有@Transactional注释的AbstractDao,而不仅仅是那些具有显式注释的类。我不明白。尽管TransactionClass在所有DAO对象上都匹配,但最后的切入点受到限制,因此只考虑DAO的子类。但无论如何,我更喜欢下面的第二个答案。
pointcut transactionsNotAllowed() : (within(@Transactional AbstractDao+) || 
  execution(@Transactional * AbstractDao+.*(..))) && !within(AbstractDao);