pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.java.aspectj</groupId>
<artifactId>aspectj-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aspectj-test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--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.11,)
</versionRange>
<goals>
<goal>test-compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
其中要用到aspectj-maven-plugin插件。
aspectj文件——*.aj文件在maven项目中会有红叉,不知道什么原因,将红叉删掉就好了
编译前后的文件对比
编译前:
package com.java.aspectj;
public class Hello {
public void hello(String name) {
System.out.println(name+",hello!");
}
}
package com.java.aspectj;
public aspect HelloWorld {
/**
* 第一个*号是指返回值不限,第二个*号是指方法名不限
* 括号只是任意个数类型不限的形参
*/
before() : call(* com.java.aspectj.*.*(..)) {
System.out.println("hello前的检查,哈哈");
}
after() : call(* com.java.aspectj.*.*(..)) {
System.out.println("hello后的检查,哈哈");
}
}
package com.java.aspectj;
public class Test {
public static void main(String[] args) {
System.out.println("main ");
Hello hello = new Hello();
hello.hello("张三");
System.out.println("main ");
}
}
编译后:
package com.java.aspectj;
import java.io.PrintStream;
public class Hello
{
public void hello(String name)
{
System.out.println(name + ",hello!");
}
}
package com.java.aspectj;
import java.io.PrintStream;
import org.aspectj.lang.NoAspectBoundException;
public class HelloWorld
{
private static Throwable ajc$initFailureCause;
public static final HelloWorld ajc$perSingletonInstance;
public static boolean hasAspect()
{
return ajc$perSingletonInstance != null;
}
private static void ajc$postClinit()
{
ajc$perSingletonInstance = new HelloWorld();
}
public static HelloWorld aspectOf()
{
if (ajc$perSingletonInstance == null) {
throw new NoAspectBoundException("com_java_aspectj_HelloWorld", ajc$initFailureCause);
}
return ajc$perSingletonInstance;
}
static
{
try
{
}
catch (Throwable localThrowable)
{
ajc$initFailureCause = localThrowable;
}
}
public void ajc$before$com_java_aspectj_HelloWorld$1$4be4d41d()
{
System.out.println("hello前的检查,哈哈");
}
public void ajc$after$com_java_aspectj_HelloWorld$2$4be4d41d()
{
System.out.println("hello后的检查,哈哈");
}
}
package com.java.aspectj;
import java.io.PrintStream;
public class Test
{
public static void main(String[] args)
{
System.out.println("main ");
Hello hello = new Hello();
try
{
HelloWorld.aspectOf().ajc$before$com_java_aspectj_HelloWorld$1$4be4d41d();hello.hello("张三");
}
catch (Throwable localThrowable)
{
HelloWorld.aspectOf().ajc$after$com_java_aspectj_HelloWorld$2$4be4d41d();throw localThrowable;
}
HelloWorld.aspectOf().ajc$after$com_java_aspectj_HelloWorld$2$4be4d41d();
System.out.println("main ");
}
}
执行结果:
说明:我的eclipse是安装了AJDT插件。