Merge branch '3.0.x' into main
This commit is contained in:
@@ -318,8 +318,15 @@ public class EnvironmentRepositoryConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(JdbcTemplate.class)
|
||||
public JdbcEnvironmentRepositoryFactory jdbcEnvironmentRepositoryFactory(JdbcTemplate jdbc) {
|
||||
return new JdbcEnvironmentRepositoryFactory(jdbc);
|
||||
public JdbcEnvironmentRepositoryFactory jdbcEnvironmentRepositoryFactory(JdbcTemplate jdbc,
|
||||
JdbcEnvironmentRepository.PropertiesResultSetExtractor propertiesResultSetExtractor) {
|
||||
return new JdbcEnvironmentRepositoryFactory(jdbc, propertiesResultSetExtractor);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(JdbcEnvironmentRepository.PropertiesResultSetExtractor.class)
|
||||
public JdbcEnvironmentRepository.PropertiesResultSetExtractor propertiesResultSetExtractor() {
|
||||
return new JdbcEnvironmentRepository.PropertiesResultSetExtractor();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
|
||||
private final JdbcTemplate jdbc;
|
||||
|
||||
private final PropertiesResultSetExtractor extractor = new PropertiesResultSetExtractor();
|
||||
private final PropertiesResultSetExtractor extractor;
|
||||
|
||||
private int order;
|
||||
|
||||
@@ -65,11 +65,18 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
|
||||
private boolean failOnError;
|
||||
|
||||
@Deprecated
|
||||
public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties properties) {
|
||||
this(jdbc, properties, new PropertiesResultSetExtractor());
|
||||
}
|
||||
|
||||
public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties properties,
|
||||
PropertiesResultSetExtractor extractor) {
|
||||
this.jdbc = jdbc;
|
||||
this.order = properties.getOrder();
|
||||
this.sql = properties.getSql();
|
||||
this.failOnError = properties.isFailOnError();
|
||||
this.extractor = extractor;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
@@ -105,7 +112,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
for (String app : applications) {
|
||||
for (String env : envs) {
|
||||
try {
|
||||
Map<String, String> next = this.jdbc.query(this.sql, this.extractor, app, env, label);
|
||||
Map<String, Object> next = this.jdbc.query(this.sql, this.extractor, app, env, label);
|
||||
if (next != null && !next.isEmpty()) {
|
||||
environment.add(new PropertySource(app + "-" + env, next));
|
||||
}
|
||||
@@ -142,11 +149,11 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
|
||||
this.failOnError = failOnError;
|
||||
}
|
||||
|
||||
public static class PropertiesResultSetExtractor implements ResultSetExtractor<Map<String, String>> {
|
||||
public static class PropertiesResultSetExtractor implements ResultSetExtractor<Map<String, Object>> {
|
||||
|
||||
@Override
|
||||
public Map<String, String> extractData(ResultSet rs) throws SQLException, DataAccessException {
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
public Map<String, Object> extractData(ResultSet rs) throws SQLException, DataAccessException {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
while (rs.next()) {
|
||||
map.put(rs.getString(1), rs.getString(2));
|
||||
}
|
||||
|
||||
@@ -24,15 +24,24 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
public class JdbcEnvironmentRepositoryFactory
|
||||
implements EnvironmentRepositoryFactory<JdbcEnvironmentRepository, JdbcEnvironmentProperties> {
|
||||
|
||||
private JdbcTemplate jdbc;
|
||||
private final JdbcTemplate jdbc;
|
||||
|
||||
private final JdbcEnvironmentRepository.PropertiesResultSetExtractor extractor;
|
||||
|
||||
@Deprecated
|
||||
public JdbcEnvironmentRepositoryFactory(JdbcTemplate jdbc) {
|
||||
this(jdbc, new JdbcEnvironmentRepository.PropertiesResultSetExtractor());
|
||||
}
|
||||
|
||||
public JdbcEnvironmentRepositoryFactory(JdbcTemplate jdbc,
|
||||
JdbcEnvironmentRepository.PropertiesResultSetExtractor extractor) {
|
||||
this.jdbc = jdbc;
|
||||
this.extractor = extractor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JdbcEnvironmentRepository build(JdbcEnvironmentProperties environmentProperties) {
|
||||
return new JdbcEnvironmentRepository(this.jdbc, environmentProperties);
|
||||
return new JdbcEnvironmentRepository(this.jdbc, environmentProperties, extractor);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
package org.springframework.cloud.config.server.environment;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -25,6 +28,7 @@ import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.cloud.config.server.ConfigServerApplication;
|
||||
import org.springframework.cloud.config.server.test.ConfigServerTestUtils;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -42,6 +46,19 @@ public class JdbcEnvironmentRepositoryConfigurationTests {
|
||||
.run(context -> {
|
||||
assertThat(context).hasSingleBean(JdbcEnvironmentRepositoryFactory.class);
|
||||
assertThat(context).hasSingleBean(JdbcEnvironmentRepository.class);
|
||||
assertThat(context).hasSingleBean(JdbcEnvironmentRepository.PropertiesResultSetExtractor.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jdbcEnvironmentRepositoryBeansConfiguredWitCustomResultSetExtractor() {
|
||||
new WebApplicationContextRunner().withUserConfiguration(ConfigServerApplication.class)
|
||||
.withBean(CustomResultSetExtractor.class, CustomResultSetExtractor::new)
|
||||
.withPropertyValues("spring.profiles.active=test,jdbc", "spring.main.web-application-type=none")
|
||||
.run(context -> {
|
||||
assertThat(context).hasSingleBean(JdbcEnvironmentRepositoryFactory.class);
|
||||
assertThat(context).hasSingleBean(JdbcEnvironmentRepository.class);
|
||||
assertThat(context).hasSingleBean(CustomResultSetExtractor.class);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -75,4 +92,13 @@ public class JdbcEnvironmentRepositoryConfigurationTests {
|
||||
.run(consumer);
|
||||
}
|
||||
|
||||
private static class CustomResultSetExtractor extends JdbcEnvironmentRepository.PropertiesResultSetExtractor {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> extractData(ResultSet rs) throws SQLException, DataAccessException {
|
||||
return super.extractData(rs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,7 +50,8 @@ public class JdbcEnvironmentRepositoryTests {
|
||||
@Test
|
||||
public void basicProperties() {
|
||||
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource),
|
||||
new JdbcEnvironmentProperties()).findOne("foo", "bar", "");
|
||||
new JdbcEnvironmentProperties(), new JdbcEnvironmentRepository.PropertiesResultSetExtractor())
|
||||
.findOne("foo", "bar", "");
|
||||
assertThat(env.getName()).isEqualTo("foo");
|
||||
assertThat(env.getProfiles()).isEqualTo(new String[] { "default", "bar" });
|
||||
assertThat(env.getLabel()).isEqualTo("master");
|
||||
|
||||
Reference in New Issue
Block a user