16. Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52001744
在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使用Druid,在上一节我们新建了一个类:DruidConfiguration我在这个类进行编码:
package com.kfit.base.servlet;
import Java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
importorg.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* druid 配置.
*
* 这样的方式不需要添加注解:@ServletComponentScan
* @author Administrator
*
*/
@Configuration
public class DruidConfiguration {
/**
* 注册一个StatViewServlet
* @return
*/
@Bean
publicServletRegistrationBean DruidStatViewServle2(){
//org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
ServletRegistrationBeanservletRegistrationBean = new ServletRegistrationBean(newStatViewServlet(),"/druid2/*");
//添加初始化参数:initParams
//白名单:
servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow): 如果满足deny的话提示:Sorry, you arenot permitted to view this page.
servletRegistrationBean.addInitParameter("deny","192.168.1.73");
//登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername","admin2");
servletRegistrationBean.addInitParameter("loginPassword","123456");
//是否能够重置数据.
servletRegistrationBean.addInitParameter("resetEnable","false");
returnservletRegistrationBean;
}
/**
* 注册一个:filterRegistrationBean
* @return
*/
@Bean
publicFilterRegistrationBean druidStatFilter2(){
FilterRegistrationBeanfilterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
returnfilterRegistrationBean;
}
/**
* 注册dataSouce,这里只是一个简单的例子,只注入了部分参数,其它自行注入。
* @param driver
* @param url
* @param username
* @param password
* @param maxActive
* @return
*/
@Bean
public DataSourcedruidDataSource(@Value("${spring.datasource.driverClassName}") Stringdriver,
@Value("${spring.datasource.url}") String url,
@Value("${spring.datasource.username}")String username,
@Value("${spring.datasource.password}") String password,
@Value("${spring.datasource.maxActive}") int maxActive
) {
DruidDataSourcedruidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setMaxActive(maxActive);
System.out.println("DruidConfiguration.druidDataSource(),url="+url+",username="+username+",password="+password);
try {
druidDataSource.setFilters("stat, wall");
} catch(SQLException e) {
e.printStackTrace();
}
returndruidDataSource;
}
}
这里的区别在于加入一个方法:druidDataSource进行数据源的注入(当然这么一比较当然选择上一章节在application.properties配置的方式是比较好的,如果有特殊需求的话,也可以在这里进行注入)。
如果同时进行了编程式的注入和配置的注入,配置的就无效了。
16. Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】的更多相关文章
- 48. spring boot单元测试restfull API【从零开始学Spring Boot】
回顾并详细说明一下在在之前章节中的中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...
- (44). Spring Boot日志记录SLF4J【从零开始学Spring Boot】
在开发中打印内容,使用 System.out.println() 和 Log4j 应当是人人皆知的方法了. 其实在开发中我们不建议使用 System.out 因为大量的使用 System.out 会增 ...
- (39.4) Spring Boot Shiro权限管理【从零开始学Spring Boot】
在读此文章之前您还可能需要先了解: (39.1) Spring Boot Shiro权限管理[从零开始学Spring Boot] http://412887952-qq-com.iteye.com/b ...
- (21)Spring Boot过滤器、监听器【从零开始学Spring Boot】
Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring boo ...
- (36)Spring Boot Cache理论篇【从零开始学Spring Boot】
Spring Boot Cache理论篇 在上一篇中我们介绍了Spring Boot集成Redis的实战例子,里面使用到了Spring Cache,那么什么是Spring Cache呢,本章将会做一个 ...
- (31)Spring Boot导入XML配置【从零开始学Spring Boot】
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] Spring Boot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Co ...
- 57. Spring 自定义properties升级篇【从零开始学Spring Boot】
之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...
- 73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 针对于Spring Boot提供的注解,如果没有好好研究一下的话,那么想应用自如Spring Boot的话,还是有点困难的,所以我们这小节,说说S ...
- (39.2). Spring Boot Shiro权限管理【从零开始学Spring Boot】
(本节提供源代码,在最下面可以下载) (4). 集成Shiro 进行用户授权 在看此小节前,您可能需要先看: http://412887952-qq-com.iteye.com/blog/229973 ...
- 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】
[原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...
随机推荐
- [oldboy-django][2深入django]Form总结
1 form总结 # Form数据格式验证 - 原理: - 流程 a.写类LoginForm(Form): 字段名 = fields.xxFields() # 验证规则,本质是正则表达式(fields ...
- jquery判断元素是否存在在数组中
var myArray = new Array(); function checkRepeat(sel) { console.log("索引是:" + $.inArray(sel, ...
- col-md-push-*和col-md-offset的区别
实现方式的区别:col-md-offset-*,是利用margin-left实现的,col-md-push-*/col-md-pull-*是利用相对定位实现的. 效果的区别,col-md-offset ...
- loadrunner rtsp协议模拟
在核心网做过3年的sip消息模拟,所以rtsp消息模拟只要知道信令消息交互就非常顺利了 RTSP 实时流传输协议, 是TCP/IP协议体系中的一个应用层协议, 该协议定义了一对多应用程序如何有效地通过 ...
- Linux主机系统目录误操作权限修改为777修复方法
ECS Linux中,如果意外误操作将/目录权限批量设置,比如chmod -R 777 / ,系统中的大部分服务以及命令将无法使用,这时候可以通过系统自带的getfacl命令来拷贝和还原系统权限,修复 ...
- 【bzoj3028】食物 数论+生成函数
题目描述 明明这次又要出去旅游了,和上次不同的是,他这次要去宇宙探险! 我们暂且不讨论他有多么NC,他又幻想了他应该带一些什么东西.理所当然的,你当然要帮他计算携带N件物品的方案数. 他这次又准备带一 ...
- NBUT 1618 投放炸弹(树状数组)
[1618] 投放炸弹 时间限制: 1000 ms 内存限制: 65535 K 问题描述 我们定义一个炸弹能炸毁的地方要求曼哈顿距离小于等于某个值. 曼哈顿距离——两点在南北方向上的距离加上在东西方向 ...
- 笔记:CS231n+assignment2(作业二)(一)
第二个作业难度很高,但做(抄)完之后收获还是很大的.... 一.Fully-Connected Neural Nets 首先是对之前的神经网络的程序进行重构,目的是可以构建任意大小的全连接的neura ...
- python获取目录下文件夹名称
path = '/opt' dirs = os.listdir(path) for dir in dirs: print dir
- Flask request获取参数问题
https://www.jianshu.com/p/ecd97b1c21c1 https://blog.csdn.net/lovebyz/article/details/52244330 https: ...