user3629892
user3629892

Reputation: 3046

getting started with aspectj without Spring AOP

I'd like to incorporate AspectJ in my app to understand how it works. I d like not to use Spring AOP, but "pure" aspectj.

Here's what I have:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.6.11</version>
</dependency>

And:

package tmp;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoggingAspect {

    @Pointcut("execution(* *.*(..))")
    void anyMethodCall() {
    }

    @Before("anyMethodCall()")
    public void beforeMethod() {
        System.out.println("Aspect Before Method");
    }
}

When I execute my App, the message is not printed.

The way I understand it, beforeMethod should be called before any method of any class in the entire project.

I'm guessing I' forgetting something but I haven't found a good tutorial yet where it was clear to me how it works.

Where do I go form here?

Upvotes: 0

Views: 3022

Answers (2)

Tyorofurin
Tyorofurin

Reputation: 11

In my IDEA, it has already build-in the plugin of aspectj since the version of 13.The next step that you should do is change the setting of Java Compiler.

Project Setting –> Compiler –> Java Compiler

"Use compile": change to Ajc;

"Path to Ajc compiler" : aspjectjtools.jar;

If you don't have the jar, please hit http://mvnrepository.com/artifact/org.aspectj to get what you want.

Now, run your programe, it will works. Good luck!

Upvotes: 1

Frank M.
Frank M.

Reputation: 997

In Eclipse project type must be changed to aspectj project (right mouse on project->AspectJ)

You need in pom.xml

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            </plugin>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.codehaus.mojo
                                    </groupId>
                                    <artifactId>
                                        aspectj-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.7,)
                                    </versionRange>
                                    <goals>
                                        <goal>compile</goal>
                                        <goal>test-compile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

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

Change your pointcut to "!within(tmp.LoggingAspect) && execution(* .(..))" for avoid catch him self.

Upvotes: 1

Related Questions