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

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

问题描述

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

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

这是我需要的配置插件:

<groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.7</version><!-- AspectJ weaver plugin 7 适用于 java 8(1.6 版适用于 java 7)--><配置><complianceLevel>1.8</complianceLevel><来源>1.8</来源><目标>1.8</目标></配置><执行><执行><目标><目标>编译</目标><目标>测试编译</目标></目标></执行></执行></插件>

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

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

解决方案

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

经过多次头痛和数小时的挣扎后,幸运的是我可以解决这个问题.这是我所做的:

要在 Java 8 中使用 aspectj-maven-plugin,我可以配置版本 aspectj-maven-plugin 1.7(注意 aspectj-maven-plugin 1.6 适用于 Java 7).

所以,maven插件配置需要:

<插件><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.7-SNAPSHOT</version><配置><complianceLevel>1.8</complianceLevel><来源>1.8</来源><目标>1.8</目标></配置><执行><执行><目标><目标>编译</目标><目标>测试编译</目标></目标></执行></执行></插件>

顺便说一下,所需的aspectJ jars是:

<依赖><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.1</version></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.0.4.RELEASE</version></依赖><依赖><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.1</version></依赖>

我遇到的最重要的事情是安装 aspectj-maven-plugin 1.7 jar 我必须手动安装,因为这些 jar/pom 文件还没有在 maven repo 上.

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

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

下载并将其复制到我的本地存储库后,我需要在目录中创建我的自己的 aspectj-maven-plugin-1.7-SNAPSHOT.pom 文件:

.m2
epositoryorgcodehausmojoaspectj-maven-plugin1.7-SNAPSHOTaspectj-maven-plugin-1.7-SNAPSHOT.pom

我基于 1.6 版的副本,但不得不修改以下内容:

1.7-SNAPSHOT<属性><aspectjVersion>1.8.1</aspectjVersion><mavenVersion>2.2.1</mavenVersion><changesPluginVersion>2.9</changesPluginVersion></属性>

就这些,希望能帮到你.

更新:(根据 Xtreme Biker 在评论中的要求添加更多细节)

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

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

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

@Aspect公共类 NotificationAspect{...@AfterThrowing(pointcut="@annotation(com.integration.core.meta.NotifyOnFailure)", throwing="ex")public void executeOnException(JoinPoint joinPoint, ExternalApiExecutionException ex) 抛出 Throwable{...

<小时><小时><小时>

最终于 2015 年 9 月发布的官方插件

这是对官方插件版本答案的更新.为了在 AspectJ 中使用 Java 8,可以在此链接上找到官方的 aspectj maven 插件:

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

这里是maven仓库的链接:

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

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

...<依赖项>...<依赖><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.7</version></依赖>...</依赖项>...<构建><插件><插件><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.8</version><执行><执行><目标><目标>编译</目标><!-- 使用这个目标来编织你所有的主要类 --><目标>测试编译</目标><!-- 使用这个目标来编织你所有的测试类 --></目标></执行></执行></插件>...</插件><构建>...</项目>

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.

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.

This is the configuration plugin I need:

<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>

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

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

解决方案

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:

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).

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>

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>

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.

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

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
epositoryorgcodehausmojoaspectj-maven-plugin1.7-SNAPSHOTaspectj-maven-plugin-1.7-SNAPSHOT.pom

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.

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>

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
    {
    ...




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

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>

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

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