How can I log original line numbers of my method with AspectJ(Spring) framework? I’m new to aop programming, so I just want to know if it’s possible or how to? Since aop will delegate the invocation process of my method, which generates new class and new method, line numbers logged are always not original ones.
Below are some of my codes:
Schema-based aop configuration:
<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>
Here’s my delegated method:
@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());
}
}
Here’s my delegating method:
public void logDBException( JoinPoint joinPoint, Exception ex) {
if(loggerDB.isErrorEnabled()){
loggerDB.error(ex.getMessage());
}
}
Are you using full AspectJ or just Spring’s AOP? Spring AOP is primarily based on Java dynamic proxies. It’s usually enough to do what you need, it’s fairly straightforward, and it doesn’t really interfere with stack trace line numbers at all.