java - druid-spring-boot-starter中的一个疑问
问题描述
先贴代码
@Configuration@ConditionalOnClass(com.alibaba.druid.pool.DruidDataSource.class)@EnableConfigurationProperties(DruidStatProperties.class)@Import({DruidSpringAopConfiguration.class, DruidStatViewServletConfiguration.class, DruidWebStatFilterConfiguration.class})public class DruidDataSourceAutoConfigure { @Bean @ConfigurationProperties('spring.datasource.druid') @ConditionalOnMissingBean public DataSource dataSource(Environment env) {DruidDataSource dataSource = DruidDataSourceBuilder.create().build();//if not found prefix ’spring.datasource.druid’ settings,’spring.datasource’ prefix settings will be used.if (dataSource.getUsername() == null) { dataSource.setUsername(env.getProperty('spring.datasource.username'));}if (dataSource.getPassword() == null) { dataSource.setPassword(env.getProperty('spring.datasource.password'));}if (dataSource.getUrl() == null) { dataSource.setUrl(env.getProperty('spring.datasource.url'));}if (dataSource.getDriverClassName() == null) { dataSource.setDriverClassName(env.getProperty('spring.datasource.driver-class-name'));}// set filters default value on StatViewServlet enabled.if (! 'false'.equals(env.getProperty('spring.datasource.druid.StatViewServlet.enabled'))) { try {dataSource.setFilters('stat'); } catch (SQLException e) {e.printStackTrace(); }}return dataSource; }}
public class DruidDataSourceBuilder { private Map<String, String> properties = new HashMap<String, String>(); public static DruidDataSourceBuilder create() {return new DruidDataSourceBuilder(); } public DruidDataSource build() {DruidDataSource dataSource = new DruidDataSource();maybeGetDriverClassName();bind(dataSource);return dataSource; } //use spring boot relaxed binding by reflection config druid . detail see Spring Boot Reference Relaxed binding section. private void bind(DruidDataSource result) {MutablePropertyValues properties = new MutablePropertyValues(this.properties);new RelaxedDataBinder(result).withAlias('url', 'jdbcUrl').withAlias('username', 'user').bind(properties); } private void maybeGetDriverClassName() {if (!this.properties.containsKey('driverClassName')&& this.properties.containsKey('url')) { String url = this.properties.get('url'); String driverClass = DatabaseDriver.fromJdbcUrl(url).getDriverClassName(); this.properties.put('driverClassName', driverClass);} }}
不理解的地方在DruidDataSourceBuilder的maybeGetDriverClassName方法,显然DruidDataSourceAutoConfigure中的方法dataSource在使用DruidDataSourceBuilder的时候,DruidDataSourceBuilder中的properties是没有属性的,这样的话maybeGetDriverClassName方法不是没有意义吗?还有一点是DruidDataSourceAutoConfigure的dataSource方法中的if判断,在方法中dataSource应该还没注入属性吧,这样判断不是也没意义?
问题解答
回答1:可能是为以后 properties 里面添加其他属性留余地吧。
相关文章:
1. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””2. angular.js - angular内容过长展开收起效果3. docker安装后出现Cannot connect to the Docker daemon.4. debian - docker依赖的aufs-tools源码哪里可以找到啊?5. docker镜像push报错6. docker gitlab 如何git clone?7. angular.js - angularjs的自定义过滤器如何给文字加颜色?8. mac里的docker如何命令行开启呢?9. javascript - 如何保证数据库里的信息安全?10. python - 用urllib抓取网页上的下载链接,目标文件是xls形式,但发现抓下来的xls是空表,里面只有一句报错信息,求帮助。