Hello, I want to measure the execution time of ALL methods for this class using an aspect in Spring:

@Service @Transactional public class ObjectManager { @Autowired private SessionFactory sessionFactory; public Serializable insertObject(Object object) { return sessionFactory.getCurrentSession().save(object); } public void updateObject(Object object) { sessionFactory.getCurrentSession().update(object); } public void removeObject(Object object) { sessionFactory.getCurrentSession().delete(object); } } 

here is a measuring class:

 @Component public class Measure { private long minTime = Long.MAX_VALUE; private long maxTime = Long.MIN_VALUE; public Object measureTime(ProceedingJoinPoint joinPoint ) { long start = System.currentTimeMillis(); try { joinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } long end = System.currentTimeMillis(); long time = end - start; if (time > maxTime) maxTime = time; if (time < minTime) minTime = time; System.out.println(maxTime + " " + minTime); return joinPoint; } 

Here is the XML configuration:

 <aop:config> <aop:aspect id="mes" ref="measure"> <aop:pointcut id="serviceMeasure" expression="execution(* com.components.service.ObjectManager.*(..) )"/> <aop:around method="measureTime" pointcut-ref="serviceMeasure" /> </aop:aspect> </aop:config> 

I had a problem where I was very stuck because the insertObject method returns the Serializable parameter.

Help please solve this problem.

    1 answer 1

    the error was due to the fact that I returned in the measureTime () method, a variable of type ProceedingJoinPoint, but I had to object.

    that's right:

     @Component public class Measure { private long minTime = Long.MAX_VALUE; private long maxTime = Long.MIN_VALUE; public Object measureTime(ProceedingJoinPoint joinPoint ) { long start = System.currentTimeMillis(); Object output = null; try { output = joinPoint.proceed(); } catch (Throwable e) { throwable.printStackTrace(); } long end = System.currentTimeMillis(); long time = end - start; if (time > maxTime) maxTime = time; if (time < minTime) minTime = time; System.out.println(maxTime + " " + minTime); return output; }