zoukankan      html  css  js  c++  java
  • springboot集成druid数据源

    package com.book.app.utils;

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

    import javax.sql.DataSource;

    import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
    import org.springframework.boot.bind.RelaxedPropertyResolver;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.EnvironmentAware;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;

    import com.alibaba.druid.pool.DruidDataSource;
    import com.alibaba.druid.support.http.StatViewServlet;
    import com.alibaba.druid.support.http.WebStatFilter;
    import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
    @Configuration
    public class DruidDataSourceConfig implements EnvironmentAware {

    private RelaxedPropertyResolver propertyResolver;
    /**
    * 功能:可以看druid的一些信息,不看可以不配
    */
    public void setEnvironment(Environment env) {
    this.propertyResolver = new RelaxedPropertyResolver(env,"spring.datasource.");
    }
    @Bean
    public DataSource dataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(propertyResolver.getProperty("url"));
    dataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
    dataSource.setPassword(propertyResolver.getProperty("password"));
    dataSource.setUsername(propertyResolver.getProperty("username"));
    try {
    dataSource.setFilters("stat,wall");
    } catch (Exception e) {
    e.printStackTrace();
    }
    return dataSource;
    }
    @Bean
    public ServletRegistrationBean druidServlet() {
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
    servletRegistrationBean.setServlet(new StatViewServlet());
    servletRegistrationBean.addUrlMappings("/druid/*");
    Map<String, String> initParameters = new HashMap<String,String>();
    initParameters.put("resetEnable","false");
    initParameters.put("allow", "127.0.0.1");
    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,*.bmp,*.png,*.css");
    return filterRegistrationBean;
    }
    @Bean
    public DruidStatInterceptor druidStatInterceptor() {
    DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();
    return druidStatInterceptor;
    }

    public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
    BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
    beanNameAutoProxyCreator.setProxyTargetClass(true);
    beanNameAutoProxyCreator.setInterceptorNames("druid-stat-interceptor");
    return beanNameAutoProxyCreator;
    }

    }

  • 相关阅读:
    c++调用lua
    HTTP实现长连接(TTP1.1和HTTP1.0相比较而言,最大的区别就是增加了持久连接支持Connection: keep-alive)
    C++: std::string 与 Unicode 如何结合?
    统计一下你写过多少代码
    解读jQuery中extend函数
    C#如何通过SOCKET的方式获取HTTPONLY COOKIE
    Java进阶代码
    SQLSERVER聚集索引与非聚集索引的再次研究(上)
    c,c++函数返回多个值的方法
    COM思想的背后
  • 原文地址:https://www.cnblogs.com/manager222/p/10077372.html
Copyright © 2011-2022 走看看