简体   繁体   中英

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.

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.

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. 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?

this my 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:

@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

There are several issues with your 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.
  • [medium] You use an outdated version 1.4 of AspectJ Maven Plugin , which by default uses AspectJ compiler 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.
  • [minor] Your project's dependency on AspectJ Runtime should match the version used by the AspectJ Maven Plugin .
  • [minor] You do not need the project dependency on aspectjweaver which is only necessary for load-time weaving. For compile-time weaving you already use the plugin and during runtime then you only need aspectjrt .

Update:

Here is a sample config from one of my own projects (Java SE, no application server, but it should be equivalent):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM