Druid 连接池 springboot2.5.5 启动类配置
时间: 2023-08-21 22:29:43 浏览: 114
好的,对于使用Druid连接池的Spring Boot 2.5.5应用程序,您需要在启动类中进行以下配置:
1.引入Druid依赖
在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
```
2.配置数据源
在application.properties或application.yml中添加以下配置:
```yaml
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
3.配置Druid连接池
在启动类中添加以下代码:
```java
@Configuration
public class DruidConfig {
@Bean
public ServletRegistrationBean<StatViewServlet> druidServlet() {
ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
registrationBean.addInitParameter("allow", "127.0.0.1");
registrationBean.addInitParameter("deny", "192.168.1.100");
registrationBean.addInitParameter("loginUsername", "admin");
registrationBean.addInitParameter("loginPassword", "123456");
return registrationBean;
}
@Bean
public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
FilterRegistrationBean<WebStatFilter> registrationBean = new FilterRegistrationBean<>(new WebStatFilter());
registrationBean.addUrlPatterns("/*");
registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return registrationBean;
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
}
```
以上代码中,我们配置了Druid的Servlet和Filter,并且创建了一个Druid连接池的DataSource。
注意:以上代码中的url、username和password需要根据您的实际情况进行修改。
希望这能对您有所帮助。
阅读全文