由于有很多模块,都需要和数据库进行交互,这样配置p6spy比较麻烦。

查看官网的配置信息​

谷粒商城心得二_多模块

 于是可以提取到common模块。添加2个配置类



@Configuration
public class P6SpyConfig {

/**
* P6数据源包装, 打印SQL语句
*/
@Bean
public P6DataSourceBeanPostProcessor p6DataSourceBeanPostProcessor() {
return new P6DataSourceBeanPostProcessor();
}

class P6DataSourceBeanPostProcessor implements BeanPostProcessor, PriorityOrdered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DataSource) {
P6SpyPropertyConfig.init();
return new P6DataSource((DataSource) bean);
}
return bean;
}

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}

}



public class P6SpyPropertyConfig {

private static final String PRE = "p6spy.config.";

/**
* 初始化设置
*/
public static void init(){
Properties prop = System.getProperties();
prop.put(PRE + "autoflush" , "false");
prop.put(PRE + "dateformat" , "yyyy-MM-dd HH:mm:ss");
prop.put(PRE + "appender" , "com.p6spy.engine.spy.appender.Slf4JLogger");
prop.put(PRE + "logMessageFormat" , "com.p6spy.engine.spy.appender.CustomLineFormat");
prop.put(PRE + "customLogMessageFormat" , "%(executionTime)ms | %(sqlSingleLine)");
prop.put(PRE + "databaseDialectDateFormat" , "yyyy-MM-dd");
prop.put(PRE + "databaseDialectTimestampFormat" , "yyyy-MM-dd HH:mm:ss");
prop.put(PRE + "databaseDialectBooleanFormat" , "boolean");
prop.put(PRE + "filter" , "true");
prop.put(PRE + "exclude" , "^SELECT 1");
prop.put(PRE + "excludecategories" , "info,debug,result,resultset,batch,commit,rollback");
}
}  


但是发现的码云生效,需要在各个启动类上,把这个路径扫描进去。

谷粒商城心得二_sed_02

 就会生效了。