在上一节使用是配置文件的方式进行使用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. c# 删除程序占用的文件,强力删除文件,彻底删除文件,解除文件占用

    c# 删除程序占用的文件.清理删除文件.彻底删除文件,解除文件占用 文件打开时,以共享读写模式打开 FileStream inputStream = new FileStream(name, File ...

  2. hdoj-看病要排队

    看病要排队 Problem Description 看病要排队这个是地球人都知道的常识. 不过经过细心的0068的观察,他发现了医院里排队还是有讲究的.0068所去的医院有三个医生(汗,这么少)同时看 ...

  3. 洛谷P3808 & P3796 AC自动机模板

    题目:P3808:https://www.luogu.org/problemnew/show/P3808 P3796:https://www.luogu.org/problemnew/show/P37 ...

  4. Linux下查看操作系统的位数和系统名称版本信息

    Linux下如何明确地查看操作系统的位数 如何知晓操作系统是32位还是64位?这里介绍一种简单的方式: [plain] [root@localhost mysql-5.1.57]# getconf L ...

  5. acc文件的运行

    1.method 1: use "acc" >acc hello.acc world.mc <--- compilation will generate the hel ...

  6. 【LuoguP2210 USACO】 Haywire

    这种答案跟序列排列顺序有关的,n比较小的(稍微大一点的也可以),求最优解的,一般都可以随机化过 随机化不一定是模拟退火或是什么遗传蚁群 哪怕只是直接随机化一个序列,只要你随机的次数够多,它都能找到正解 ...

  7. UIView动画基础

    1 Position 平移 [UIView animateWithDuration:1.0 animations:^{ _blueView.centerX = self.view.width -100 ...

  8. POJ 1149 PIGS (AC这道题很不容易啊)网络流

    PIGS Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlo ...

  9. [Luogu 1850] noip16 换教室

    [Luogu 1850] noip16 换教室 好久没有更博客了,先唠嗑一会,花了两天的空闲时间大致做完了昨年的noip真题 虽然在经过思考大部分题目都可出解(天天爱跑步除外),但是并不知道考试时候造 ...

  10. CSS浮动的处理

    之前已经发过一遍有关浮动的解决办法,今天看到一些资料后又有了新的想法: 在CSS布局中float属性经常会被用到,但使用float属性后会使其在普通流中脱离父容器,让人很苦恼 1 浮动带来布局的便利, ...