How to disable Spring's JpaExceptionTranslator

2019-05-28 13:30发布

问题:

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:

  • Spring rejected the issue because it's a build issue
  • AspectJ Maven Plugin rejected the issue because the AspectJ compiler doesn't support excluding specific aspects from a library

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

回答1:

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>


回答2:

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