转载: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】的更多相关文章

  1. 48. spring boot单元测试restfull API【从零开始学Spring Boot】

    回顾并详细说明一下在在之前章节中的中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

  2. (44). Spring Boot日志记录SLF4J【从零开始学Spring Boot】

    在开发中打印内容,使用 System.out.println() 和 Log4j 应当是人人皆知的方法了. 其实在开发中我们不建议使用 System.out 因为大量的使用 System.out 会增 ...

  3. (39.4) Spring Boot Shiro权限管理【从零开始学Spring Boot】

    在读此文章之前您还可能需要先了解: (39.1) Spring Boot Shiro权限管理[从零开始学Spring Boot] http://412887952-qq-com.iteye.com/b ...

  4. (21)Spring Boot过滤器、监听器【从零开始学Spring Boot】

    Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring boo ...

  5. (36)Spring Boot Cache理论篇【从零开始学Spring Boot】

    Spring Boot Cache理论篇 在上一篇中我们介绍了Spring Boot集成Redis的实战例子,里面使用到了Spring Cache,那么什么是Spring Cache呢,本章将会做一个 ...

  6. (31)Spring Boot导入XML配置【从零开始学Spring Boot】

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] Spring Boot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Co ...

  7. 57. Spring 自定义properties升级篇【从零开始学Spring Boot】

    之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...

  8. 73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 针对于Spring Boot提供的注解,如果没有好好研究一下的话,那么想应用自如Spring Boot的话,还是有点困难的,所以我们这小节,说说S ...

  9. (39.2). Spring Boot Shiro权限管理【从零开始学Spring Boot】

    (本节提供源代码,在最下面可以下载) (4). 集成Shiro 进行用户授权 在看此小节前,您可能需要先看: http://412887952-qq-com.iteye.com/blog/229973 ...

  10. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

随机推荐

  1. 解决Win10 中打开VS2012 出现“ASP.NET 4.0 尚未在 Web 服务器上注册”

    系统升级为win10后,在使用vs2012打开原来的项目时,会出现“ASP.NET 4.0 尚未在 Web 服务器上注册”的问题,如图: 想到在win8.1系统下,也出现过同样的问题,就直接使用命令提 ...

  2. 2017年浙江工业大学之江学院程序设计竞赛决赛 I: qwb VS 去污棒(可持久化Trie+离线)

    问题 I: qwb VS 去污棒 时间限制: 2 Sec  内存限制: 256 MB 提交: 74  解决: 26 [提交][状态][讨论版] 题目描述 qwb表白学姐失败后,郁郁寡欢,整天坐在太阳底 ...

  3. 洛谷4438 [Hnoi2018]道路 【树形dp】

    题目 题目太长懒得打 题解 HNOI2018惊现普及+/提高? 由最长路径很短,设\(f[i][x][y]\)表示\(i\)号点到根有\(x\)条未修公路,\(y\)条未修铁路,子树所有乡村不便利值的 ...

  4. BZOJ1801 [Ahoi2009]chess 中国象棋 【dp】

    题目 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置方法,中国像棋中炮的行走方式大家应该很清楚吧. 输入格式 一行包含两个整数N,M,中间用空格分开. ...

  5. Array拼接字符串

    原文发布时间为:2011-01-12 -- 来源于本人的百度文章 [由搬家工具导入] Array拼接字符串本来就是一种投机取巧的无聊玩意,来源是IE6对字符串的+实现错误一般情况下,如果是语义性的字符 ...

  6. MVC中的过滤器/拦截器怎么写

    创建一个AuthenticateFilterAttribute(即过滤器/拦截器) 引用System.Web.Mvc; public class AuthenticateFilterAttribute ...

  7. 安装配置Vim中文帮助文档

    1.home/.vimrc是用户自己的vim配置文件,在这个配置文件中设置的配置只影响该用安装前的准备工作: 在home目录下列新建文件夹  : .vim ------------------> ...

  8. fprintf与fscanf

    #include <stdio.h> int main() { //printf("Please input the value a:\n"); 等于下一句 fprin ...

  9. 八、Ubuntu安装Tomcat和jdk

    1.解压Tomcat 和 jdk tar -zxvf apache-tomcat-8.0.28.tar.gz tar -zxvf jdk-8u60-linux-x64.gz 2.将解压后的tomcat ...

  10. react 使用antd的多选功能做一个单选与全选效果

    一个小而简单的单选全选功能,其实官网已经给出效果了,不过是我多做了些复合用法 addorupdatemodal.jsx import React from "react"; imp ...