在maven项目中使用aspectj

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插件。

  • 0
    点赞
  • 0
    评论
  • 3
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

qq_42072311

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值