springboot2配置druid数据库连接池

注意配置以下的依赖:

        <!-- 引入druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency> <!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- 注意修改mysql的版本-->
<scope>8.0.12</scope>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

代码如下:

@Configuration
public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid() {
return new DruidDataSource();
} // 配置Druid的监控
// 1、配置一个管理后台的Servlet
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","localhost"); //默认就是允许所有访问
initParams.put("deny","192.168.1.103");
bean.setInitParameters(initParams);
return bean;
} // 2、配置一个web监控的filter
@Bean
public FilterRegistrationBean WebStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter()); Map<String,String> initParams = new HashMap<>();
// 配置不拦截的静态资源文件
initParams.put("exclusion","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
// 拦截所有请求
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
上一篇:spring boot:配置druid数据库连接池(开启sql防火墙/使用log4j2做异步日志/spring boot 2.3.2)


下一篇:自定义滚动条 - mCustomScrollbar