赞
踩
目录
- package com.roncoo.education.util.configuration;
-
- import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
- import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.boot.jdbc.DatabaseDriver;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import com.alibaba.druid.pool.DruidDataSource;
-
- @Configuration
- public class DruidConfiguration {
-
- @ConditionalOnClass(DruidDataSource.class)
- @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true)
- static class Druid extends DruidConfiguration {
- @Bean
- @ConfigurationProperties("spring.datasource.druid")
- public DruidDataSource dataSource(DataSourceProperties properties) {
- DruidDataSource druidDataSource = (DruidDataSource) properties.initializeDataSourceBuilder().type(DruidDataSource.class).build();
- DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl());
- String validationQuery = databaseDriver.getValidationQuery();
- if (validationQuery != null) {
- druidDataSource.setValidationQuery(validationQuery);
- }
- return druidDataSource;
- }
- }
- }
- package com.roncoo.education.util.servlet;
-
- import javax.servlet.annotation.WebInitParam;
- import javax.servlet.annotation.WebServlet;
-
- import com.alibaba.druid.support.http.StatViewServlet;
-
- /**
- *
- * @author
- * 访问http://localhost:8080/druid会有druid的管理工具
- */
- @WebServlet(urlPatterns = { "/druid/*" }, initParams = { @WebInitParam(name = "loginUsername", value = "roncoo"), @WebInitParam(name = "loginPassword", value = "roncoo") })
- public class DruidStatViewServlet extends StatViewServlet {
-
- private static final long serialVersionUID = 1L;
-
- }
-
- package com.roncoo.education.util.filter;
-
- import javax.servlet.annotation.WebFilter;
- import javax.servlet.annotation.WebInitParam;
-
- import com.alibaba.druid.support.http.WebStatFilter;
-
- /**
- * @author
- */
- @WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = { @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*") })
- public class DruidWebStatFilter extends WebStatFilter {
-
- }
- #初始化连接大小
- spring.datasource.druid.initial-size=8 #最小空闲连接数
- spring.datasource.druid.min-idle=5 #最大连接数
- spring.datasource.druid.max-active=10
- #查询超时时间
- spring.datasource.druid.query-timeout=6000
- #事务查询超时时间
- spring.datasource.druid.transaction-query-timeout=6000
- #关闭空闲连接超时时间
- spring.datasource.druid.remove-abandoned-timeout=1800
- package com.roncoo.education;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.servlet.ServletComponentScan;
- import org.springframework.context.annotation.ImportResource;
-
- @ServletComponentScan
- @SpringBootApplication
- @ImportResource(locations = { "classpath:druid-bean.xml" })
- public class SpringBootDemo291Application {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringBootDemo291Application.class, args);
- }
- }
druid-bean.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd">
-
- <!-- 配置_Druid和Spring关联监控配置 -->
- <bean id="druid-stat-interceptor"
- class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>
-
- <!-- 方法名正则匹配拦截配置 -->
- <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
- scope="prototype">
- <property name="patterns">
- <list>
- <value>com.roncoo.education.mapper.*</value>
- </list>
- </property>
- </bean>
-
- <aop:config proxy-target-class="true">
- <aop:advisor advice-ref="druid-stat-interceptor"
- pointcut-ref="druid-stat-pointcut" />
- </aop:config>
-
- </beans>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。