簡體   English   中英

向Maven項目添加方面

[英]Adding aspects to maven project

我在單獨的Maven項目中創建了aspectJ類:

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

但仍然根本看不到任何方面。 我是否缺少一些步驟? 我該怎么辦?

你看這個了嗎?

AspectJ編譯器Maven插件-用法

為了正確地將代碼與庫進行編織,應在依賴項和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 -->

您必須使用代碼來編織方面。 這可以通過兩種方式完成:

加載時編織功能更全面,但正確設置可能會有些挑戰。 它在啟動過程中(編織時)消耗更多的CPU,並且還占用內存。 顯然,編譯時編織在編譯期間會消耗更多的CPU,但是您不必為每次重新啟動付出代價。

我遇到了同樣的問題...但是添加了這個Maven回購后,它就可以了

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM