我正在尝试从我周围的建议中的连接点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 {
}
}
答案 0 :(得分:2)
它从未奏效,请参阅来源code here
private class SourceLocationImpl implements SourceLocation {
...
@Override
public String getFileName() {
throw new UnsupportedOperationException();
}
@Override
public int getLine() {
throw new UnsupportedOperationException();
}
}
答案 1 :(得分: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
}
答案 2 :(得分:0)
这是Spring AOP的限制。正如 deFreitas 所示,它从未在那里实现过。解决方案是使用AspectJ via LTW而不是Spring AOP,然后它的工作原理如下:
驱动程序应用程序:
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());
}
}
BTW,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