【问题标题】:How to use AspectJ for Loggin in a maven project?如何在 Maven 项目中使用 AspectJ 进行日志记录?
【发布时间】:2015-01-31 22:07:44
【问题描述】:

我有一个 Maven Java EE 6 项目,我在每个方法中都有一个 Logger 信息,以在控制台中显示参数的开头和结尾。

在某些方法中,我忘记了 make,所以我想使用 aspectJ 来管理每个调用方法的开头和结尾。

我使用 Jboss EAP6 作为服务器,使用 Jbossdevelopper 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;
    }
}

这是我试图让我的aspectJ的一个类witch:

@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 类要测试,当我运行我的项目时,它让我在控制台上没有日志

【问题讨论】:

    标签: java maven aop aspectj aspectj-maven-plugin


    【解决方案1】:

    您的 POM 存在几个问题:

    • [major] AspectJ Maven 插件 如果您只在pluginManagement 部分配置它,但在实际plugins 部分没有实际使用它,则不会应用它.
    • [medium]您使用的是过时的 1.4 版 AspectJ Maven 插件,默认情况下使用 AspectJ 编译器 1.6.11。我建议您使用基于 AspectJ 1.8.2 的当前插件版本 1.7,但可以在插件的 dependencies 子部分中覆盖以使用当前版本 1.8.4,我也建议您这样做,因为 1.8 .4 与 1.8.2 相比包含多个修复。
    • [minor]您的项目对 AspectJ Runtime 的依赖应与 AspectJ Maven 插件使用的版本相匹配。
    • [minor] 您不需要对 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>
    

    【讨论】:

    • 如果我想应用 tje Aspect Maven 插件,我该怎么做才能解决主要问题?
    • 您需要一个额外的部分&lt;plugins&gt;&lt;plugin&gt;&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;&lt;artifactId&gt;aspectj-maven-plugin&lt;/artifactId&gt;&lt;/plugin&gt;&lt;/plugins&gt;(没有插件版本)参考&lt;pluginManagement&gt;...部分中的配置版本和其他设置。我将使用我自己的一个项目中的示例来更新我的答案。我的示例解决了从主要到次要的所有问题。我希望这对你没问题。如果是这样,请接受并支持我的回答。
    • 非常感谢当我执行我的主类时,我现在有了日志,我希望它适用于我的其他战争项目,它们是分开的:接口项目、实现项目和战争项目
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 2014-12-05
    • 2011-12-26
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多