springboot添加druid步骤(两种方案)

这几天以来都是去学习如何去搭建一个项目的基本框架,然而并不是很顺利,会遇到各种各样的大小问题。今天把druid搭到我的框架里面,刚开始并不是很顺利,中途也遇到了一些问题,今天通过博客记录我所遇到的问题,也给广大老铁们分享一下。

                                                 方案一

1、添加maven依赖(注意:添加依赖要注意一下,否则druid没有sql记录)

错误的依赖(其实并非是错误的依赖,下面依赖想要得到sql记录也有处理的办法,具体可以度娘,这里就不具体说了)

运行结果并没有相关的sql记录:

正确的依赖:

运行结果有相关的sql记录:

2、将druidxian相关的source添加到yml里面

yml文件中要注意的一个地方:main:allow-bean-definition-overriding: true。在步骤一的添加依赖中,添加druid依赖是<artifactId>druid-spring-boot-starter</artifactId>,因为版本原因(我的springboot是2.1.3),和springcloud版本不兼容,导致报错:

Description:

The bean 'filterRegistrationBean', defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/stat/DruidWebStatFilterConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/aaron/config/DruidDBConfig.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

解决的办法有两个,第一种方法是降低pringboot的版本(由于我一开始就用2.1.3,为了避免出现其他报错,我就采取了第二种方法);第二种方法是,在yml文件中添加main:allow-bean-definition-overriding: true即可。

3、添加druid的config类文件

@Configuration
public class DruidDBConfig {

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "druid");
        reg.addInitParameter("loginPassword", "123456");
        reg.addInitParameter("resetEnable", "false");
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<String, String>();
        initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.setInitParameters(initParams);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

4、运行

运行springboot的启动文件,访问http://127.0.0.1:8080/druid/index.html,通过单元测试,即可在druid中看到相关的数据。

 

                                                 方案二

1、添加maven依赖(步骤如上述方案一)

2、添加druid的属性类文件(本类中通过Lombok注解处理)

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

@Data
@ConfigurationProperties(prefix = "druid")
public class Druid {
    private String url;
    private String username;
    private String password;
    private String driverClass;

    private int maxActive;
    private int minIdle;
    private int initialSize;
    private boolean testOnBorrow;
}

3、添加druid相关的配置类

@Configuration
@EnableConfigurationProperties(Druid.class)
@ConditionalOnClass(DruidDataSource.class)
@ConditionalOnProperty(prefix = "druid", name = "url")
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
public class DruidAutoConfig {
    @Autowired
    private Druid properties;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        if (properties.getInitialSize() > 0) {
            dataSource.setInitialSize(properties.getInitialSize());
        }
        if (properties.getMinIdle() > 0) {
            dataSource.setMinIdle(properties.getMinIdle());
        }
        if (properties.getMaxActive() > 0) {
            dataSource.setMaxActive(properties.getMaxActive());
        }
        dataSource.setTestOnBorrow(properties.isTestOnBorrow());
        try {
            dataSource.init();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return dataSource;
    }
}

4、在application文件中添加相关配置

spring:
  profiles:
    active: test
    #连接池配置
    druid:
      url: jdbc:mysql://localhost:3306/springboottest?characterEncoding=UTF-8@&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
      # 帐号
      username: druid
      # 密码
      password: druid
      # 初始化大小,最小,最大
      initial-size: 5
      min-idle: 1
      max-active: 100
      test-on-borrow: true

5、测试

运行springboot的启动文件,访问http://127.0.0.1:8080/druid/index.html,通过单元测试,即可在druid中看到相关的数据。

上述就是springboot整合druid的相关步骤,有疑问或者文章中有错误的地方,望老铁指正。

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值