虽然spring boot提供了4种数据源的配置,但是如果要使用其他的数据源怎么办?例如,有人就是喜欢druid可以监控的强大功能,有些人项目的需要使用c3p0,那么,我们就没办法了吗?我们就要编程式新建一个数据源了吗?不用了!spring boot 1.4.1.RELEASE为我们提供了简洁的方式使用自己想要的数据源。

  网上也有其他数据源的配置方法,但是都是编程式新建一个数据源,太繁琐了。我在这里记录一下官网的做法:

1、Configure a DataSource

官网介绍:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-configure-a-datasource

@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DruidConfiguration { @Bean
@ConfigurationProperties(prefix="spring.datasource.druid")
public DataSource dataSource(DataSourceProperties properties) {
return properties.initializeDataSourceBuilder()
// additional customizations
.build();
}
}

说明:@ConfigurationProperties(prefix="spring.datasource.druid"),前缀可以自己随意。

2、配置文件

spring:
datasource:
name: test
url: jdbc:mysql://localhost:3306/test
username: root
password: root
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
druid:
filters: stat,wall,log4j
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20

  以上步骤就配置好druid了,在验证druid的配置属性是否有成功注入的时候,可以注入DataSource,断点观察DataSource的属性validationQuery是否和你写的一样。

3、配置提示

有强迫症可以看看,这个是配置提示的功能,不是必要项。

添加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

在resources下新建META-INF,在建一个additional-spring-configuration-metadata.json文件

{
"properties": [
{
"name": "spring.datasource.druid.maxActive",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.maxActive."
},
{
"name": "spring.datasource.druid.initialSize",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.initialSize."
},
{
"name": "spring.datasource.druid.filters",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.filters."
},
{
"name": "spring.datasource.druid.maxWait",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.maxWait."
},
{
"name": "spring.datasource.druid.minIdle",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.minIdle."
},
{
"name": "spring.datasource.druid.timeBetweenEvictionRunsMillis",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.timeBetweenEvictionRunsMillis."
},
{
"name": "spring.datasource.druid.minEvictableIdleTimeMillis",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.minEvictableIdleTimeMillis."
},
{
"name": "spring.datasource.druid.validationQuery",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.validationQuery."
},
{
"name": "spring.datasource.druid.testWhileIdle",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.testWhileIdle."
},
{
"name": "spring.datasource.druid.testOnBorrow",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.testOnBorrow."
},
{
"name": "spring.datasource.druid.testOnReturn",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.testOnReturn."
},
{
"name": "spring.datasource.druid.poolPreparedStatements",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.poolPreparedStatements."
},
{
"name": "spring.datasource.druid.maxOpenPreparedStatements",
"type": "java.lang.String",
"description": "Description for spring.datasource.druid.maxOpenPreparedStatements."
}
]
}

这样在使用druid的时候IDEA就会出现提示了。

注意:如果没有出现提示,可以断点数据源,观察相应的配置就行了(大概):)

4、druid监控

网上有很多例子,这个就不多说了。

