只要简单两步即可
a.
application.properties配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
b.
DruidTool.java
/** 数据库连接池druid的配置 */
@Configuration
public class DruidTool {
public static Logger LOG = LoggerFactory.getLogger(DruidTool.class);
@Bean
public ServletRegistrationBean druidServlet(){
return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
}
@Bean
public DataSource druidDataSource(
@Value("${spring.datasource.driverClassName}") String driverClass,
@Value("${spring.datasource.url}") String url,
@Value("${spring.datasource.username}") String username,
@Value("${spring.datasource.password}") String password){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driverClass);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
try {
ds.setFilters("stat,wall");
} catch (SQLException e) {
e.printStackTrace();
LOG.error("druid err:{}", e);
}
return ds;
}
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean fb = new FilterRegistrationBean();
fb.setFilter(new WebStatFilter());
fb.addUrlPatterns("/*");
fb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return fb;
}
}