SpringBoot 使用自定义的方式整合Druid数据源(powernode document)(内含源代码)
源代码下载链接地址
:https://download.csdn.net/download/weixin_46411355/87404561
目录
- SpringBoot 使用自定义的方式整合Druid数据源(powernode document)(内含源代码)
- `源代码下载链接地址`:[https://download.csdn.net/download/weixin_46411355/87404561](https://download.csdn.net/download/weixin_46411355/87404561)
- 一、介绍
- 二、添加durid的依赖
- 三、修改application.yml配置文件
- 四、添加MyDruidProperties配置文件类
- 五、添加MyDruidAutoConfiguration自动配置类
- 六、测试访问
一、介绍
就是只导入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