spring:
datasource:
username: root
password: vinllor
url: jdbc:mysql://127.0.0.1:3306/springboot01
driver-class-name: com.mysql.cj.jdbc.Driver
initialization-mode: always
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource getDataSource() {
return new DruidDataSource();
}
@Bean
// 配置监控
// 1. 配置一个管理后台的servlet
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,Object> map = new HashMap<>();
map.put("jmxUsername","admin");
map.put("jmxPassword","vinllor");
bean.setInitParameters(map);
return bean;
}
@Bean
// 2. 配置一个监控的filter
public FilterRegistrationBean ds(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,Object> map = new HashMap<>();
map.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(map);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
<!-- 引入druid数据源-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>