日期:2014-05-16 浏览次数:20682 次
<bean id="log4jdbcInterceptor" class="net.sf.log4jdbc.DataSourceSpyInterceptor" /> <bean id="dataSourceLog4jdbcAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list><value>log4jdbcInterceptor</value></list> </property> <property name="beanNames"> <list><value>dataSource</value></list> </property> </bean>
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class DataSourceSpyInterceptor implements MethodInterceptor { private RdbmsSpecifics rdbmsSpecifics = null; private RdbmsSpecifics getRdbmsSpecifics(Connection conn) { if(rdbmsSpecifics == null) { rdbmsSpecifics = DriverSpy.getRdbmsSpecifics(conn); } return rdbmsSpecifics; } public Object invoke(MethodInvocation invocation) throws Throwable { Object result = invocation.proceed(); if(SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()) { if(result instanceof Connection) { Connection conn = (Connection)result; return new ConnectionSpy(conn,getRdbmsSpecifics(conn)); } } return result; } }