Spring 3 AOP finetuning

Version 2.1 by Vladyslav Pavlov on 2018/03/25 19:09

  This page applied to GeDA v.2.0.1+, specifically module geda.spring-integration.

This short article briefly explains use of regex match and nomatch attribute of annotation driven tag.

Overview

GeDA 2.0.1 offers a way to fine tune the advices method matching by providing two new attributes. A sample configuration may look something like this:


   <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:geda="http://geda-genericdto.svn.sourceforge.net/svnroot/geda-genericdto/schema"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://geda-genericdto.svn.sourceforge.net/svnroot/geda-genericdto/schema
                           http://geda-genericdto.svn.sourceforge.net/svnroot/geda-genericdto/schema/geda-spring-integration-1.1.xsd"
>

   <geda:annotation-driven dto-factory="dtoFactory"
                           dto-adapters-registrar="adapterRegistrar"
                           use-bean-preprocessor="true"
                           pointcut-match-regex="com.inspiresoftware.lib.dto.geda.test.impl.TestService.*"/>

   <bean id="dtoFactory" class="com.inspiresoftware.lib.dto.geda.impl.DTOFactoryImpl">
       <constructor-arg>
           <map>
               <entry key="entityKey" value="com.inspiresoftware.lib.dto.geda.test.impl.DomainObjectImpl"/>
               <entry key="dtoKey"    value="com.inspiresoftware.lib.dto.geda.test.impl.ExtendedDataTransferObjectImpl"/>
               <entry key="filterKey" value="com.inspiresoftware.lib.dto.geda.test.impl.DataTransferObjectImpl"/>
           </map>
       </constructor-arg>
   </bean>

   <bean id="simpleTransferableService"
         class="com.inspiresoftware.lib.dto.geda.test.impl.TestServiceImpl"/>
   <bean id="simpleTransferableNoMatchService"
         class="com.inspiresoftware.lib.dto.geda.test.impl.TestNoMatchServiceImpl"/>

   <bean id="adapterRegistrar" class="com.inspiresoftware.lib.dto.geda.test.impl.ExposedDTOAdaptersRegistrarImpl">
       <constructor-arg>
           <map>
               <entry key="vc" value-ref="adapterValueConverter"/>
               <entry key="er" value-ref="adapterEntityRetriever"/>
               <entry key="ma" value-ref="adapterMatcher"/>
           </map>
       </constructor-arg>
   </bean>

   <bean id="adapterValueConverter" class="com.inspiresoftware.lib.dto.geda.test.impl.ValueConverterImpl"/>
   <bean id="adapterEntityRetriever" class="com.inspiresoftware.lib.dto.geda.test.impl.EntityRetrieverImpl"/>
   <bean id="adapterMatcher" class="com.inspiresoftware.lib.dto.geda.test.impl.DtoToEntityMatcherImpl"/>

</beans>

In the above example the match regex is set to:pointcut-match-regex="com.inspiresoftware.lib.dto.geda.test.impl.TestService.*"

Which means that in this particular case on service bean with id "simpleTransferableService" will be eligible to be enriched with AOP advice. The service bean with id "simpleTransferableNoMatchService" will not however even though the underlaying implementation has the same annotations that the simpleTransferableService does (see junit of the module).

  Therefore we are able to fine tune which bean we need to support DTO transfer and which ones should not.
This is especially useful in complex Spring applications when different AOP advices may be in conflict or where GeDA advice will produce adverse effect of starting transactions, say with hibernate lazy objects

Similarly we can use the "pointcut-nomatch-regex" property to exclude whatever get captures by the "pointcut-match-regex".

  "pointcut-nomatch-regex" only has effect when "pointcut-match-regex" is specified. This is by design and is a limitation imposed by the Spring AOP framework.

Here is an example of nomatch configuration:


  <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:geda="http://geda-genericdto.svn.sourceforge.net/svnroot/geda-genericdto/schema"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://geda-genericdto.svn.sourceforge.net/svnroot/geda-genericdto/schema
                           http://geda-genericdto.svn.sourceforge.net/svnroot/geda-genericdto/schema/geda-spring-integration-1.1.xsd"
>

   <geda:annotation-driven dto-factory="dtoFactory"
                           dto-adapters-registrar="adapterRegistrar"
                           use-bean-preprocessor="true"
                           pointcut-match-regex="com.inspiresoftware.lib.dto.geda.test.*"
                           pointcut-nomatch-regex="com.inspiresoftware.lib.dto.geda.test.impl.TestNoMatch.*,com.inspiresoftware.lib.dto.geda.test.TestNoMatch.*"/>

   <bean id="dtoFactory" class="com.inspiresoftware.lib.dto.geda.impl.DTOFactoryImpl">
       <constructor-arg>
           <map>
               <entry key="entityKey" value="com.inspiresoftware.lib.dto.geda.test.impl.DomainObjectImpl"/>
               <entry key="dtoKey"    value="com.inspiresoftware.lib.dto.geda.test.impl.ExtendedDataTransferObjectImpl"/>
               <entry key="filterKey" value="com.inspiresoftware.lib.dto.geda.test.impl.DataTransferObjectImpl"/>
           </map>
       </constructor-arg>
   </bean>

   <bean id="simpleTransferableService"
         class="com.inspiresoftware.lib.dto.geda.test.impl.TestServiceImpl"/>
   <bean id="simpleTransferableNoMatchService"
         class="com.inspiresoftware.lib.dto.geda.test.impl.TestNoMatchServiceImpl"/>

   <bean id="adapterRegistrar" class="com.inspiresoftware.lib.dto.geda.test.impl.ExposedDTOAdaptersRegistrarImpl">
       <constructor-arg>
           <map>
               <entry key="vc" value-ref="adapterValueConverter"/>
               <entry key="er" value-ref="adapterEntityRetriever"/>
               <entry key="ma" value-ref="adapterMatcher"/>
           </map>
       </constructor-arg>
   </bean>

   <bean id="adapterValueConverter" class="com.inspiresoftware.lib.dto.geda.test.impl.ValueConverterImpl"/>
   <bean id="adapterEntityRetriever" class="com.inspiresoftware.lib.dto.geda.test.impl.EntityRetrieverImpl"/>
   <bean id="adapterMatcher" class="com.inspiresoftware.lib.dto.geda.test.impl.DtoToEntityMatcherImpl"/>

</beans>
  Note from the example that it is possible to create a list of regex expressions by separating then with a comma.
Tags:
Created by Vladyslav Pavlov on 2018/03/20 09:04
    
GeDA - Generic DTO Assembler © Denys Pavlov 2009 - 2019
v.1.0.0