SpringBoot 为什么能够自动的注入一些常用的Bean ?
原文转载至:https://blog.csdn.net/qq_29941401/article/details/79605388
但是我一直没有搞懂druid是怎么自动配置的?
这个是properties文件的配置,配置数据库数据源使用druid。
spring.datasource.password=123456 spring.datasource.username=yuanqiao spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.driver-class-name= oracle.jdbc.driver.OracleDriver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #spring.datasource.schema=oracle # 连接池的配置信息 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 spring.datasource.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
其实根本不用下面这个配置类,Springboot就能自动实例DataSource了,但是实例出来的DataSource会缺少很多DruidDataSource的属性。
package com.yuanqiao.configuration; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.context.properties.ConfigurationProperties; 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 javax.sql.DataSource; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @Configuration public class DruidConfiguration { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } //配置druid的监控 //配置一个后台管理的Servlet @Bean public ServletRegistrationBean StatViewServlet(){ ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); Map<String,String> initParams=new HashMap<>(); initParams.put("loginUsername","admin"); initParams.put("loginPassword","123456"); initParams.put("allow","");//默认就是允许所有 initParams.put("deny","192.168.1.4"); bean.setInitParameters(initParams); return bean; } //配置一个监控的Filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean =new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String,String> initParams=new HashMap<>(); initParams.put("exclusions","*.js,*.css,/druid/*"); bean.setUrlPatterns(Arrays.asList("/*")); bean.setInitParameters(initParams); return bean; } }
那个spring的IOC容易不就自动注入了两个DataSource实例吗?还是说,我们自己配置了DataSource之后,就以我们配置的为主呢?