学记:spring boot使用官网推荐以外的其他数据源druid
Posted 一步一脚印
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学记: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 boot整合shiro出现UnavailableSecurityManagerException
如何获取 Spring Boot 和 OAuth2 示例以使用默认密码授予凭据以外的其他凭据
使用@sql spring boot test从资源文件夹以外的自定义位置读取.sql文件
spring/spring boot/spring cloud书籍推荐