方面注释链接

Aspect annotation links

面向方面开发的新手。

即将出现两部分问题。

  1. 你们有没有包含教程和运行代码的好网站? 到目前为止,我已经看过很多教程,但代码都是零散的,没有任何东西可以拼凑起来以使其在本地运行。

  2. 我正在尝试创建一个具有方面和 aspectj class 的框架,它应该拦截所有用方面注释的方法调用。 它在我的本地项目中运行良好,但当我尝试在另一个项目中使用该方面时,它似乎不起作用。

代码示例:切面拦截器

@Aspect
public class InterceptCallAspect {
    @Around("execution(* *(@InterceptCall (*)));")
    public void record(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //Before
        System.out.println("Before");
        proceedingJoinPoint.proceed();
        System.out.println("After");
        //After
    }
}

看点

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InterceptAspectAnnotation {
}

所以当我在我的项目中注释我的测试用例时,我在正确的地方得到了系统输出。 但是当我创建我的 jar 并将其捆绑在另一个项目中时,它什么也没做。

我的 pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>InterceptCall</artifactId>
<groupId>testing</groupId>
<packaging>jar</packaging>
<version>0.0.2-SNAPSHOT</version>    
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我只能解决你的第二个问题,所以我会把第一个留给其他人。

如果您通过外部库(例如您构建的 jar)提供方面,则需要告诉 aspectj-maven-plugin 在哪里可以找到用于编织的方面。 configuration-tag 需要包含一个名为 aspectLibraries 的标签,每个 lib 要使用的 aspectLibrary-tags :

        <aspectLibraries>
           <aspectLibrary>
              <groupId>com.your.example.util</groupId>
              <artifactId>tracing-aspect</artifactId>
           </aspectLibrary>
        </aspectLibraries>