1

aspectJ在单独的 Maven 项目中创建了类:

@Aspect
public class AspectE {

    @Pointcut("execution(@EntryPoint * *.*(..))")
    public void defineEntryPoint() {
    }

    @Before("defineEntryPoint()")
    public void setThreadName(JoinPoint joinPoint) {
       ...
    }

    @After("defineEntryPoint()")
    public void removeThreadName(JoinPoint joinPoint) {
        ...
    }
}

然后在第二个项目中,我注释了几个方法并添加到pom.xml

    <dependency>
        <groupId>first-project</groupId>
        <artifactId>first-project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.0</version>
    </dependency>

但仍然根本看不到任何方面。我错过了一些步骤吗?我应该怎么办?

4

4 回答 4

3

你看过这个吗?

AspectJ 编译器 Maven 插件 - 用法

于 2012-09-11T10:33:11.173 回答
2

为了将代码与库正确编织,您应该在依赖项和 aspectj 编织器中声明它们:

<dependencies>
    <!-- Aspectj lib  -->
    <dependency>
        <groupId>com.my.group</groupId>
        <artifactId>my-aspect-lib</artifactId>
        <version>1.0</version>
    </dependency>

    <!-- Other dependencies -->

</dependencies>

<build>
    <!-- Specific build configuration -->

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>com.my.group</groupId>
                        <artifactId>my-aspect-lib</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
        </plugin>

        <!-- Other plugins configuration -->

    </plugins>
</build>

<!-- Other settings -->

于 2012-09-11T14:11:51.940 回答
1

你必须用代码编织方面。这可以通过两种方式完成:

加载时编织更加通用,但正确设置可能有点挑战。它在启动期间(编织发生时)消耗更多 CPU,并且还具有内存占用。很明显,编译时编织会在编译期间消耗更多的 CPU,但是您不必为每次重新启动付出代价。

于 2012-09-11T11:02:05.683 回答
0

我遇到了同样的问题......但是在我添加了这个 maven repo 之后它就可以工作了

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>
于 2016-12-10T18:39:14.083 回答