文档章节

SpringBoot集成Druid的最简单的小示例

k
 karma123
发布于 06/24 08:34
字数 422
阅读 48
收藏 6

参考网页

https://blog.csdn.net/king_is_everyone/article/details/53098350

建立maven工程

Pom文件

<?xml version="1.0" encoding="UTF-8"?>

    <project xmlns="http://maven.apache.org/POM/4.0.0"

             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

        <parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>1.4.1.RELEASE</version>

        </parent>

        <modelVersion>4.0.0</modelVersion>

 

        <artifactId>springboot-6</artifactId>

        <dependencies>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter</artifactId>

            </dependency>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-web</artifactId>

            </dependency>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-test</artifactId>

                <scope>test</scope>

            </dependency>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-jdbc</artifactId>

            </dependency>

            <dependency>

                <groupId>mysql</groupId>

                <artifactId>mysql-connector-java</artifactId>

                <version>5.1.39</version>

            </dependency>

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>druid</artifactId>

                <version>1.0.26</version>

            </dependency>

        </dependencies>

        <build>

            <plugins>

                <plugin>

                    <groupId>org.springframework.boot</groupId>

                    <artifactId>spring-boot-maven-plugin</artifactId>

                    <version>1.4.1.RELEASE</version>

                    <configuration>

                        <fork>true</fork>

                    </configuration>

                </plugin>

            </plugins>

        </build>

 

    </project>

主启动类

package com.richfit.ruiche.DruidExample;

 

import java.sql.SQLException;

import java.util.HashMap;

import java.util.Map;

 

import javax.sql.DataSource;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.boot.web.servlet.ServletRegistrationBean;

import org.springframework.context.annotation.Bean;

 

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.support.http.StatViewServlet;

import com.alibaba.druid.support.http.WebStatFilter;

 

@SpringBootApplication

public class DruidExampleApp {

public static void main(String[] args) throws Exception {

        SpringApplication.run(DruidExampleApp.class, args);

    }

 

    /**

     * 注册DruidServlet

     *

     * @return

     */

    @Bean

    public ServletRegistrationBean druidServletRegistrationBean() {

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();

        servletRegistrationBean.setServlet(new StatViewServlet());

        servletRegistrationBean.addUrlMappings("/druid/*");

        

        Map<String,String> params = new HashMap<String,String>();

        //账号密码,是否允许重置数据

        params.put("loginUsername","admin");

        params.put("loginPassword","admin");

        params.put("resetEnable","true");

        servletRegistrationBean.setInitParameters(params);

        

        return servletRegistrationBean;

    }

 

    /**

     * 注册DruidFilter拦截

     *

     * @return

     */

    @Bean

    public FilterRegistrationBean duridFilterRegistrationBean() {

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

        filterRegistrationBean.setFilter(new WebStatFilter());

        Map<String, String> initParams = new HashMap<String, String>();

        //设置忽略请求

        initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");

        filterRegistrationBean.setInitParameters(initParams);

        filterRegistrationBean.addUrlPatterns("/*");

        return filterRegistrationBean;

    }

 

    /**

     * 配置DataSource

     * @return

     * @throws SQLException

     */

    @Bean

    public DataSource druidDataSource() throws SQLException {

        DruidDataSource druidDataSource = new DruidDataSource();

        druidDataSource.setUsername("root");

        druidDataSource.setPassword("root");

        druidDataSource.setUrl("jdbc:mysql://localhost:3306/springbootdb_master");

        druidDataSource.setMaxActive(100);

        druidDataSource.setFilters("stat,wall");

        druidDataSource.setInitialSize(10);

        return druidDataSource;

    }

}

运行主启动类并测试

http://localhost:8080/druid/login.html

登录后

SpringBoot集成Druid的关键要点--以下3点在主启动类中都有所体现

注册DruidServlet

注册DruidFilter拦截

配置DataSource

© 著作权归作者所有

共有 人打赏支持
k
粉丝 8
博文 86
码字总数 64740
作品 0
丰台
私信 提问
恒宇少年/spring-boot-chapter

简书整套文档以及源码解析 专题 专题名称 专题描述 001 Spring Boot 核心技术 讲解SpringBoot一些企业级层面的核心组件 002 Spring Cloud 核心技术 对Spring Cloud核心技术全面讲解 003 Quer...

恒宇少年
04/19
0
0
Spring Boot学习资源

Spring Boot 入门 http://blog.csdn.net/isea533/article/details/50278205 ---------------------------------------------------------- Spring Boot 静态资源处理 http://blog.csdn.net/c......

IamOkay
2017/02/17
0
0
yadong.zhang/springboot-shiro

springboot-shiro 项目介绍 Springboot + shiro权限管理。这或许是流程最详细、代码最干净、配置最简单的shiro上手项目了。 开发环境 工具 版本或描述 OS Windows 7 JDK 1.7+ IDE IntelliJ I...

yadong.zhang
05/21
0
0
Spring Boot学习笔记

RabbitMQ RabbitMQ 安装 linux安装RabbitMQ详细教程 Ubuntu 16.04 RabbitMq 安装与运行(安装篇) ubantu安装rabbitMQ步骤 多线程 Spring @transactional注解和synchronized同步锁同时使用不...

OSC_fly
07/26
0
0
Spring Boot 全家桶 - SpringBootBucket

Spring Boot 现在已经成为Java 开发领域的一颗璀璨明珠,它本身是包容万象的,可以跟各种技术集成。 本项目对目前Web开发中常用的各个技术,通过和SpringBoot的集成,并且对各种技术通过“一...

一刀
03/05
0
1

没有更多内容

加载失败,请刷新页面

加载更多

在Flutter中嵌入Native组件的解决方案

摘要: 引言 在漫长的从Native向Flutter过渡的混合工程时期,要想平滑地过渡,在Flutter中使用Native中较为完善的控件会是一个很好的选择。本文希望向大家介绍AndroidView的使用方式以及在此...

阿里云官方博客
41分钟前
1
0
aws S3 util demo

package com.example.demo;import com.amazonaws.AmazonClientException;import com.amazonaws.AmazonServiceException;import com.amazonaws.auth.BasicAWSCredentials;import co......

经常把天聊死的胖子
今天
4
0
linux下查看cpu、memo、io、swap性能数据脚本

直接贴脚本: 1、cpu #!/bin/bashCurrentDate=`date -d today '+%Y%m%d'`CurrentTime=`date -d today '+%Y%m%d%H%M'`mytext="$CurrentTime\t`top -b -n 1 | grep Cpu\(s\......

郑加威
今天
5
0
MySQL之——查询重复记录、删除重复记录方法大全

MySQL之——查询重复记录、删除重复记录方法大全

安小乐
今天
2
0
spring容器启动,停止,关闭事件监听-ApplicationEvent

ApplicationEvent ApplicationEvent相当于一个事件,所有自定义事件都需要继承这个抽象类。在Eclipse中Ctrl+Shift+H调用类的层次结构列表,可以看到如下 Application下抽象子类ApplicationCo...

tantexian
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部