Setup pointcut AOP Spring

Asked

Viewed 224 times

2

I have the below pointcut setting on Spring 3

 <aop:config>
        <aop:pointcut id="baseDaoPointcut"      expression="execution(* br.com.infraestrutura.domain.BaseDao+.*(..))" />
        <aop:pointcut id="baseBusinessPointcut" expression="execution(* br.com.infraestrutura.domain.BaseBusiness+.*(..))" />
       .....

I would like these two pointcut to be ignored when I jot down a class with a certain Annotation, as in the example below.

@NoTransactional
public interface VendaMercadoriaBusiness extends VendaMercadoriaBaseBusiness{}

or

public interface VendaMercadoriaBusiness extends VendaMercadoriaBaseBusiness{
    @NoTransactional
    public void notificar();
}

2 answers

2


From a look at designators (Pcds) @target, @within and @annotation in spring documentation. See what pointcut expressions can be combined with &&, || and ! to form rules such as:

execution(* meuPacote.MinhaClasse+.*(..)) && !@annotation(meuPacote.NoTransactional)
execution(* meuPacote.MinhaClasse+.*(..)) && !@within(meuPacote.NoTransactional)

As expressions quickly become complex, it is worth combining named rules:

@Pointcut("execution(* br.com.infraestrutura.domain.BaseBusiness+.*(..)")
public void anyMethodInBaseBusinessOrSubtypes() {}

@Pointcut("@annotation(meuPacote.NoTransactional)")
public void nonTransactionalMethod() {}

@Pointcut("@within(meuPacote.NoTransactional)")
public void nonTransactionalClass() {}

@Pointcut("anyMethodInBaseBusinessOrSubtypes() && !nonTransactionalClass() && !nonTransactionalMethod()")
public void trasactionalBusinessMethod() {}

Unfortunately cannot match named pointcuts with XML, one of the good reasons to migrate to notes. Another detail to consider is whether you really want to have an annotation for methods and types nay transactional (@NoTransactional). For me it would make much more sense to demarcate methods and transactional types.


Some related examples:

  1. Soen: @Aspectj pointcut for all methods of a class with specific Annotation
  2. Soen: Pointcut for annotated methods or methods in annotated classes
  3. Soen: Spring AOP pointcut Expression to exclude Annotations
  • I’m trying this solution because the system has an old architecture where it defines that all classes that implement Basedao or Basebusiness are transactional, however there are some exceptions emerging where I would note the class as Notransactional is transactional methods with Transactional.

1

Based on Anthony Accioly’s answer I’ve come up with a solution.

Annotation:

@Retention(RetentionPolicy.RUNTIME)
public @interface NoTransactional {

}

Declaration class of pointcuts:

@Aspect
public class AspectTransactionConfig {


    @Pointcut("execution(* br.com.infraestrutura.domain.BaseBusiness+.*(..))")
    public void baseBusinessPointcut() {}

    @Pointcut("@annotation(br.com.webfarma.sis.application.NoTransactional)")
    public void nonTransactionalMethod() {}

    @Pointcut("@within(br.com.webfarma.sis.application.NoTransactional)")
    public void nonTransactionalClass() {}

    @Pointcut("execution(* br.com.infraestrutura.domain.BaseDao+.*(..))")
    public void baseDaoPointcut() {}

    @Pointcut("baseBusinessPointcut() && !(nonTransactionalMethod() || nonTransactionalClass())")
    public void trasactionalBaseBusinessMethod() {}
}

XML of configuration:

  <aop:config>
        <aop:advisor advice-ref="permissaoMetodoAdvice"      pointcut="br.com.webfarma.sis.application.AspectTransactionConfig.baseDaoPointcut()"                order="1" />
        <aop:advisor advice-ref="notificacaoEntidadeAdvice"  pointcut="br.com.webfarma.sis.application.AspectTransactionConfig.baseDaoPointcut()"                order="2" />
        <aop:advisor advice-ref="transactionDaoAdvice"       pointcut="br.com.webfarma.sis.application.AspectTransactionConfig.baseDaoPointcut()"      order="3" />
        <aop:advisor advice-ref="queryFilterManagerAdvice"   pointcut="br.com.webfarma.sis.application.AspectTransactionConfig.baseDaoPointcut()"                order="4" />

        <aop:advisor advice-ref="permissaoMetodoAdvice"      pointcut="br.com.webfarma.sis.application.AspectTransactionConfig.baseBusinessPointcut()"           order="1" />
        <aop:advisor advice-ref="transactionBusinessAdvice"  pointcut="br.com.webfarma.sis.application.AspectTransactionConfig.trasactionalBaseBusinessMethod()" order="2" />
    </aop:config>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.