簡體   English   中英

如何在 maven 項目中使用 AspectJ for Loggin?

[英]How to use AspectJ for Loggin in a maven project?

我有一個 maven Java EE 6 項目,我在每個方法中都有一個 Logger 信息,以在控制台中顯示參數的開頭和結尾。

在某些方法中,我忘記了 make,所以我想使用 aspectJ 來管理每個調用方法的開始和結束。

我使用 Jboss EAP6 作為服務器,使用 Jboss developer Studio 作為 IDE,我在網上找到了一些教程,但它總是談論 spring 或 java aspactJ 項目。 我在我的 IDE 上安裝了插件 aspectJ,我嘗試添加一個方面,它告訴我我的 maven 項目不是一個 aspectJ 項目,所以如何解決這個問題?

這是我的 maven pom.xml

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

<repositories>
    <repository>
        <id>snapshots</id>
        <name>repo1-maven</name>
        <url>http://central.maven.org/maven2</url>
    </repository>
    <repository>
        <id>JBoss repository</id>
        <url>http://repository.jboss.org/nexus/content/groups/public/</url>
    </repository>
</repositories>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

這是我的界面:

public interface IClient {
    void addClient(ClientBean clt);
}

這是實現:

public class ClientImpl implements IClient {
    private List<ClientBean> ListOfCustomers;

    public ClientImpl() {
        setListOfCustomers(new ArrayList<ClientBean>());
    }

    public void addClient(ClientBean clt) {
        ListOfCustomers.add(clt);
    }

    public List<ClientBean> getListOfCustomers() {
        return ListOfCustomers;
    }

    public void setListOfCustomers(List<ClientBean> listOfCustomers) {
        ListOfCustomers = listOfCustomers;
    }
}

這是一個我試圖讓我的方面J的課程:

@Aspect
public class ClientAspect {
    @Pointcut("execution(* *.*(..))")
    void anyCallMethod() {}

    @Before(value = "anyCallMethod()")
    public void befor(JoinPoint joinPoint) {
        System.out.println("Before, class: "
                + joinPoint.getSignature().getDeclaringType().getSimpleName()
                + ", method: " + joinPoint.getSignature().getName());
    }

    @After(value = "anyCallMethod()")
    public void after(JoinPoint joinPoint) {
        System.out.println("After, class: "
                + joinPoint.getSignature().getDeclaringType().getSimpleName()
                + ", method: " + joinPoint.getSignature().getName());
    }
}

我有一個 man 類要測試,當我運行我的項目時,它讓我在控制台上沒有登錄

您的 POM 有幾個問題:

  • [主要] AspectJ Maven Plugin如果只在pluginManagement部分配置,而在實際plugins部分沒有實際使用,則不會應用AspectJ Maven Plugin
  • [中]您使用AspectJ Maven Plugin的過時版本 1.4,默認情況下使用 AspectJ 編譯器 1.6.11。 我建議您使用基於 AspectJ 1.8.2 的當前插件版本 1.7,但可以在插件的dependencies子部分中覆蓋以使用當前版本 1.8.4,我也建議您這樣做,因為 1.8.4與 1.8.2 相比,包含幾個修復。
  • [次要]您的項目對AspectJ 運行時的依賴應與AspectJ Maven 插件使用的版本相匹配。
  • [次要]您不需要對aspectjweaver的項目依賴,這僅在加載時編織時才需要。 對於編譯時編織,您已經使用了插件,並且在運行時您只需要aspectjrt

更新:

這是我自己的項目之一的示例配置(Java SE,沒有應用程序服務器,但它應該是等效的):

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>1.7</java.source-target.version>
    <aspectj.version>1.8.4</aspectj.version>
</properties>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>${java.source-target.version}</source>
                    <target>${java.source-target.version}</target>
                    <!-- IMPORTANT -->
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>${java.source-target.version}</source>
                    <target>${java.source-target.version}</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>${java.source-target.version}</complianceLevel>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <!-- IMPORTANT -->
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

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

暫無
暫無

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

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