在上一节使用是配置文件的方式进行使用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;

import org.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

       public ServletRegistrationBean DruidStatViewServle2(){

              //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.

              ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid2/*");

              //添加初始化参数: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 druidStatFilter2(){

              FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());

              //添加过滤规则.

              filterRegistrationBean.addUrlPatterns("/*");

              //添加不需要忽略的格式信息.

              filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");

              return filterRegistrationBean;

       }

       /**

        * 注册dataSouce,这里作为一个例子,只注入了部分参数信息,其它的参数自行扩散思维。

        * @param driver

        * @param url

        * @param username

        * @param password

        * @param maxActive

        * @return

        */

       @Bean

    public DataSource druidDataSource(@Value("${spring.datasource.driverClassName}") String driver,

                                      @Value("${spring.datasource.url}") String url,

                                      @Value("${spring.datasource.username}") String username,

                                      @Value("${spring.datasource.password}") String password,

                                      @Value("${spring.datasource.maxActive}") int maxActive

                                                              ) {

        DruidDataSource druidDataSource = 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();

        }

        return druidDataSource;

    }

}

这里的区别在于加入一个方法:druidDataSource进行数据源的注入(当然这么一选择上一章节在application.properties配置的方式是比较好,如果有特殊需求的话,也可以在这里进行注入)。

如果同时进行了编程式的注入和配置的注入,配置的就无效了。

-----------------------------------------------------------------

实际中推荐使用配置文件的方式,参考:

(15)Spring Boot使用Druid和监控配置【从零开始学Spring Boot】:http://412887952-qq-com.iteye.com/blog/2292362

Spring Boot 系列博客】

58. Spring Boot国际化(i18n)【从零开始学Spring Boot】

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

56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】

55. spring boot 服务配置和部署【从零开始学Spring Boot】

54. spring boot日志升级篇—logback【从零开始学Spring Boot】

52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】

51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

50. Spring Boot日志升级篇—log4j【从零开始学Spring Boot】

49. spring boot日志升级篇—理论【从零开始学Spring Boot】

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

47. Spring Boot发送邮件【从零开始学Spring Boot】

46. Spring Boot中使用AOP统一处理Web请求日志

45. Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】

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

43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】

42. Spring Boot多数据源【从零开始学Spring Boot】

41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

40. springboot + devtools(热部署)【从零开始学Spring Boot】

39.4 Spring Boot Shiro权限管理【从零开始学Spring Boot】

39.3 Spring Boot Shiro权限管理【从零开始学Spring Boot】

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

39.1 Spring Boot Shiro权限管理【从零开始学Spring Boot】

38 Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】

37 Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

36 Spring Boot Cache理论篇【从零开始学Spring Boot】

35 Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

34Spring Boot的启动器Starter详解【从零开始学Spring Boot】

33 Spring Boot 监控和管理生产环境【从零开始学Spring Boot】

32 Spring Boot使用@SpringBootApplication注解【从零开始学Spring Boot】

31 Spring Boot导入XML配置【从零开始学Spring Boot】

更多查看博客: http://412887952-qq-com.iteye.com/

(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. HDU 5536/ 2015长春区域 J.Chip Factory Trie

    Chip Factory Problem Description John is a manager of a CPU chip factory, the factory produces lots ...

  2. commons-fileupload上传文件(1)

    近期,写一个上传图片的功能.于是用到commons-fileupload这个组件.提过form提交表单到后台(这里没实用到structs框架).在后台List pl = dfu.parseReques ...

  3. 一个简单的JS日期挂历脚本

    分享一个JS脚本做的日期挂历,在需要的时候可以引入你的程序. 如需单独引入这个脚本,请将它保存在一个文件中然后引入它:如这样 <script type="text/javascript ...

  4. bzoj 1029 [ JSOI 2007 ] 建筑抢修 —— 贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1029 想不出来贪心... 首先把任务按结束时间排序: 因为任务一定是越提前做越好,所以从头开 ...

  5. XML案例(使用JAXP进行SAX解析)

    1.Book.java package cn.itcast.sax; public class Book { private String name; private String author; p ...

  6. (Go)11.九九乘法表示例

    //九九乘法表 package main import ( "fmt" ) func chengfa() { ; m < ; m ++ { ; n <= m; n++ ...

  7. iOS开发之判断手机号和邮箱 正则表达式

    #pragma mark --判断手机号合法性 + (BOOL)checkPhone:(NSString *)phoneNumber { NSString *regex = @"^((13[ ...

  8. c语言中struct的初始化

    C++中的struct已经和class一样,可以用构造函数初始化. C语言中的struct怎么初始化呢? typedef struct _TEST_T {        int i;        c ...

  9. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  10. RabbitMQ 官方NET教程(二)【工作队列】

    这篇中我们将会创建一个工作队列用来在工作者(consumer)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务和避免必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送 ...