2

I'm migrating from Spring 2.5.6 to 3.2.5. The jar spring-aspects-3.2.5 contains the new aspect JpaExceptionTranslatorAspect which translates standard JPA exceptions into Spring exceptions. It seems to be a Roo-specific aspect. This aspect gets automatically weaved into repositories (annotated with @Repository). Consequently, standard JPA exceptions are not caught anymore and the application is broken.

How can I exclude JpaExceptionTranslatorAspect from being weaved? If it can't be done, is there any other workaround? Or am I missing some piece of configuration?

I'm using AspectJ 1.7.4 and AspectJ Maven Plugin 1.4.

What I have already gathered:

However, I wonder if those pieces of information are up to date.

Hugo Wood
  • 2,140
  • 15
  • 19
  • Have you tried using @Component instead of @Repository? Simpliest way if want to remove exception translation. – Sergey Makarov Dec 17 '13 at 18:50
  • That would be a simple workaround thank you. However, there could be some other aspects that are wanted and based on the presence of @Repository. I'm not aware of all the consequences removing it could have. Still, it's an idea and I'll give it a try. – Hugo Wood Dec 18 '13 at 08:37
  • Actually JpaExceptionTranslatorAspect is weaved independently of @Repository, as mentioned in one of the links in my question. I tried nonetheless and it's true. – Hugo Wood Dec 18 '13 at 09:26

2 Answers2

10

First, upgrade aspectj-maven-plugin to 1.5 and add the complianceLevel tag in the configuration of the plugin (otherwise it will try to compile with java 1.4 compliance by default). Then you can specify the exclusion through the xmlConfigured property of the aspectj-maven-plugin. This property references a file from your local directory (i.e. where your pom.xml is)

pom.xml exemple :

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.5</version>
    <configuration>
      <source>${maven.compiler.source}</source>
      <target>${maven.compiler.target}</target>
      <complianceLevel>${maven.compiler.target}</complianceLevel>
      <xmlConfigured>myCtAspects.xml</xmlConfigured>
      <aspectLibraries>
        <aspectLibrary>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
        </aspectLibrary>
      </aspectLibraries>
      <showWeaveInfo>true</showWeaveInfo>
      <weaveMainSourceFolder>true</weaveMainSourceFolder>
      <proceedOnError>${maven.aspectj.failOnError}</proceedOnError>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>compile</goal>
        </goals>
        <phase>process-resources</phase>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
      </dependency>
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>${aspectj.version}</version>
      </dependency>
    </dependencies>
  </plugin>

Then in myCtAspects.xml file, you just have to specify all the wanted aspects explicitly, including Spring Aspects. In your case:

<?xml version="1.0"?>
<aspectj>
    <aspects>
        <!-- Spring Aspects -->
        <aspect name="org.springframework.beans.factory.aspectj.AbstractInterfaceDrivenDependencyInjectionAspect"/>
        <aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/>
        <aspect name="org.springframework.transaction.aspectj.AnnotationTransactionAspect"/>        
        <!-- Your Application Aspects -->
    </aspects>
</aspectj>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Arg, it actually was said [here](https://jira.springsource.org/browse/SPR-7649) but I missed it because AspectJ Maven Plugin did not support it at the time. Thanks. – Hugo Wood Dec 18 '13 at 12:31
  • I try and it seems to be OK : there is no JpaExceptionTranslatorAspect refence in the compiled code – Philippe Baye Dec 18 '13 at 12:39
  • Wait, it worked but it disabled all aspects ^^'. Seems like I need to declare every aspect I want to use in the XML. – Hugo Wood Dec 19 '13 at 12:43
  • You are rigth. It is necessary to list all needed aspects : spring and application aspects. I updated the example above. – Philippe Baye Jan 07 '14 at 13:32
  • This answer helped me. I know a lot about AspectJ, but `-xmlConfigured` is totally undocumented for *Ajc*. Thus, I have just created a [bug ticket](https://bugs.eclipse.org/bugs/show_bug.cgi?id=455014) for it. – kriegaex Dec 12 '14 at 12:25
0

Please try to use aop-autoproxy's include proprety with some invert regexp (something like ^((?! JpaExceptionTranslatorAspect).)*$).

Parixit
  • 3,829
  • 3
  • 37
  • 61
  • That would work if Spring was in charge of the AOP stuff. In my case it's AspectJ, with compile-time weaving. – Hugo Wood Dec 18 '13 at 12:14
  • if so you should use "exclude" configuration on you aspectj-maven plugin configuration. – Fscellos Dec 18 '13 at 12:43
  • This only works for aspects that are part of the module being compiled. It doesn't work for aspects residing in external libraries. – Hugo Wood Dec 18 '13 at 13:02