当前位置: 首页 > 知识库问答 >
问题:

如何在另一个项目中导入包含AspectJ方面和注释的项目

牛华皓
2023-03-14

我在将包含一些自定义方面的实用程序jar文件导入到另一个项目中遇到了一些困难。需要注意的是,我没有在这个项目中使用Spring,因为我的客户对Spring有些反感。

我已经创建了一个概念验证(下面是完整的代码示例)。当我在utility jar中运行测试运行器时,任何用AspectJ注释注释的方法都可以很好地执行它们的方面。当我在另一个项目中使用相同的jar时,这些方面被忽略了。

$> java -cp aspectjrt-1.8.2.jar;aop-util-1.0-SNAPSHOT.jar TestOne
AspectOne's aroundAdvice's body is now executed Before aspectTestMethod is called. 
Executing TestOne.aspectTestMethod() 
AspectOne's aroundAdvice's body is now executed After aspectTestMethod is called.
$>java -cp aspectjrt-1.8.2.jar;aop-util-1.0-SNAPSHOT.jar;aop-consumer-1.0-SNAPSHOT.jar Test
Test.testAspectOne
AspectTwo's aroundAdvice's body is now executed Before aspectTestMethod is called.
Test.testAspectTwo
AspectTwo's aroundAdvice's body is now executed After aspectTestMethod is called.
<?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>

        <groupId>sandbox.aop</groupId>
        <artifactId>aop-util</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.8.2</version>
            </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>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnotationOne { }
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.JoinPoint;

@Aspect
public class AspectOne {

    @Pointcut("@annotation(AnnotationOne)")
    public void annotationPointCutDefinition(){
    }

    @Pointcut("execution(* *(..))")
    public void atExecution(){}

    @Around("@annotation(AnnotationOne) && execution(* *(..))")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        Object returnObject = null;
        try {
            System.out.println("AspectOne's aroundAdvice's body is now executed Before aspectTestMethod is called.");
            returnObject = joinPoint.proceed();
        } catch (Throwable throwable) {
            throw throwable;
        }
        finally {
            System.out.println("AspectOne's aroundAdvice's body is now executed After aspectTestMethod is called.");
        }
        return returnObject;
    }

    @After("annotationPointCutDefinition() && atExecution()")
    public void printNewLine(JoinPoint pointcut){
        System.out.print("\n\r");
    }
}
public class TestOne {

    public static void main(String[] args) {
        TestOne testOne = new TestOne();
        testOne.aspectTestMethod();
    }

    @AnnotationOne
    public void aspectTestMethod(){
        System.out.println("Executing TestOne.aspectTestMethod()");
    }
}
<?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>

    <groupId>sandbox.aop</groupId>
    <artifactId>aop-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>sandbox.aop</groupId>
            <artifactId>aop-util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </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>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnotationTwo {}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectTwo {

    @Pointcut("@annotation(AnnotationTwo)")
    public void annotationPointCutDefinition(){
    }

    @Pointcut("execution(* *(..))")
    public void atExecution(){}

    @Around("@annotation(AnnotationTwo) && execution(* *(..))")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        Object returnObject = null;
        try {
            System.out.println("AspectTwo's aroundAdvice's body is now executed Before aspectTestMethod is called.");
            returnObject = joinPoint.proceed();
        } catch (Throwable throwable) {
            throw throwable;
        }
        finally {
            System.out.println("AspectTwo's aroundAdvice's body is now executed After aspectTestMethod is called.");
        }
        return returnObject;
    }

    @After("annotationPointCutDefinition() && atExecution()")
    public void printNewLine(JoinPoint pointcut){
        System.out.print("\n\r");
    }
}
public class Test {

    public static void main(String[] args) {
        Test test = new Test();
        test.testAspectOne();
        test.testAspectTwo();
    }

    @AnnotationOne
    public void testAspectOne() {
        System.out.println(Test.class.getName() + ".testAspectOne");
    }

    @AnnotationTwo
    public void testAspectTwo() {
        System.out.println(Test.class.getName() + ".testAspectTwo");
    }
}

共有1个答案

万俟穆冉
2023-03-14

您需要告诉aspectj weaver使用AspectLibraries编织库中定义的方面:

<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>
       <aspectLibraries>
            <aspectLibrary>
                <groupId>sandbox.aop</groupId>
                <artifactId>aop-util</artifactId>
            </aspectLibrary>
       </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
 类似资料:
  • 问题内容: 我有两个名为和的项目。 这两个项目都是,并且都具有一个父项目名称s。 我在项目。 我有一个具有类的它具有一个向我返回消息“ ”的方法。 我想在中导入以便Bean可以在项目中的控制器中使用。 项目中有一个项目。 因此,请告诉我如何将一个项目导入另一个项目,以便只需导入即可将first的所有bean导入另一个项目? 我不想重写所有。 问题答案:

  • 我是java的新手,我正在尝试在Intellij IDEA中的另一个项目中导入一个项目包。我想在我的第二个项目中使用LSE和LSENode。 下面是带有我试图导入的包(buarque.thiago.dataTypes)的项目- 这是我的第二个项目,我正在尝试导入第一个项目- 错误-

  • 所以我有一个多项目设置,看起来像这样

  • 我有两个包com.a.b.c和com.x.y.z。在com.a.b2.c中,我定义了这样一个组件: 在com.x.y.z中,我有一个类,我想像这样注入我的ClassA: 我需要做什么样的配置更改才能将我的类注入到我的其他类中?现在我得到了构建错误 org.springframework.beans.factory。NoSuchBeanDefinitionException:未找到依赖项的[com.

  • 我正在构建一个定制的Java库。我把我的大部分“重复”代码都保存在那里,比如文件处理、字符串处理等。每次我想使用它们时,我都必须将该类复制并粘贴到我正在进行的其他项目中。有没有办法让这个自定义库类成为“依赖项”?我在用我的智能手机。

  • 问题内容: 我正在eclipse中进行两个项目,我想将一些类从项目a导入到项目b。我该怎么办? 有没有一种方法可以在不将项目添加到构建路径的情况下进行? 问题答案: 将项目A添加到项目B的构建路径。 请按照下列步骤操作: 通过 在Eclipse中右键单击项目B的文件夹进行编辑->属性->构建路径->项目->添加。 现在添加项目A