Setting pointcut AOP Spring

2

I have the setting below pointcut in 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 annotate 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();
}
    
asked by anonymous 26.01.2017 / 11:45

2 answers

2

Take a look at the designators% of%,% with% and% with% in Spring documentation . See what pointcut expressions can be merged with @target , @within and @annotation to form rules like:

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 is not possible to combine named pointcuts with XML , one of the good reasons to migrate to annotations. Another thing to think about is if you really want to have an annotation for transactional methods and no types ( && ). For me it would make much more sense to mark transactional methods and types.

Some related examples:

  • SOen: @AspectJ pointcut for all methods of a class with specific annotation
  • SOen: Pointcut for annotated methods or methods in annotated classes
  • SOen: Spring AOP pointcut expression to exclude annotations
  • 26.01.2017 / 13:44
    1

    Based on Anthony Accioly's response I came up with a solution.

    Annotation:

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

    Pointcuts declaration class:

    @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() {}
    }
    

    Configuration XML:

      <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>
    
        
    26.01.2017 / 18:52