2

我正在尝试从我的周围建议中的连接点 Sourcelocation 访问行号(用于记录目的)。但它给

Exception in thread "main" java.lang.UnsupportedOperationException
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl.getLine(MethodInvocationProceedingJoinPoint.java:282)

loc.getLine()在调试时得到空值。

@Around("execution (* com.logger.rms.*.*(..))")
public void logAround(ProceedingJoinPoint procJoinpoint) throws Throwable {
  StaticPart part = (procJoinpoint.getStaticPart());
  org.aspectj.lang.reflect.SourceLocation loc = part.getSourceLocation();
  int line = loc.getLine();
  System.out.println(line);

  try {
    procJoinpoint.proceed();
  } catch (Throwable e) {
    System.out.println("checking " + procJoinpoint.getSourceLocation().getWithinType().getCanonicalName());
    String str = "Aspect ConcreteTester :" + procJoinpoint.getStaticPart().getSourceLocation().getLine();
    throw new BusinessLogicException("Throwing Custom business exception from around");
  } finally {
  }
}
4

3 回答 3

3

它从来没有用过,请参阅此处的源代码

private class SourceLocationImpl implements SourceLocation {

    ...

    @Override
    public String getFileName() {
        throw new UnsupportedOperationException();
    }

    @Override
    public int getLine() {
        throw new UnsupportedOperationException();
    }
}
于 2018-02-13T22:18:33.813 回答
1

用这个

procJoinpoint.getSourceLocation().getLine()

@Around("execution (* com.logger.rms.*.*(..))")

      public Object logAround(ProceedingJoinPoint procJoinpoint) throws Throwable {

       int line= procJoinpoint.getSourceLocation().getLine();

        System.out.println(line);

    System.out.println("in around before " + procJoinpoint );
    Object result=null;
        try {
         result = procJoinpoint.proceed();

        } catch (Throwable e) {

            throw new BusinessLogicException("Throwing Custom business exception from around");

        }finally{
            }
System.out.println("in around after " + procJoinpoint+ " wtirh result is "
                + result);
     return result
        }
于 2015-04-16T14:01:50.523 回答
0

这是 Spring AOP 的一个限制。正如deFreitas所示,它从未在那里实施。解决方案是通过 LTW 而不是 Spring AOP 使用AspectJ,然后它的工作方式如下:

驱动应用:

package de.scrum_master.app;

public class Application {
  public static void main(String[] args) {
    new Application().doSomething();
    new Application().doSomethingElse();
  }

  public void doSomething() {
    System.out.println("Doing something");
  }

  private void doSomethingElse() {
    System.out.println("Doing something else");
  }
}

方面:

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.SourceLocation;

@Aspect
public class MyAspect {
  @Before("execution(* *())")
  public void interceptCalls(JoinPoint thisJoinPoint) throws Throwable {
    System.out.println(thisJoinPoint);
    SourceLocation sourceLocation = thisJoinPoint.getSourceLocation();
    System.out.println("  " + sourceLocation.getWithinType());
    System.out.println("  " + sourceLocation.getFileName());
    System.out.println("  " + sourceLocation.getLine());
  }
}

顺便说一句,getColumn()被标记为已弃用,不应使用。无论如何,它都会产生 -1 的返回值。我认为该方法首先在 AOP 上下文中毫无意义。

控制台日志:

execution(void de.scrum_master.app.Application.doSomething())
  class de.scrum_master.app.Application
  Application.java
  9
Doing something
execution(void de.scrum_master.app.Application.doSomethingElse())
  class de.scrum_master.app.Application
  Application.java
  13
Doing something else
于 2018-02-14T00:49:23.360 回答