/**
* 注册一个StatViewServlet
* @return
*/
@Bean
public ServletRegistrationBean druidStatViewServle(){
//org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //添加初始化参数:initParams //白名单:
//servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
//servletRegistrationBean.addInitParameter("deny","192.168.1.73");
//登录查看信息的账号密码.
//servletRegistrationBean.addInitParameter("loginUsername","admin2");
//servletRegistrationBean.addInitParameter("loginPassword","123456");
//是否能够重置数据.
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
} /**
* 注册一个:filterRegistrationBean
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*"); //添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}

怎么使用可以看druid官网。

学记:spring boot使用官网推荐以外的其他数据源druid的更多相关文章

  1. spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包

    下载spring http://spring.io/ 最重要是在特征下面的这段话,需要注意: All avaible features and modules are described in the ...

  2. Kudu安装(官网推荐的步骤)(installing build Kudu from source)

    不多说,直接上干货! Kudu安装前的建议说明(博主推荐) 这是安装Kudu的另一种方法 Kudu安装(官网推荐的步骤)(installing Kudu using parcels or packag ...

  3. Kudu安装(官网推荐的步骤)(installing Kudu using parcels or packages)

    不多说,直接上干货! Kudu安装前的建议说明(博主推荐) Kudu官网推荐的步骤: 本篇博文是installing Kudu using parcels or packages的方式. http:/ ...

  4. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

  5. Spring众多jar包的特点,及Spring jar包官网下载方法

    下面给大家说说spring众多jar包的特点吧,无论对于初学spring的新手,还是spring高手,这篇文章都会给大家带来知识上的收获,如果你已经十分熟悉本文内容就当做一次温故知新吧.spring. ...

  6. 下雨天,适合学「Spring Boot」

      北方的闷热,让不少小伙伴盼着下雨,前几天北京下了场大雨,杭州也紧跟这下了场雨,就在昨天原本还很闷热的天,突然就飘泼大雨了.今天也断断续续的下着小雨,一觉醒来已经是10点了.有句话说:懒惰是人的天性 ...

  7. Spring Boot系列:七、 实现Mybatis多数据源切换

    一.引入相关maven配置 mybatis;  mysql驱动:jdbc <dependency> <groupId>org.mybatis.spring.boot</g ...

  8. 学习Spring Boot:(二十四)多数据源配置与使用

    前言 随着业务量增大,可能有些业务不是放在同一个数据库中,所以系统有需求使用多个数据库完成业务需求,我们需要配置多个数据源,从而进行操作不同数据库中数据. 正文 JdbcTemplate 多数据源 配 ...

  9. Spring Boot教程(三十二)多数据源配置与使用

    之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...

随机推荐

  1. 12款简化 Web 开发的 JavaScript 开发框架

    前端框架简化了开发过程中,像 Bootstrap 和 Foundation 就是前端框架的佼佼者.在这篇文章了,我们编制了一组新鲜的,实用的,可以帮助您建立高质量的 Web 应用程序的 JavaScr ...

  2. querySelector系列方法相比 getElementsBy 系列方法有什么区别?

    querySelector 和  querySelectorAll 相比下面这些方法有什么区别? getElementsByTagName getElementsByClassName getElem ...

  3. SAP GUI的配置文件

    GUI是SAP系统最常用的客户端,在一台客户机上,利用GUI可以连接多套SAP系统(连接方法参见<客户端连接配置(SAP GUI 710)>),也可以设置多个快捷方式登录(参见<用快 ...

  4. iOS 报错汇总

    1. Unknown type name 'class'; did you mean 'Class' 问题解决方法 objectice-c 工程中的类(比如 类 A)使用 C++ 文件时  A.m 文 ...

  5. [Android]使用Dagger 2依赖注入 - 图表创建的性能(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5098943.html 使用Dagger 2依赖注入 - 图表创 ...

  6. Newtonsoft.Json 自定义 解析协议

    在开发web api的时候 遇到一个要把string未赋值默认为null的情况改成默认为空字符串的需求 这种情况就需要自定义json序列话的 解析协议了 Newtonsoft.Json默认的解析协议是 ...

  7. IOS开发基础知识--碎片49

    1:iOS项目配置文件info.plist文件解析 Localization native development region本地化 Executable file可执行文件路径 Bundle id ...

  8. Sql基础

    SELECT 列名称 FROM 表名称 SELECT * FROM 表名称 SELECT DISTINCT Company FROM Orders 去重 SELECT 列名称 FROM 表名称 WHE ...

  9. 【推荐】CentOS安装Subversion-1.8.11+HTTP协议支持配置

    注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. 我们需要搭建一个自己的SVN服务器. 此外,搭建好的SVN服务器除了需要支持svn协议外,最好还需要支持HTTP协议和HTTPS协 ...

  10. x01.Tetris: 俄罗斯方块

    最强大脑有个小孩玩俄罗斯方块游戏神乎其技,那么,就写一个吧,玩玩而已. 由于逻辑简单,又作了一些简化,所以代码并不多. using System; using System.Collections.G ...