Druid常识看这里Druid wiki ,中文wiki 和源码中文注释太友好啦
1 从maven仓库找到druid maven 地址 mvnrepository
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2 指定datasource
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://10.10.8.27:3306/jdbc
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
3 testDatasource报错
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.apache.tomcat.jdbc.pool.DataSource' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
仔细看才发现导错包了 晕。。
import javax.sql.DataSource; 导成
import org.apache.tomcat.jdbc.pool.DataSource;
4 配置DataSource properties
springboot 对druid许多配置不自动支持,需要配置自定义配置类,class add anotation @Configration
创建DruidDataSource 加入容器
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource(){
return new DruidDataSource();
}
}
2 配置druid 监控:配置管理后台的Servlet和监控filter,在druid wiki 中例子是配置在web.xml,我们是在配置类DruidConfig编码实现,http://localhos