Druid配置web.xml
1.导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
2.填写自定义配置与数据库连接配置
spring:
datasource:
username: root
password: liuqing.1998
url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
3.配置Druid,注入容器中。并实现后台监听功能(注意:自定义Druid的时候使用了log4j,所以导入log4j的依赖)
@Configuration
public class DruidConfig {
//将自定义的DURID加入容器中
@ConfigurationProperties( prefix = "spring.datasource")
@Bean
public DataSource druidDataSource(){
return new DruidDataSource();
}
//后台监控,web.xml ServletRegistrationBean
//因为springboot内置了servlet容器,所以没有web.xml 替代方法:ServletRegistrationBean
@Bean
public ServletRegistrationBean StatViewServlet(){
ServletRegistrationBean<StatViewServlet> bean =
new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
//后台需要人登录,账号密码配置
Map<String, String> initParameters = new HashMap<>();
//增加配置,登录的key是固定的,loginUsername loginPassword
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","123");
//允许谁都可以访问
initParameters.put("allow","");
bean.setInitParameters(initParameters);
return bean;
}
//filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
bean.setFilter(new WebStatFilter());
//过滤请求
Map<String, String> initParameters = new HashMap<>();
//这些东西不统计
initParameters.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParameters);
return bean;
}
}