简体   繁体   English

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

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

I have a maven Java EE 6 project and I have into every methods a Logger information to show in the console the beginning with parameters and the end too.我有一个 maven Java EE 6 项目,我在每个方法中都有一个 Logger 信息,以在控制台中显示参数的开头和结尾。

In some methods i forgot the make that so i want to use aspectJ to manage the beginning and the end to every called methods.在某些方法中,我忘记了 make,所以我想使用 aspectJ 来管理每个调用方法的开始和结束。

I use Jboss EAP6 as server and Jboss developper Studio as IDE and i found some tuturials on the net but always it talkes about spring or java aspactJ project.我使用 Jboss EAP6 作为服务器,使用 Jboss developer Studio 作为 IDE,我在网上找到了一些教程,但它总是谈论 spring 或 java aspactJ 项目。 I installed the pluging aspectJ on my IDE and i try to add an aspect it told me that my maven project is not an aspectJ project so how can solve that?我在我的 IDE 上安装了插件 aspectJ,我尝试添加一个方面,它告诉我我的 maven 项目不是一个 aspectJ 项目,所以如何解决这个问题?

this my maven pom.xml这是我的 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>

this my interface:这是我的界面:

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

this is the implementation:这是实现:

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;
    }
}

this is a class whitch i try to make my aspectJ:这是一个我试图让我的方面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());
    }
}

i have a man class to test and when i run my project it gives me nos log on the console我有一个 man 类要测试,当我运行我的项目时,它让我在控制台上没有登录

There are several issues with your POM:您的 POM 有几个问题:

  • [major] The AspectJ Maven Plugin will not be applied if you only configure it in the pluginManagement section, but do not actually use it in the actual plugins section. [主要] AspectJ Maven Plugin如果只在pluginManagement部分配置,而在实际plugins部分没有实际使用,则不会应用AspectJ Maven Plugin
  • [medium] You use an outdated version 1.4 of AspectJ Maven Plugin , which by default uses AspectJ compiler 1.6.11. [中]您使用AspectJ Maven Plugin的过时版本 1.4,默认情况下使用 AspectJ 编译器 1.6.11。 I recommend you to use the current plugin version 1.7 which is based on AspectJ 1.8.2, but can be overridden to use the current version 1.8.4 in the plugin's dependencies sub-section, which I also recommend you to do because 1.8.4 contains several fixes in comparison to 1.8.2.我建议您使用基于 AspectJ 1.8.2 的当前插件版本 1.7,但可以在插件的dependencies子部分中覆盖以使用当前版本 1.8.4,我也建议您这样做,因为 1.8.4与 1.8.2 相比,包含几个修复。
  • [minor] Your project's dependency on AspectJ Runtime should match the version used by the AspectJ Maven Plugin . [次要]您的项目对AspectJ 运行时的依赖应与AspectJ Maven 插件使用的版本相匹配。
  • [minor] You do not need the project dependency on aspectjweaver which is only necessary for load-time weaving. [次要]您不需要对aspectjweaver的项目依赖,这仅在加载时编织时才需要。 For compile-time weaving you already use the plugin and during runtime then you only need aspectjrt .对于编译时编织,您已经使用了插件,并且在运行时您只需要aspectjrt

Update:更新:

Here is a sample config from one of my own projects (Java SE, no application server, but it should be equivalent):这是我自己的项目之一的示例配置(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