AspectJ:忽略自定义* .aj文件 [英] AspectJ: custom *.aj file is ignored

查看:165
本文介绍了AspectJ:忽略自定义* .aj文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 aspectj-maven-plugin 忽略我的 AnnotationInheritor.aj 文件?我配置错了吗?

Why aspectj-maven-plugin ignore my AnnotationInheritor.aj file? Am I configured something wrong?

我想通过自定义注释建议 ItemRepository#getById

I want to advice ItemRepository#getById with custom annotation:

@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {

    // AOP does not work, since autogenerated ItemRepositoryImpl#getById 
    // won't have @MyAnnotation annotation
    @MyAnnotation 
    public Item getById(Long id);
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation {
}

@Aspect
@Component
public class MyAspects {

    @Around("@annotation(MyAnnotation)")
    public Object execute(ProceedingJoinPoint joinPoint) {
        // This advice works correct when @MyAnnotation is placed on class, I tested. 
        // The problem is that I have to put @MyAnnotation on interface method
    }
}

Spring Data JPA使用接口和Java注释永远不会从接口继承到子类(由于JVM限制)。为了使我的建议适用于自定义注释有一点AspectJ技巧。因此,如前所述,我创建了 AnnotationInheritor.aj 文件:

Spring Data JPA use interfaces and Java annotations are never inherited from interface to subclass (due JVM limitations). To make my advice work with custom annotations there is a little AspectJ trick. So as described at previous referrence, I created AnnotationInheritor.aj file:

package com.vbakh.somepackage.aspects;

// For some reason does not work. WHY?
public aspect AnnotationInheritor { 
    declare @method : void ItemRepository+.getById() : @MyAnnotation;
}

并将以下配置添加到我的 pom.xml

And add the following configurations to my pom.xml:

<dependencies>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.9</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <!-- IMPORTANT -->
                <useIncrementalCompilation>false</useIncrementalCompilation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.9</version>
            <configuration>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <encoding>UTF-8 </encoding>
            </configuration>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.8.10</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

P.S。 有没有办法在没有* .aj文件的情况下执行相同的逻辑?具有* .java文件的方法。

推荐答案

我将您的代码复制到AspectJ项目中(没有Spring或Spring AOP)那里)为了测试它。我发现了一些问题:

I copied your code into an AspectJ project (no Spring or Spring AOP there) in order to test it. I found a few problems:


  • @Around(@ annotation(MyAnnotation))将找不到注释,因为没有完全限定的类名。

  • @Around("@annotation(MyAnnotation)") will not find the annotation because there is no fully qualified class name.

声明@method:void ItemRepository +。 getById():@ MyNnnotation; 与您的接口方法的签名项目getById(长ID)不匹配。

declare @method : void ItemRepository+.getById() : @MyAnnotation; does not match your interface method's signature Item getById(Long id).

MyAspects.execute(..)需要抛出 Throwable ,当然还有返回一些内容,例如 joinPoint.proceed()的结果。但也许这只是草率的副本和粘贴。

MyAspects.execute(..) needs to throw Throwable and of course also return something, such as the result of joinPoint.proceed(). But maybe that was just sloppy copy & paste.

修复此问题后,以下 MCVE 非常有效:

After fixing this, the following MCVE works beautifully:

帮助项目编译项目:

package de.scrum_master.app;

public class Item {}



package de.scrum_master.app;

public interface JpaRepository<P, Q> {}



package de.scrum_master.app;

import org.springframework.stereotype.Repository;

@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
  Item getById(Long id);
}



package de.scrum_master.app;

public class ItemRepositoryImpl implements ItemRepository {
  @Override
  public Item getById(Long id) {
    return new Item();
  }
}

标记注释:

package de.scrum_master.app;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation {}

驱动程序申请:

package de.scrum_master.app;

public class Application {
  public static void main(String[] args) {
    ItemRepository repository = new ItemRepositoryImpl();
    repository.getById(11L);
  }
}

方面:

万一你想知道为什么我把执行(* *(..))添加到切入点,这是因为我想排除匹配的 call()在AspectJ中可用的连接点而不是Spring AOP。

Just in case you wonder why I added execution(* *(..)) to the pointcut, this is because I wanted to exclude matching call() joinpoints which are available in AspectJ as opposed to Spring AOP.

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {
  @Around("@annotation(de.scrum_master.app.MyAnnotation) && execution(* *(..))")
  public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println(joinPoint);
    return joinPoint.proceed();
  }
}



package de.scrum_master.aspect;

import de.scrum_master.app.Item;
import de.scrum_master.app.ItemRepository;
import de.scrum_master.app.MyAnnotation;

public aspect AnnotationInheritor {
  declare @method : Item ItemRepository+.getById(Long) : @MyAnnotation;
}

控制台日志:

execution(Item de.scrum_master.app.ItemRepositoryImpl.getById(Long))

Voilà!它运作良好。

Voilà! It works nicely.

如果它对你不起作用,你有其他问题,如(但不是唯一的)

If it does not work for you like this you have other issues such as (but not exclusively)


  • 你自己提到的自动生成的 ItemRepositoryImpl#getById 。无论何时何地在构建过程中生成此内容,都需要在将方面应用于它之前存在。为了分析这一点,我需要在GitHub上 MCVE

  • the "auto-generated ItemRepositoryImpl#getById" you mentioned in passing. Whenever and wherever this is generated during the build process, it needs to exist before the aspect is applied to it. In order to analyze this I would need an MCVE on GitHub, though.

编织方面的目标代码是否与方面代码在同一个Maven模块中。如果不是,则需要更改Maven设置。

whether the target code to weave the aspect into is in the same Maven module as the aspect code. If it is not, you need to change your Maven setup.

这篇关于AspectJ:忽略自定义* .aj文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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