10springboot之集成druid

Posted 孤独时光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了10springboot之集成druid相关的知识,希望对你有一定的参考价值。

在pom.xml中添加

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.15</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>

添加配置类

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfiguration {

  private static final Logger log = LoggerFactory.getLogger(DruidConfiguration.class);

  @Bean
  public ServletRegistrationBean druidServlet() {
    log.info("init Druid Servlet Configuration ");
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
    servletRegistrationBean.setServlet(new StatViewServlet());
    servletRegistrationBean.addUrlMappings("/druid/*");
    Map<String, String> initParameters = new HashMap<String, String>();
    initParameters.put("loginUsername", "admin");// 用户名
    initParameters.put("loginPassword", "admin");// 密码
    initParameters.put("resetEnable", "false");// 禁用html页面上的“Reset All”功能
    initParameters.put("allow", ""); // IP白名单 (没有配置或者为空,则允许所有访问)
    //initParameters.put("deny", "192.168.20.38");// IP黑名单 (存在共同时,deny优先于allow)
    servletRegistrationBean.setInitParameters(initParameters);
    return servletRegistrationBean;
  }

  @Bean
  public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(new WebStatFilter());
    filterRegistrationBean.addUrlPatterns("/*");
    filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    return filterRegistrationBean;
  }

}

启动springboot,输入http://localhost:9999/druid/index.html

出现登陆框,密码和用户名都设置为admin,输入进入。

 

以上是关于10springboot之集成druid的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot入门之集成Druid

springboot集成druid连接池不支持批量更新

SpringBoot系列七:SpringBoot 集成 MyBatis事物配置及使用druid 数据源druid 监控使用

SpringBoot+SpringCloud+vue+Element开发项目——集成Druid数据源

SpringBoot随笔-SpringBoot集成Druid

SpringBoot随笔-SpringBoot集成Druid