繁体   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