Skip to content

5.6 理解自动装配,113页-115页问题 #87

@nanlei

Description

@nanlei

阅读本书,收益颇多,今发现如下不解:

我在新版2.3.2 debug源代码时,发现5.6节113页-115页,使用HSQLDB的运行效果和书中不符,为了验证,将版本降至2.0.2,(两个版本都使用了HikariCP作为连接池)依赖为:

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

DataSourceAutoConfiguration.EmbeddedDatabaseConditiongetMatchOutcome方法中,执行到

			if (anyMatches(context, metadata, this.pooledCondition)) {
				return ConditionOutcome
						.noMatch(message.foundExactly("supported pooled data source"));
			}

就返回了(两版本相同),而没有继续往下匹配

			EmbeddedDatabaseType type = EmbeddedDatabaseConnection
					.get(context.getClassLoader()).getType();
			if (type == null) {
				return ConditionOutcome
						.noMatch(message.didNotFind("embedded database").atAll());
			}
			return ConditionOutcome.match(message.found("embedded database").items(type));

因为两个Spring版本debug结果一致,也就进一步看了看,这里意思是内嵌数据源支持连接池,PooledDataSourceAvailableCondition起了作用,有如下发现:
DataSourceProperties 实现了 InitializingBean接口,其中:

	@Override
	public void afterPropertiesSet() throws Exception {
		this.embeddedDatabaseConnection = EmbeddedDatabaseConnection
				.get(this.classLoader);
	}

被回调时才会去进行获取EmbeddedDatabaseConnection的类型HSQL

PooledDataSourceConfiguration装载时,Import了Hikari,而后Hikari通过DataSourceConfiguration.Hikari进行装载:

		@Bean
		@ConfigurationProperties(prefix = "spring.datasource.hikari")
		public HikariDataSource dataSource(DataSourceProperties properties) {
			HikariDataSource dataSource = createDataSource(properties,
					HikariDataSource.class);
			if (StringUtils.hasText(properties.getName())) {
				dataSource.setPoolName(properties.getName());
			}
			return dataSource;
		}

EmbeddedDataSourceConfiguration@Bean dataSource启动时并没有执行到。

因整书并没有看完,不知后续是否对此是否有进一步的解释,感觉在这里的表述确和实际不符,也不知我的运行是否有问题?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions