Ok, here's an example that I knocked up for another thread. It does method profiling but it's the same principle.
Code:
Profiling
Hello World!
java.lang.RuntimeException
StopWatch 'Profiling': running time (millis) = 110
-----------------------------------------
ms % Task name
-----------------------------------------
00110 100% execution(doIt)
applicationContext.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:configuration.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost</value>
</property>
<property name="username">
<value>${database.username}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="topLevelBean" class="example.TopLevelBeanImpl"/>
<bean id="profiler" class="example.MethodProfileInterceptor">
<property name="order" value="1"/>
</bean>
<aop:config>
<aop:aspect id="profilingAspect" ref="profiler">
<aop:pointcut id="serviceMethodWithReturnValue" expression="execution(void example.TopLevelBean.*(..))"/>
<aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"/>
</aop:aspect>
</aop:config>
</beans>
configuration.properties
Code:
database.username=sa
database.password=
TranactionProfileExample
Code:
package example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TranactionProfileExample
{
public static void main ( String [] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext ( "applicationContext.xml" );
TopLevelBean topLevelBean = ( TopLevelBean ) context.getBean ( "topLevelBean" );
topLevelBean.doIt ();
}
}
TopLevelBean
Code:
package example;
public interface TopLevelBean
{
void doIt ();
}
TopLevelBeanImpl
Code:
package example;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Transactional (propagation = Propagation.REQUIRED)
public class TopLevelBeanImpl implements TopLevelBean
{
public void doIt ()
{
System.out.println ( "Hello World!" );
throw new RuntimeException();
}
}
MethodProfileInterceptor
Code:
package example;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.core.Ordered;
import org.springframework.util.StopWatch;
public final class MethodProfileInterceptor implements Ordered
{
private int order;
public int getOrder ()
{
return order;
}
public void setOrder ( int order )
{
this.order = order;
}
public Object profile ( ProceedingJoinPoint call ) throws Throwable
{
System.out.println ( "Profiling" );
StopWatch clock = new StopWatch ( "Profiling" );
try
{
clock.start ( call.toShortString () );
return call.proceed ();
}
catch ( Throwable e )
{
System.out.println ( e );
throw e;
}
finally
{
clock.stop ();
System.out.println ( clock.prettyPrint () );
}
}
}