学记:spring boot使用官网推荐以外的其他数据源druid
虽然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的更多相关文章
- spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包
下载spring http://spring.io/ 最重要是在特征下面的这段话,需要注意: All avaible features and modules are described in the ...
- Kudu安装(官网推荐的步骤)(installing build Kudu from source)
不多说,直接上干货! Kudu安装前的建议说明(博主推荐) 这是安装Kudu的另一种方法 Kudu安装(官网推荐的步骤)(installing Kudu using parcels or packag ...
- Kudu安装(官网推荐的步骤)(installing Kudu using parcels or packages)
不多说,直接上干货! Kudu安装前的建议说明(博主推荐) Kudu官网推荐的步骤: 本篇博文是installing Kudu using parcels or packages的方式. http:/ ...
- 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用
2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...
- Spring众多jar包的特点,及Spring jar包官网下载方法
下面给大家说说spring众多jar包的特点吧,无论对于初学spring的新手,还是spring高手,这篇文章都会给大家带来知识上的收获,如果你已经十分熟悉本文内容就当做一次温故知新吧.spring. ...
- 下雨天,适合学「Spring Boot」
北方的闷热,让不少小伙伴盼着下雨,前几天北京下了场大雨,杭州也紧跟这下了场雨,就在昨天原本还很闷热的天,突然就飘泼大雨了.今天也断断续续的下着小雨,一觉醒来已经是10点了.有句话说:懒惰是人的天性 ...
- Spring Boot系列:七、 实现Mybatis多数据源切换
一.引入相关maven配置 mybatis; mysql驱动:jdbc <dependency> <groupId>org.mybatis.spring.boot</g ...
- 学习Spring Boot:(二十四)多数据源配置与使用
前言 随着业务量增大,可能有些业务不是放在同一个数据库中,所以系统有需求使用多个数据库完成业务需求,我们需要配置多个数据源,从而进行操作不同数据库中数据. 正文 JdbcTemplate 多数据源 配 ...
- Spring Boot教程(三十二)多数据源配置与使用
之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...
随机推荐
- 你必须知道的Microsoft SQL Server一
不知道为什么我Win10环境下安装的Sqlserver2012,智能提示的功能基本上没有用,没办法,我还是选择安装插件SQL Prompt 5吧.下载地址:http://www.uzzf.com/so ...
- 如何解决MSI类型的Sharepoint Server2016 安装即点即用的office 2016 plus问题
前提 在sharepoint server 2016安装office 2016 plus提示如下错误: 解决方法 Ø 概念 1. 即点和即用的概念:即点即用是一种通过 Internet 安装和更新 O ...
- 在 Debian 上安装 SQL Server vNext CTP1
微软在开源 .NET Framework 之后,相继推出了跨平台的编辑器 Visual Studio Code,跨平台的 SQL Server 数据库 SQL Server vNext,Visual ...
- seL4之hello-2旅途(完成更新)
seL4之hello-2旅途 2016/11/19 13:15:38 If you like my blog, please buy me a cup of coffee. 回顾上周 seL4运行环境 ...
- AngularJS 过滤器
过滤器可以使用一个管道字符(|)添加到表达式和指令中 AngularJS 过滤器可用于转换数据: currency 格式化数字为货币格式. filter 从数组项中选择一个子集. lowercase ...
- Scala 数据类型(二)
Scala 与 Java有着相同的数据类型,下表列出了 Scala 支持的数据类型: Byte8位有符号补码整数.数值区间为 -128 到 127 Short16位有符号补码整数.数值区间为 -327 ...
- oracle触发器
类型 行级触发器: FOR EACH ROW 影响的每一行都会执行触发器 语句级出发器 默认的模式,一句话才执行一次触发器 触发器不能嵌套,不能含有事务控制语句 何时触发 Before:条件运行前 A ...
- Long类型的数据转换时间格式方法
function getDate(date) { //得到日期对象 var d=new Date(date); //得到年月日 var year =d.getFullYear(); ); var da ...
- Holographic Remoting Player
https://developer.microsoft.com/en-us/windows/holographic/holographic_remoting_player http://www.cnb ...
- Webform:Application、ViewState对象的用法
Application Application对象的作用范围是整个全局,也就是说对所有用户都有效.它在整个应用程序生命周期中都是有效的,类似于使用全局变量一样,所以可以在不同页面中对它进行存取.它和S ...