Druid提供了Spring和Jdbc的关联监控。

配置spring

com.alibaba.druid.support.spring.stat.DruidStatInterceptor是一个标准的Spring MethodInterceptor。可以灵活进行AOP配置。

Spring AOP的配置文档: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop-api.html

按类型拦截配置

  1. <bean id="druid-stat-interceptor"
  2. class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
  3. </bean>
  4. <bean id="druid-type-proxyCreator" class="com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator">
  5. <!-- 所有ABCInterface的派生类被拦截监控 -->
  6. <property name="targetBeanType" value="xxxx.ABCInterface" />
  7. <property name="interceptorNames">
  8. <list>
  9. <value>druid-stat-interceptor</value>
  10. </list>
  11. </property>
  12. </bean>

方法名正则匹配拦截配置

  1. <bean id="druid-stat-interceptor"
  2. class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
  3. </bean>
  4. <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
  5. scope="prototype">
  6. <property name="patterns">
  7. <list>
  8. <value>com.mycompany.service.*</value>
  9. <value>com.mycompany.dao.*</value>
  10. </list>
  11. </property>
  12. </bean>
  13. <aop:config>
  14. <aop:advisor advice-ref="druid-stat-interceptor"
  15. pointcut-ref="druid-stat-pointcut" />
  16. </aop:config>

有些情况下,可能你需要配置proxy-target-class,例如:

  1. <aop:config proxy-target-class="true">
  2. <aop:advisor advice-ref="druid-stat-interceptor"
  3. pointcut-ref="druid-stat-pointcut" />
  4. </aop:config>

按照BeanId来拦截配置

  1. <bean id="druid-stat-interceptor"
  2. class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
  3. </bean>
  4. <bean
  5. class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  6. <property name="proxyTargetClass" value="true" />
  7. <property name="beanNames">
  8. <list>
  9. <!-- 这里配置需要拦截的bean id列表 -->
  10. <value>xxx-dao</value>
  11. <value>xxx-service</value>
  12. </list>
  13. </property>
  14. <property name="interceptorNames">
  15. <list>
  16. <value>druid-stat-interceptor</value>
  17. </list>
  18. </property>
  19. </bean>