21. Spring Boot Druid 数据源配置解析

1、数据源配置属性类源码

package org.springframework.boot.autoconfigure.jdbc;

@ConfigurationProperties(
prefix = "spring.datasource"
)
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
private ClassLoader classLoader;
private String name;
private boolean generateUniqueName;
private Class<? extends DataSource> type;
private String driverClassName;
private String url;
private String username;
private String password;
private String jndiName;
private DataSourceInitializationMode initializationMode;
private String platform;
private List<String> schema;
private String schemaUsername;
private String schemaPassword;
private List<String> data;
private String dataUsername;
private String dataPassword;
private boolean continueOnError;
private String separator;
private Charset sqlScriptEncoding;
private EmbeddedDatabaseConnection embeddedDatabaseConnection;
private DataSourceProperties.Xa xa;
private String uniqueName;
......
}

详见Spring Boot 2.1.0文档

2、配置

spring:
datasource:
url: jdbc:mysql://127.0.0.1:/springboot?useSSL=false
username: root
password: xiaochao
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialization-mode: ALWAYS
schema-username: root
schema-password: xiaochao
schema:
- classpath:schema.sql
data-username: root
data-password: xiaochao
# hikari:
# username: root
# password: xiaochao
# jdbc-url: jdbc:mysql://localhost:/springboot?useSSL=false
# driver-class-name: com.mysql.cj.jdbc.Driver
# schema:
# - classpath:schema.sql
server:
port:

注意:schema.sql 没有生效,待解!!!

 3、注解注入数据源类型和配置过滤器类和Servelt类

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; import javax.sql.DataSource; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.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.ResourceServlet;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter; @Configuration
public class MyConfig { @Bean
@ConfigurationProperties("spring.datasource")
public DataSource dataSource() {
//对应DataSourceConfiguration类下的几个类型
//javax.sql.DataSource (Generic)
//BasicDataSource (Dbcp2)
//HikariDataSource (Hikari)
//org.apache.tomcat.jdbc.pool.DataSource (tomcat)
return DataSourceBuilder.create().type(DruidDataSource.class).build();
} //配置Druid数据源的Servelt
@Bean
public ServletRegistrationBean<StatViewServlet> statViewServlet() {
ServletRegistrationBean<StatViewServlet> servlet = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); Map<String,String> initParam = new HashMap<>();
initParam.put(ResourceServlet.PARAM_NAME_USERNAME,"admin");
initParam.put(ResourceServlet.PARAM_NAME_PASSWORD,"123456");
initParam.put(ResourceServlet.PARAM_NAME_ALLOW,"");//默认允许所有地址来源的访问
initParam.put(ResourceServlet.PARAM_NAME_DENY,"192.168.1.234"); servlet.setInitParameters(initParam);
return servlet;
}
//配置Druid数据源的过滤器
@Bean
public FilterRegistrationBean<WebStatFilter> StatViewFilter() {
WebStatFilter druidFilter = new WebStatFilter();
FilterRegistrationBean<WebStatFilter> filterBean = new FilterRegistrationBean<>(druidFilter); Map<String,String> initParam = new HashMap<>();
initParam.put(WebStatFilter.PARAM_NAME_EXCLUSIONS,"*.js,*.css,/druid/*");
filterBean.setUrlPatterns(Arrays.asList("/*")); //过滤所有请求
filterBean.setInitParameters(initParam);
return filterBean;
} }

21. Spring Boot Druid 数据源配置解析

21. Spring Boot Druid 数据源配置解析

上一篇:vim 标记 mark 详解 (转载)


下一篇:Node学习思维导图