AspectJ插件构建良好,但在运行时注释不起作用

时间:2018-12-20 13:22:35

标签: java annotations runtime aspectj aspectj-maven-plugin

我正在使用AspectJ Maven插件来构建我的项目并使用AspectLibrary(这是一个在其中定义了我的方面的jar)。

这是我要使用的方面

@Around("execution(* *(..))&&@annotation(com.cisco.commerce.pricing.lp.commons.util.annotations.TimeMe)")
public Object timeMeAroundAspect(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {// NOSONAR
    Timer timer = Timer.instance().start();
    MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
    Method method = signature.getMethod();
    TimeMe timeMeAnnotation = method.getAnnotation(TimeMe.class);
    String name = timeMeAnnotation.name();
    boolean log = timeMeAnnotation.log();
    boolean addToMetrics = timeMeAnnotation.addToMetrics();
    Object response = null;
    try {
        response = proceedingJoinPoint.proceed();
    } finally {
        try {
            Long timeTaken = timer.timeTaken();
            if (log) {
                LOGGER.info("MethodName: {} Time taken: {}", name, timeTaken);
            }
            if (addToMetrics) {
                ExecutionDetailsUtil.addMethodExecutionTime(name, timeTaken);
            }
        } catch (Exception e) {
            LOGGER.warn("Exception while trying to log time", e);
        }
    }
    return response;
}

此代码在jar文件中,我将其用作pom中的AspectLibrary

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <encoding>UTF-8</encoding>
                <source>${java.source-target.version}</source>
                <target>${java.source-target.version}</target>
                <Xlint>ignore</Xlint>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>it.cvc.ciscocommerce.lps.lp-commons</groupId>
                        <artifactId>lp-commons</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <complianceLevel>${java.source-target.version}</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>

下面是我的注释定义

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

  public String name();

  public boolean log() default true;

  public boolean addToMetrics() default true;

}

这是我尝试使用此批注的代码段(在使用上述jar作为依赖项的不同代码库中)

@TimeMe(name = "classifyLine")
private void classifyLine(PricingObject pricingObject, 
   PricingLineObject pricingLineObject, LineTypes lineTypes) {
//logic
}

我的构建运行良好,并在MAVEN控制台中打印了以下内容

  

[INFO]联接点'方法执行(无效com.cisco.pricing.lps.main.ListPriceService.classifyLine(com.cisco.pricing.lps.bean.PricingObject,com.cisco.pricing.lps.bean。 “ com.cisco.pricing.lps.main.ListPriceService”类型(ListPriceService.java:235)中的PricingLineObject,com.cisco.pricing.lps.dto.LineTypes))的建议来自“ com.cisco.commerce”的建议。 Price.lp.commons.util.logging.LoggingAspectDefiner'(lp-commons-2019.03.01-SNAPSHOT.jar!LoggingAspectDefiner.class(来自LoggingAspectDefiner.java))

我分解了war文件,并查看了生成的类文件。我为使用注释的Java文件生成了以下AjcClosure1类。

public class ListPriceService$AjcClosure1 extends AroundClosure {

  public Object run(Object[] paramArrayOfObject) {
    Object[] arrayOfObject = this.state;
    ListPriceService.classifyLine_aroundBody0((ListPriceService) 
    arrayOfObject[0],
    (PricingObject)arrayOfObject[1],
    (PricingLineObject)arrayOfObject[2], (LineTypes)arrayOfObject[3], 
    (JoinPoint)arrayOfObject[4]);return null;
  }

  public ListPriceService$AjcClosure1(Object[] paramArrayOfObject)
  {
     super(paramArrayOfObject);
  }
}

在使用注释的Java类文件中,没有看到对classifyLine方法的更改。

但是,当我运行我的应用程序时,注释不起作用。它不执行我在jar中定义的Aspect。

我不知道为什么。我的模式不匹配吗?它可以在Spring应用程序中匹配并正常工作,但不能在非Spring应用程序中运行。

0 个答案:

没有答案