spring boot 配置druid

发布时间:2020-05-26 00:23:54 作者:乾坤刀
来源:网络 阅读:1275

application.yaml

spring:
  main:
    banner-mode: console
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/test
    username: test
    password: 123456
#    driver-class-name: com.mysql.jdbc.Driver
#    druid pool customize configuration
    driverClassName: com.mysql.jdbc.Driver
    initialSize: 10
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 1 from dual
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    removeAbandoned: true
    removeAbandonedTimeout: 1800
    logAbandoned: true
    filters: stat,wall,slf4j
    logSlowSql: true
    loginUsername: test
    loginPassword: 123456
          jdbc:
    template:
      max-rows: 1000
      query-timeout: 1200
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
logging:
  level:
    root: debug

DruidConfiguration

@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidConfiguration {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
    private int initialSize;
    private int minIdle;
    private int maxActive;
    private int maxWait;
    private int timeBetweenEvictionRunsMillis;
    private int minEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean removeAbandoned;
    private int removeAbandonedTimeout;
    private boolean logAbandoned;
    private String filters;
    private boolean logSlowSql;
    private String loginUsername;
    private String loginPassword;
}

@Configuration配置

@Configuration
public class CustomizeConfig {

    private final static Logger LOGGER = LoggerFactory.getLogger(CustomizeConfig.class);

    @Autowired
    private DruidConfiguration druidConfiguration;

        @Bean
    public DataSource druidDataSource(){
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(druidConfiguration.getUrl());
        datasource.setUsername(druidConfiguration.getUsername());
        datasource.setPassword(druidConfiguration.getPassword());
        datasource.setDriverClassName(druidConfiguration.getDriverClassName());
        datasource.setInitialSize(druidConfiguration.getInitialSize());
        datasource.setMinIdle(druidConfiguration.getMinIdle());
                datasource.setMaxActive(druidConfiguration.getMaxActive());
        datasource.setMaxWait(druidConfiguration.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(druidConfiguration.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(druidConfiguration.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(druidConfiguration.getValidationQuery());
        datasource.setTestWhileIdle(druidConfiguration.isTestWhileIdle());
        datasource.setTestOnBorrow(druidConfiguration.isTestOnBorrow());
        datasource.setTestOnReturn(druidConfiguration.isTestOnReturn());
        datasource.setRemoveAbandoned(druidConfiguration.isRemoveAbandoned());
        datasource.setRemoveAbandonedTimeout(druidConfiguration.getRemoveAbandonedTimeout());
        datasource.setLogAbandoned(druidConfiguration.isLogAbandoned());
            try {
            datasource.setFilters(druidConfiguration.getFilters());
        } catch (SQLException e) {
            LOGGER.error("datasource.setFilters occur error.", e);
        }
        return datasource;
    }

      @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", druidConfiguration.getLoginUsername());
        reg.addInitParameter("loginPassword", druidConfiguration.getLoginPassword());
        reg.addInitParameter("logSlowSql", String.valueOf(druidConfiguration.isLogSlowSql()));
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }
}
推荐阅读:
  1. Spring Boot整合MyBatis学习总结
  2. 怎么在spring boot中利用DRUID实现数据源监控

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

druid boot spring boo

上一篇:IO流内容整理

下一篇:华为交换机命令行操作小结

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》