我正在试验 AspectJ 的 @DeclareMixin
功能,但我不断收到此错误:
/media/thomas/data-disk/sandbox/java/aspectj-tests/src/main/java/org/myproject/aspects/MyAspect.java:7:0::0 Method 'org.myproject.aspects.MyInterface org.myproject.aspects.MyAspect.declareStuff()': factory method does not return something that implements 'org.myproject.aspects.MyInterface'
这是我的方面:
package org.myproject.aspects;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareMixin;
@Aspect
public class MyAspect
{
@DeclareMixin(value = "@org.myproject.aspects.Marker *",
interfaces = MyInterface.class)
public MyInterface declareStuff()
{
return new MyImplementation();
}
}
注释如下:
package org.myproject.aspects;
public @interface Marker
{
}
这是界面:
package org.myproject.aspects;
public interface MyInterface
{
public void doSomething();
}
实现如下:
package org.myproject.aspects;
public class MyImplementation implements MyInterface
{
@Override
public void doSomething()
{
System.out.println("Hello, world!");
}
}
这是 pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aspectj-tests</groupId>
<artifactId>aspectj-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<aspectj-version>1.7.4</aspectj-version>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
最佳答案
我发现如果我从 @DeclareMixin
注释中删除 interfaces
元素,它就可以工作。
关于java - 无法使用 @DeclareMixin 编译 AspectJ 方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21640062/