【spring-boot】spring-boot整合Druid时,编写的配置文件

package com.springboothelloworld.com.springboothelloworld.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 javax.sql.DataSource;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhuhonggen
 * @version 创建时间:2019/08/05 8:02
 * @ClassName 类名称
 * @Description 类描述
 */

@Configuration
public class DruidConfig {

    /**
     * @Auther: ZhuHonGen
     * @Date: 2019-08-05 08:05
     * @Param:
     * @return:
     * @Description: 这一步的目的是让yml文件中的Druid配置生效,默认是不生效的
     */
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid() {
        return new DruidDataSource();
    }

    //配置Druid的监控
    //配置一个管理后台的servlet
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        Map<String, String> initParams = new HashMap<>();
        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "admin");
        initParams.put("allow", ""); //允许所有
        bean.setInitParameters(initParams);
        return bean;

    }

    //配置一个监控filter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<>();
        initParams.put("exclusions", "*.js,*.css,/druid/*"); //这带有这种格式的文件不能被拦截

        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}

 

上一篇:超详细讲解SpringBoot——数据访问


下一篇:SpringBoot 配置Druid数据源及监控