Spring + AspectJ使用aspectj-maven-plugin编织java 8 [英] Spring + AspectJ weaving for java 8 using aspectj-maven-plugin

查看:761
本文介绍了Spring + AspectJ使用aspectj-maven-plugin编织java 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的项目从java 7迁移到java 8,我遇到的问题与使用 aspectj-maven-plugin 的aspectj编织有关。

I'm migrating my project from java 7 to java 8 and the problem I have is related to aspectj weaving using aspectj-maven-plugin.

根据 Haus文档。但问题是我没有找到任何方法来使用(和找到)支持java 8的插件版本7.我看到这里插件7添加了java 8支持,但找不到使用它的方法。

I could configure successfuly the weaving using this plugin running on Java 6 and 7 according to Haus documentation. But the problem is that I haven't found any way to use (and find) plugin version 7 that supports java 8. I saw here that plugin 7 adds java 8 support but couldn't find a way to use it.

这是配置插件我需要:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version> <!-- AspectJ weaver plugin 7 is for java 8 (version 1.6 is for java 7) -->
          <configuration>
              <complianceLevel>1.8</complianceLevel>
              <source>1.8</source>
              <target>1.8</target>
          </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我确认使用1.6版的上述代码适用于Java 7,但没有运气试图使用版本1.7。

I confirmed that above code using version 1.6 works fine for Java 7, but had no luck trying to use version 1.7.

您知道如何在Java 8上运行spring + aspectj的weaver吗?

Do you know how to run the weaver for spring+aspectj running on Java 8?

推荐答案

2015年9月之前正式发布前的解决方案



之后很多头疼和许多小时都在努力对付这个,幸运的是我可以解决这个问题。这是我做的:

Solution before the official release prior to Sep 2015

After many headaches and many hours struggling against this, fortunately I could solve this problem. Here is what I did:

要使用Java 8我可以配置版本方面的 aspectj-maven-plugin maven-plugin 1.7(注意,aspectj-maven-plugin 1.6适用于Java 7)。

To use aspectj-maven-plugin with Java 8 I could configure version aspectj-maven-plugin 1.7 (Note that aspectj-maven-plugin 1.6 works for Java 7).

因此,maven插件配置必须是:

So, the maven plugin configuration needs to be:

<!-- AspectJ configuration -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7-SNAPSHOT</version>
    <configuration>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

顺便说一句,所需的aspectJ罐子是:

By the way, the aspectJ jars needed are:

<!-- Spring AOP + AspectJ -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.0.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.1</version>
</dependency>

我遇到的最重要的事情是安装aspectj-maven-plugin 1.7 jar I必须手动完成,因为这些jar / pom文件尚未在maven repo上。

The most important thing I've struggled was that to install the aspectj-maven-plugin 1.7 jar I had to do it manually since these jar/pom files aren't on maven repo yet.

更新:因此,jar文件可以是从 Haus Jira 链接下载(查看附件部分)。如果Haus不再可用,你可以从我的github下载:

Update: So, the jar file can be downloaded from Haus Jira link (look at the Attachment section). If Haus is not available anymore you can download it from my github:

https://github.com/fedepia/aspectj-maven-plugin-1.7

下载后将其复制到我当地的仓库我需要在目录中创建拥有 aspectj-maven-plugin-1.7-SNAPSHOT.pom 文件:

After download it and copy it to my local repo I needed to create my own aspectj-maven-plugin-1.7-SNAPSHOT.pom file within the directory:

.m2\repository\org\codehaus\mojo\aspectj-maven-plugin\1.7-SNAPSHOT\aspectj-maven-plugin-1.7-SNAPSHOT.pom

我基于版本1.6的副本但是有修改以下内容:

I based on a copy from version 1.6 but had to modify the following content:

<version>1.7-SNAPSHOT</version>

<properties>
    <aspectjVersion>1.8.1</aspectjVersion>
    <mavenVersion>2.2.1</mavenVersion>
    <changesPluginVersion>2.9</changesPluginVersion>
</properties>

这就是你要去的地方,希望能提供帮助。

That's all here you go, hope to help.

更新:(在评论中添加了Xtreme Biker提供的更多详细信息)

Update: (adding more details as Xtreme Biker asked in the comments)

在我的上下文配置中,我有:

In my context configuration I have:

<aop:aspectj-autoproxy /> 

<bean id="notificationAspect" class="com.integration.core.aspect.NotificationAspect" factory-method="aspectOf" scope="singleton"></bean>

对于我的java方面,我使用:

For my java aspect I use:

@Aspect
public class NotificationAspect
{
   ...
   @AfterThrowing(pointcut="@annotation(com.integration.core.meta.NotifyOnFailure)", throwing="ex")
   public void executeOnException(JoinPoint joinPoint, ExternalApiExecutionException ex) throws Throwable
    {
    ...












最后官方插件自2015年9月发布



这是官方插件版本的答案更新。为了将Java 8与AspectJ一起使用,官方aspectj maven插件可以在以下链接中找到:




Finally official plugin released since Sep 2015

This is an update to the answer with the official plugin release. In order to use Java 8 with AspectJ, the official aspectj maven plugin can be found on this link:

http://www.mojohaus.org/aspectj-maven-plugin/usage.html

以下是maven存储库的链接:

Here is the link to maven repository:

http://mvnrepository.com/artifact/org.codehaus.mojo/aspectj-maven-plugin/1.8

正如文件所述使用它的代码是:

As the documentation stated the code to use it is:

<project>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.7</version>
    </dependency>
    ...
  </dependencies>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  <build>
  ...
</project>

这篇关于Spring + AspectJ使用aspectj-maven-plugin编织java 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