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时,仍旧可以打成 ...
随机推荐
- DNS 劫持、HTTP 劫持与 DNS 污染
本文为本人的学习笔记,不保证正确. DNS 劫持 指DNS服务器被控制,查询DNS时,服务器直接返回给你它想让你看的信息.这种问题常为 ISP 所为. 由于一般的的电脑的 DNS 服务器 的配置都为自 ...
- 一些echarts的基本图形
先拿一个图形渲染过程举例 引用处 <bar ref="ARPUChart" v-if="ARPUChart" style="width:500p ...
- Sentinel系统监控Redis主从节点
author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- 构建Sentinel监控Redis的主节点架构 拓扑结构结构 拓扑环境 master ...
- 【APIO 练习题】Lock Puzzle
题意 你有一个长度为 $n$ 的字符串,你需要经过若干次操作将其变成目标串 $n'$. 一次操作:选择串 $n$ 的一个后缀,将其翻转,并放到串 $n$ 的最前面. 请你输出任意一种方案.当然,你达到 ...
- android2.2 watchdog分析
1 watchdog分析 Watchdog就是“看门狗”.其最初存在的意义是因为以前嵌入式设备上的程序经常跑飞(电磁干扰之类的),所以专门设置了一个硬件看门狗,每个一段时间,看门狗就去检查一下某个参数 ...
- 一个node.js图片上传显示小应用
文件结构如下: 实现的功能有: 可以通过浏览器使用. 当请求http://domain/start时,可以看到一个欢迎页面,页面上有一个文件上传的表单. 用户可以选择一个图片并提交表单,随后文件将被上 ...
- regression
单变量线性回归univariate linear regression 代价函数square error cost function : \(J(\theta)=\frac{1}{2m}\sum_{i ...
- jsonp 格式
jQuery(document).ready(function(){ $.ajax({ type: "get", async: false, url: "http://f ...
- 如何在win2003下安装sql2008[多次安装sql2008失败者必看]
原文发布时间为:2010-11-02 -- 来源于本人的百度文章 [由搬家工具导入] 如何在win2003下安装sql2008[多次安装sql2008失败者必看] 1. 安装win2003,升级全部补 ...
- MongoDB如何使用“”用户名和密码“”
在去年的 DB 勒索事件之后, 不少的同学开始加强 Mongodb 的安全性, 其中一种办法就是设置复杂的密码. 那么问题来了, 如果设置的密码里包含一些如 “@”, “:” 一样的特殊字符怎么办? ...