SpringBoot 使用自定义的方式整合Druid数据源(powernode document)(内含源代码)

导读:本篇文章讲解 SpringBoot 使用自定义的方式整合Druid数据源(powernode document)(内含源代码),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

SpringBoot 使用自定义的方式整合Druid数据源(powernode document)(内含源代码)

源代码下载链接地址https://download.csdn.net/download/weixin_46411355/87404561

一、介绍

就是只导入druid的依赖(数据源、servlet),然后自己写自动配置类
在这里插入图片描述

二、添加durid的依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>

三、修改application.yml配置文件

application.yml

my:
  druid:
    url: jdbc:mysql://localhost:13306/ssm_power_edu?serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    initial-size: 2
    max-active: 10
    min-idle: 3
    validation-query: select 'x'
    stat-view:
      login-username: admin
      login-password: admin
      allow:
      deny:
      url-mapping:
        - /druid/*
        - /druid2/*

四、添加MyDruidProperties配置文件类

package com.bjpowernode.springboot27datasourcedruidselfconfigurationdocument.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "my.druid")
public class MyDruidProperties {
    private String url;
    private String driverClassName;
    private String username;
    private String password;
    /**
     * 初始化链接数
     */
    private Integer initialSize;
    /**
     * 最大链接活跃数
     */
    private Integer maxActive;
    /**
     * 最小链接数
     */
    private Integer minIdle;
    /**
     * 检查的sql语句
     */
    private String validationQuery;

    private StatView statView;

    /**
     * 监控配置
     */
    @Data
    static class StatView {
        /**
         * 监控登陆用户名
         */
        private String loginUsername;
        /**
         * 监控登陆密码
         */
        private String loginPassword;
        /**
         * 白名单
         */
        private String allow;
        /**
         * 黑名单
         */
        private String deny;
        /**
         * 映射路径
         */
        private String[] urlMapping;
    }

}

五、添加MyDruidAutoConfiguration自动配置类

package com.bjpowernode.springboot27datasourcedruidselfconfigurationdocument.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

//@ConditionalOnClass(DataSource.class) // 必须要有这个类才生效
@EnableConfigurationProperties(MyDruidProperties.class) // 指定配置类
@Configuration // 配置类
public class MyDruidAutoConfiguration {
    @Autowired
    private MyDruidProperties myDruidProperties;

    /**
     * 创建数据源
     *
     * @return
     */
    @Bean(initMethod = "init", destroyMethod = "close")
    public DruidDataSource druidDataSource() {
        if (!StringUtils.hasText(myDruidProperties.getUrl())) {
            throw new IllegalArgumentException("URL 不能为空");
        }
        DruidDataSource druidDataSource = new DruidDataSource();
        // 设置参数
        druidDataSource.setUrl(myDruidProperties.getUrl());
        druidDataSource.setUsername(myDruidProperties.getUsername());
        druidDataSource.setPassword(myDruidProperties.getPassword());
        druidDataSource.setDriverClassName(myDruidProperties.getDriverClassName());
        druidDataSource.setMaxActive(myDruidProperties.getMaxActive());
        druidDataSource.setInitialSize(myDruidProperties.getInitialSize());
        druidDataSource.setMinIdle(myDruidProperties.getMinIdle());
        druidDataSource.setValidationQuery(myDruidProperties.getValidationQuery());
        return druidDataSource;
    }


    /**
     * 注册servlet
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServletServletRegistrationBean() {
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>();
        registrationBean.setServlet(statViewServlet);
        // 设置参数
        registrationBean.addInitParameter("loginUsername", myDruidProperties.getStatView().getLoginUsername());
        registrationBean.addInitParameter("loginPassword", myDruidProperties.getStatView().getLoginPassword());
        registrationBean.addInitParameter("allow", myDruidProperties.getStatView().getAllow());
        registrationBean.addInitParameter("deny", myDruidProperties.getStatView().getDeny());
        registrationBean.addUrlMappings(myDruidProperties.getStatView().getUrlMapping());
        return registrationBean;
    }
}

六、测试访问

http://localhost:8080/druid或者http://localhost:8080/druid2
在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/85494.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!