如何使用 AspectJ(Spring) 框架记录我的方法的原始行号?我是 aop 编程的新手,所以我只想知道是否可能或如何实现?由于aop会委托(delegate)我的方法的调用过程,从而生成新的类和新的方法,所以记录的行号始终不是原始的行号。
下面是我的一些代码:
基于模式的aop配置:
<bean id="logInterceptor" class="com.fuhu.appsub.aop.LogInterceptor"></bean>
<aop:config>
<aop:aspect id="logDBAspect" ref="logInterceptor">
<aop:pointcut id="logDBPointcut" expression="execution(* com.fuhu.appsub.service..*(..)) " />
<aop:after-throwing pointcut-ref="logDBPointcut" throwing="ex" method="logDBException"/>
</aop:aspect>
</aop:config>
这是我的委托(delegate)方法:
@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
public List<Item> findByName (String name) throws Exception{
try
{
List<Item> itemList = itemRepository.findByName(name);
int i=0,j=1;
int k = j/i;
return itemList;
}
catch(Exception ex)
{
throw new Exception(ex.getMessage() + "in findByName with name=" + name + " file:" + Thread.currentThread().getStackTrace()[2].getFileName() + " line:" + Thread.currentThread().getStackTrace()[2].getLineNumber());
}
}
这是我的委托(delegate)方法:
public void logDBException( JoinPoint joinPoint, Exception ex) {
if(loggerDB.isErrorEnabled()){
loggerDB.error(ex.getMessage());
}
}
最佳答案
您正在使用 full AspectJ or just Spring's AOP ? Spring AOP is primarily based关于 Java dynamic proxies 。它通常足以完成您需要的操作,相当简单,而且根本不会干扰堆栈跟踪行号。
关于java - 如何使用 AspectJ 和 log4j 原始日志行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6659182/