springboot配置的druid连接池参数不生效问题分析
时间: 2023-11-24 07:25:59 浏览: 94
1. 配置文件位置不正确:检查配置文件位置是否正确,是否在classpath下,是否被正确加载。
2. 配置项名称错误:检查配置项名称是否正确,是否大小写敏感。
3. 配置项值错误:检查配置项的值是否正确,如是否符合数据类型要求。
4. 配置项未生效:检查是否使用了正确的数据源,是否启用了Druid数据源。
5. 代码问题:检查代码中是否存在与配置冲突的设置,如手动设置连接池大小等。
6. 版本问题:检查使用的Spring Boot和Druid版本是否兼容,是否存在已知的bug。
7. 其它问题:如网络连接问题、数据库配置问题等。
相关问题
springboot配置druid连接池
Spring Boot 配置 Druid 连接池的步骤如下:
1. 在 pom.xml 中添加 Druid 的依赖:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
```
2. 在 application.properties 或 application.yml 中配置 Druid 的相关参数,例如:
```
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
# Druid 连接池的参数
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
```
3. 在启动类上添加 @ServletComponentScan 注解:
```
@ServletComponentScan
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
4. 在启动类中添加 Druid 的 Servlet 配置:
```
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String, String> initParams = new HashMap<>();
initParams.put("loginUsername", "admin");
initParams.put("loginPassword", "admin");
servletRegistrationBean.setInitParameters(initParams);
return servletRegistrationBean;
}
```
以上是在 Spring Boot 中配置 Druid 连接池的简单步骤
springboot整合druid连接池配置
在Spring Boot中整合Druid连接池的配置有以下几个步骤:
1. 在pom.xml文件中引入Druid的Spring Boot Starter依赖。可以使用以下代码将Druid加入到项目依赖中:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
```
这样就可以使用Druid连接池了。
2. 在application.properties或application.yml文件中配置Druid连接池的相关属性。可以配置的属性包括数据库的URL、用户名、密码以及其他一些连接池的参数,例如最大连接数、初始化连接数等。以下是一个示例配置:
```
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
```
在这个示例中,我们配置了MySQL数据库的连接信息和使用Druid连接池。
3. 如果需要监控Druid连接池的状态,可以进行Druid监控的配置。可以使用以下代码启用Druid监控:
```
@Configuration
public class DruidConfig {
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
// 设置登录用户名和密码
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "admin");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean druidFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
```
通过上述配置,我们可以在浏览器中访问`http://localhost:8080/druid`来查看Druid连接池的监控信息。
通过以上步骤,你就可以在Spring Boot中成功地整合Druid连接池并进行相关配置了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [猿创征文|使用SpringBoot整合国产数据库连接池Druid](https://blog.csdn.net/weixin_46713508/article/details/127337015)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [SpringBoot整合Druid数据库连接池的方法](https://download.csdn.net/download/weixin_38610277/12722989)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)