Javamelody, Spring & AspectJ

Javamelody is a tool for monitoring Java applications. It measures and calculates statistics of JVM state, JDBC calls, Spring, EJB or Guice managed beans’ methods execution. Javamelody provides Spring configuration (monitoring-spring.xml), which You can include in Your Spring configuration out of the box.

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:tx="http://www.springframework.org/schema/tx"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd                                       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="monitoringAdvisor" class="net.bull.javamelody.MonitoringSpringAdvisor">
        <property name="pointcut">
            <bean class="net.bull.javamelody.MonitoredWithAnnotationPointcut"></bean>
        </property>
    </bean>

    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>

    <bean id="springDataSourceBeanPostProcessor" class="net.bull.javamelody.SpringDataSourceBeanPostProcessor"></bean>
</beans>

Above configuration creates proxies on annotated classes or methods automatically. It can fail in many circumstances. E.g. if You don’t use interfaces or use aspectj with some type of code weaving. To avoid these problems you need to configure Spring monitoring in a slightly different AspectJ way:

<beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns:context="http://www.springframework.org/schema/context"        xmlns:aop="http://www.springframework.org/schema/aop"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="monitoringAdvice" pointcut-ref="allPublicFacadeOperations"></aop:advisor>
    </aop:config>

    <bean id="monitoringAdvice" class="net.bull.javamelody.MonitoringSpringInterceptor"></bean>

</beans>

3 thoughts on “Javamelody, Spring & AspectJ

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.