这几天以来都是去学习如何去搭建一个项目的基本框架,然而并不是很顺利,会遇到各种各样的大小问题。今天把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的相关步骤,有疑问或者文章中有错误的地方,望老铁指正。