Commit 5e9cfea5 authored by Phillip Webb's avatar Phillip Webb

Migrate to auto-configurations to PropertyMapper

Update auto-configuration classes that have extensive property mapping
code to make use of the new `PropertyMapper` utility.

Fixes gh-9018
parent 241a7086
...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.amqp; ...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.amqp;
import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.context.properties.PropertyMapper;
/** /**
* Configure {@link DirectRabbitListenerContainerFactoryConfigurer} with sensible * Configure {@link DirectRabbitListenerContainerFactoryConfigurer} with sensible
...@@ -33,12 +34,12 @@ public final class DirectRabbitListenerContainerFactoryConfigurer extends ...@@ -33,12 +34,12 @@ public final class DirectRabbitListenerContainerFactoryConfigurer extends
@Override @Override
public void configure(DirectRabbitListenerContainerFactory factory, public void configure(DirectRabbitListenerContainerFactory factory,
ConnectionFactory connectionFactory) { ConnectionFactory connectionFactory) {
PropertyMapper map = PropertyMapper.get();
RabbitProperties.DirectContainer config = getRabbitProperties().getListener() RabbitProperties.DirectContainer config = getRabbitProperties().getListener()
.getDirect(); .getDirect();
configure(factory, connectionFactory, config); configure(factory, connectionFactory, config);
if (config.getConsumersPerQueue() != null) { map.from(config::getConsumersPerQueue).whenNonNull()
factory.setConsumersPerQueue(config.getConsumersPerQueue()); .to(factory::setConsumersPerQueue);
}
} }
} }
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.amqp; package org.springframework.boot.autoconfigure.amqp;
import java.time.Duration;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpAdmin;
...@@ -33,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -33,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
...@@ -76,6 +79,7 @@ import org.springframework.retry.support.RetryTemplate; ...@@ -76,6 +79,7 @@ import org.springframework.retry.support.RetryTemplate;
* @author Josh Long * @author Josh Long
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Gary Russell * @author Gary Russell
* @author Phillip Webb
*/ */
@Configuration @Configuration
@ConditionalOnClass({ RabbitTemplate.class, Channel.class }) @ConditionalOnClass({ RabbitTemplate.class, Channel.class })
...@@ -88,66 +92,55 @@ public class RabbitAutoConfiguration { ...@@ -88,66 +92,55 @@ public class RabbitAutoConfiguration {
protected static class RabbitConnectionFactoryCreator { protected static class RabbitConnectionFactoryCreator {
@Bean @Bean
public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config) public CachingConnectionFactory rabbitConnectionFactory(
throws Exception { RabbitProperties properties) throws Exception {
PropertyMapper map = PropertyMapper.get();
CachingConnectionFactory factory = new CachingConnectionFactory(
getRabbitConnectionFactoryBean(properties).getObject());
map.from(properties::determineAddresses).to(factory::setAddresses);
map.from(properties::isPublisherConfirms).to(factory::setPublisherConfirms);
map.from(properties::isPublisherReturns).to(factory::setPublisherReturns);
RabbitProperties.Cache.Channel channel = properties.getCache().getChannel();
map.from(channel::getSize).whenNonNull().to(factory::setChannelCacheSize);
map.from(channel::getCheckoutTimeout).whenNonNull()
.to(factory::setChannelCheckoutTimeout);
RabbitProperties.Cache.Connection connection = properties.getCache()
.getConnection();
map.from(connection::getMode).whenNonNull().to(factory::setCacheMode);
map.from(connection::getSize).whenNonNull()
.to(factory::setConnectionCacheSize);
return factory;
}
private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean(
RabbitProperties properties) throws Exception {
PropertyMapper map = PropertyMapper.get();
RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean(); RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean();
if (config.determineHost() != null) { map.from(properties::determineHost).whenNonNull().to(factory::setHost);
factory.setHost(config.determineHost()); map.from(properties::determinePort).to(factory::setPort);
} map.from(properties::determineUsername).whenNonNull()
factory.setPort(config.determinePort()); .to(factory::setUsername);
if (config.determineUsername() != null) { map.from(properties::determinePassword).whenNonNull()
factory.setUsername(config.determineUsername()); .to(factory::setPassword);
} map.from(properties::determineVirtualHost).whenNonNull()
if (config.determinePassword() != null) { .to(factory::setVirtualHost);
factory.setPassword(config.determinePassword()); map.from(properties::getRequestedHeartbeat).whenNonNull()
} .asInt(Duration::getSeconds).to(factory::setRequestedHeartbeat);
if (config.determineVirtualHost() != null) { RabbitProperties.Ssl ssl = properties.getSsl();
factory.setVirtualHost(config.determineVirtualHost());
}
if (config.getRequestedHeartbeat() != null) {
factory.setRequestedHeartbeat(
(int) config.getRequestedHeartbeat().getSeconds());
}
RabbitProperties.Ssl ssl = config.getSsl();
if (ssl.isEnabled()) { if (ssl.isEnabled()) {
factory.setUseSSL(true); factory.setUseSSL(true);
if (ssl.getAlgorithm() != null) { map.from(ssl::getAlgorithm).whenNonNull().to(factory::setSslAlgorithm);
factory.setSslAlgorithm(ssl.getAlgorithm()); map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);
} map.from(ssl::getKeyStore).to(factory::setKeyStore);
factory.setKeyStoreType(ssl.getKeyStoreType()); map.from(ssl::getKeyStorePassword).to(factory::setKeyStorePassphrase);
factory.setKeyStore(ssl.getKeyStore()); map.from(ssl::getTrustStoreType).to(factory::setTrustStoreType);
factory.setKeyStorePassphrase(ssl.getKeyStorePassword()); map.from(ssl::getTrustStore).to(factory::setTrustStore);
factory.setTrustStoreType(ssl.getTrustStoreType()); map.from(ssl::getTrustStorePassword).to(factory::setTrustStorePassphrase);
factory.setTrustStore(ssl.getTrustStore());
factory.setTrustStorePassphrase(ssl.getTrustStorePassword());
}
if (config.getConnectionTimeout() != null) {
factory.setConnectionTimeout(
(int) config.getConnectionTimeout().toMillis());
} }
map.from(properties::getConnectionTimeout).whenNonNull()
.asInt(Duration::toMillis).to(factory::setConnectionTimeout);
factory.afterPropertiesSet(); factory.afterPropertiesSet();
CachingConnectionFactory connectionFactory = new CachingConnectionFactory( return factory;
factory.getObject());
connectionFactory.setAddresses(config.determineAddresses());
connectionFactory.setPublisherConfirms(config.isPublisherConfirms());
connectionFactory.setPublisherReturns(config.isPublisherReturns());
if (config.getCache().getChannel().getSize() != null) {
connectionFactory
.setChannelCacheSize(config.getCache().getChannel().getSize());
}
if (config.getCache().getConnection().getMode() != null) {
connectionFactory
.setCacheMode(config.getCache().getConnection().getMode());
}
if (config.getCache().getConnection().getSize() != null) {
connectionFactory.setConnectionCacheSize(
config.getCache().getConnection().getSize());
}
if (config.getCache().getChannel().getCheckoutTimeout() != null) {
connectionFactory.setChannelCheckoutTimeout(
config.getCache().getChannel().getCheckoutTimeout());
}
return connectionFactory;
} }
} }
...@@ -171,26 +164,24 @@ public class RabbitAutoConfiguration { ...@@ -171,26 +164,24 @@ public class RabbitAutoConfiguration {
@ConditionalOnSingleCandidate(ConnectionFactory.class) @ConditionalOnSingleCandidate(ConnectionFactory.class)
@ConditionalOnMissingBean(RabbitTemplate.class) @ConditionalOnMissingBean(RabbitTemplate.class)
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); PropertyMapper map = PropertyMapper.get();
RabbitTemplate template = new RabbitTemplate(connectionFactory);
MessageConverter messageConverter = this.messageConverter.getIfUnique(); MessageConverter messageConverter = this.messageConverter.getIfUnique();
if (messageConverter != null) { if (messageConverter != null) {
rabbitTemplate.setMessageConverter(messageConverter); template.setMessageConverter(messageConverter);
}
rabbitTemplate.setMandatory(determineMandatoryFlag());
RabbitProperties.Template templateProperties = this.properties.getTemplate();
RabbitProperties.Retry retryProperties = templateProperties.getRetry();
if (retryProperties.isEnabled()) {
rabbitTemplate.setRetryTemplate(createRetryTemplate(retryProperties));
} }
if (templateProperties.getReceiveTimeout() != null) { template.setMandatory(determineMandatoryFlag());
rabbitTemplate.setReceiveTimeout(templateProperties.getReceiveTimeout()); RabbitProperties.Template properties = this.properties.getTemplate();
if (properties.getRetry().isEnabled()) {
template.setRetryTemplate(createRetryTemplate(properties.getRetry()));
} }
if (templateProperties.getReplyTimeout() != null) { map.from(properties::getReceiveTimeout).whenNonNull()
rabbitTemplate.setReplyTimeout(templateProperties.getReplyTimeout()); .to(template::setReceiveTimeout);
} map.from(properties::getReplyTimeout).whenNonNull()
rabbitTemplate.setExchange(templateProperties.getExchange()); .to(template::setReplyTimeout);
rabbitTemplate.setRoutingKey(templateProperties.getRoutingKey()); map.from(properties::getExchange).to(template::setExchange);
return rabbitTemplate; map.from(properties::getRoutingKey).to(template::setRoutingKey);
return template;
} }
private boolean determineMandatoryFlag() { private boolean determineMandatoryFlag() {
...@@ -199,14 +190,16 @@ public class RabbitAutoConfiguration { ...@@ -199,14 +190,16 @@ public class RabbitAutoConfiguration {
} }
private RetryTemplate createRetryTemplate(RabbitProperties.Retry properties) { private RetryTemplate createRetryTemplate(RabbitProperties.Retry properties) {
PropertyMapper map = PropertyMapper.get();
RetryTemplate template = new RetryTemplate(); RetryTemplate template = new RetryTemplate();
SimpleRetryPolicy policy = new SimpleRetryPolicy(); SimpleRetryPolicy policy = new SimpleRetryPolicy();
policy.setMaxAttempts(properties.getMaxAttempts()); map.from(properties::getMaxAttempts).to(policy::setMaxAttempts);
template.setRetryPolicy(policy); template.setRetryPolicy(policy);
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(properties.getInitialInterval()); map.from(properties::getInitialInterval)
backOffPolicy.setMultiplier(properties.getMultiplier()); .to(backOffPolicy::setInitialInterval);
backOffPolicy.setMaxInterval(properties.getMaxInterval()); map.from(properties::getMultiplier).to(backOffPolicy::setMultiplier);
map.from(properties::getMaxInterval).to(backOffPolicy::setMaxInterval);
template.setBackOffPolicy(backOffPolicy); template.setBackOffPolicy(backOffPolicy);
return template; return template;
} }
......
...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.amqp; ...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.amqp;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.context.properties.PropertyMapper;
/** /**
* Configure {@link SimpleRabbitListenerContainerFactoryConfigurer} with sensible * Configure {@link SimpleRabbitListenerContainerFactoryConfigurer} with sensible
...@@ -33,18 +34,15 @@ public final class SimpleRabbitListenerContainerFactoryConfigurer extends ...@@ -33,18 +34,15 @@ public final class SimpleRabbitListenerContainerFactoryConfigurer extends
@Override @Override
public void configure(SimpleRabbitListenerContainerFactory factory, public void configure(SimpleRabbitListenerContainerFactory factory,
ConnectionFactory connectionFactory) { ConnectionFactory connectionFactory) {
PropertyMapper map = PropertyMapper.get();
RabbitProperties.SimpleContainer config = getRabbitProperties().getListener() RabbitProperties.SimpleContainer config = getRabbitProperties().getListener()
.getSimple(); .getSimple();
configure(factory, connectionFactory, config); configure(factory, connectionFactory, config);
if (config.getConcurrency() != null) { map.from(config::getConcurrency).whenNonNull()
factory.setConcurrentConsumers(config.getConcurrency()); .to(factory::setConcurrentConsumers);
} map.from(config::getMaxConcurrency).whenNonNull()
if (config.getMaxConcurrency() != null) { .to(factory::setMaxConcurrentConsumers);
factory.setMaxConcurrentConsumers(config.getMaxConcurrency()); map.from(config::getTransactionSize).whenNonNull().to(factory::setTxSize);
}
if (config.getTransactionSize() != null) {
factory.setTxSize(config.getTransactionSize());
}
} }
} }
...@@ -27,9 +27,9 @@ import org.springframework.batch.core.launch.support.SimpleJobLauncher; ...@@ -27,9 +27,9 @@ import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.StringUtils;
/** /**
* Basic {@link BatchConfigurer} implementation. * Basic {@link BatchConfigurer} implementation.
...@@ -103,14 +103,13 @@ public class BasicBatchConfigurer implements BatchConfigurer { ...@@ -103,14 +103,13 @@ public class BasicBatchConfigurer implements BatchConfigurer {
} }
protected JobExplorer createJobExplorer() throws Exception { protected JobExplorer createJobExplorer() throws Exception {
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean(); PropertyMapper map = PropertyMapper.get();
jobExplorerFactoryBean.setDataSource(this.dataSource); JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
String tablePrefix = this.properties.getTablePrefix(); factory.setDataSource(this.dataSource);
if (StringUtils.hasText(tablePrefix)) { map.from(this.properties::getTablePrefix).whenHasText()
jobExplorerFactoryBean.setTablePrefix(tablePrefix); .to(factory::setTablePrefix);
} factory.afterPropertiesSet();
jobExplorerFactoryBean.afterPropertiesSet(); return factory.getObject();
return jobExplorerFactoryBean.getObject();
} }
protected JobLauncher createJobLauncher() throws Exception { protected JobLauncher createJobLauncher() throws Exception {
...@@ -122,16 +121,13 @@ public class BasicBatchConfigurer implements BatchConfigurer { ...@@ -122,16 +121,13 @@ public class BasicBatchConfigurer implements BatchConfigurer {
protected JobRepository createJobRepository() throws Exception { protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(this.dataSource); PropertyMapper map = PropertyMapper.get();
String isolationLevel = determineIsolationLevel(); map.from(() -> this.dataSource).to(factory::setDataSource);
if (isolationLevel != null) { map.from(this::determineIsolationLevel).whenNonNull()
factory.setIsolationLevelForCreate(isolationLevel); .to(factory::setIsolationLevelForCreate);
} map.from(this.properties::getTablePrefix).whenHasText()
String tablePrefix = this.properties.getTablePrefix(); .to(factory::setTablePrefix);
if (StringUtils.hasText(tablePrefix)) { map.from(this::getTransactionManager).to(factory::setTransactionManager);
factory.setTablePrefix(tablePrefix);
}
factory.setTransactionManager(getTransactionManager());
factory.afterPropertiesSet(); factory.afterPropertiesSet();
return factory.getObject(); return factory.getObject();
} }
......
...@@ -23,9 +23,11 @@ import com.couchbase.client.java.Bucket; ...@@ -23,9 +23,11 @@ import com.couchbase.client.java.Bucket;
import com.couchbase.client.spring.cache.CacheBuilder; import com.couchbase.client.spring.cache.CacheBuilder;
import com.couchbase.client.spring.cache.CouchbaseCacheManager; import com.couchbase.client.spring.cache.CouchbaseCacheManager;
import org.springframework.boot.autoconfigure.cache.CacheProperties.Couchbase;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
...@@ -61,10 +63,9 @@ public class CouchbaseCacheConfiguration { ...@@ -61,10 +63,9 @@ public class CouchbaseCacheConfiguration {
public CouchbaseCacheManager cacheManager() { public CouchbaseCacheManager cacheManager() {
List<String> cacheNames = this.cacheProperties.getCacheNames(); List<String> cacheNames = this.cacheProperties.getCacheNames();
CacheBuilder builder = CacheBuilder.newInstance(this.bucket); CacheBuilder builder = CacheBuilder.newInstance(this.bucket);
Duration expiration = this.cacheProperties.getCouchbase().getExpiration(); Couchbase couchbase = this.cacheProperties.getCouchbase();
if (expiration != null) { PropertyMapper.get().from(couchbase::getExpiration).whenNonNull()
builder = builder.withExpiration((int) expiration.getSeconds()); .asInt(Duration::getSeconds).to(builder::withExpiration);
}
String[] names = cacheNames.toArray(new String[cacheNames.size()]); String[] names = cacheNames.toArray(new String[cacheNames.size()]);
CouchbaseCacheManager cacheManager = new CouchbaseCacheManager(builder, names); CouchbaseCacheManager cacheManager = new CouchbaseCacheManager(builder, names);
return this.customizers.customize(cacheManager); return this.customizers.customize(cacheManager);
......
...@@ -77,10 +77,8 @@ public class InfinispanCacheConfiguration { ...@@ -77,10 +77,8 @@ public class InfinispanCacheConfiguration {
EmbeddedCacheManager cacheManager = createEmbeddedCacheManager(); EmbeddedCacheManager cacheManager = createEmbeddedCacheManager();
List<String> cacheNames = this.cacheProperties.getCacheNames(); List<String> cacheNames = this.cacheProperties.getCacheNames();
if (!CollectionUtils.isEmpty(cacheNames)) { if (!CollectionUtils.isEmpty(cacheNames)) {
for (String cacheName : cacheNames) { cacheNames.forEach((cacheName) -> cacheManager.defineConfiguration(cacheName,
cacheManager.defineConfiguration(cacheName, getDefaultCacheConfiguration()));
getDefaultCacheConfiguration());
}
} }
return cacheManager; return cacheManager;
} }
......
...@@ -16,15 +16,13 @@ ...@@ -16,15 +16,13 @@
package org.springframework.boot.autoconfigure.cassandra; package org.springframework.boot.autoconfigure.cassandra;
import java.time.Duration;
import java.util.List; import java.util.List;
import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PoolingOptions; import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.QueryOptions; import com.datastax.driver.core.QueryOptions;
import com.datastax.driver.core.SocketOptions; import com.datastax.driver.core.SocketOptions;
import com.datastax.driver.core.policies.LoadBalancingPolicy;
import com.datastax.driver.core.policies.ReconnectionPolicy;
import com.datastax.driver.core.policies.RetryPolicy;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
...@@ -32,6 +30,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; ...@@ -32,6 +30,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -62,36 +61,27 @@ public class CassandraAutoConfiguration { ...@@ -62,36 +61,27 @@ public class CassandraAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public Cluster cassandraCluster() { public Cluster cassandraCluster() {
PropertyMapper map = PropertyMapper.get();
CassandraProperties properties = this.properties; CassandraProperties properties = this.properties;
Cluster.Builder builder = Cluster.builder() Cluster.Builder builder = Cluster.builder()
.withClusterName(properties.getClusterName()) .withClusterName(properties.getClusterName())
.withPort(properties.getPort()); .withPort(properties.getPort());
if (properties.getUsername() != null) { map.from(properties::getUsername).whenNonNull().to((username) -> builder
builder.withCredentials(properties.getUsername(), properties.getPassword()); .withCredentials(username, properties.getPassword()));
} map.from(properties::getCompression).whenNonNull().to(builder::withCompression);
if (properties.getCompression() != null) { map.from(properties::getLoadBalancingPolicy).whenNonNull()
builder.withCompression(properties.getCompression()); .as(BeanUtils::instantiateClass).to(builder::withLoadBalancingPolicy);
} map.from(this::getQueryOptions).to(builder::withQueryOptions);
if (properties.getLoadBalancingPolicy() != null) { map.from(properties::getReconnectionPolicy).whenNonNull()
LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy()); .as(BeanUtils::instantiateClass).to(builder::withReconnectionPolicy);
builder.withLoadBalancingPolicy(policy); map.from(properties::getRetryPolicy).whenNonNull().as(BeanUtils::instantiateClass)
} .to(builder::withRetryPolicy);
builder.withQueryOptions(getQueryOptions()); map.from(this::getSocketOptions).to(builder::withSocketOptions);
if (properties.getReconnectionPolicy() != null) { map.from(properties::isSsl).whenTrue().toCall(builder::withSSL);
ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy()); map.from(this::getPoolingOptions).to(builder::withPoolingOptions);
builder.withReconnectionPolicy(policy); map.from(properties::getContactPoints)
} .as((list) -> list.toArray(new String[list.size()]))
if (properties.getRetryPolicy() != null) { .to(builder::addContactPoints);
RetryPolicy policy = instantiate(properties.getRetryPolicy());
builder.withRetryPolicy(policy);
}
builder.withSocketOptions(getSocketOptions());
if (properties.isSsl()) {
builder.withSSL();
}
builder.withPoolingOptions(getPoolingOptions());
builder.addContactPoints(properties.getContactPoints().toArray(new String[0]));
customize(builder); customize(builder);
return builder.build(); return builder.build();
} }
...@@ -104,50 +94,39 @@ public class CassandraAutoConfiguration { ...@@ -104,50 +94,39 @@ public class CassandraAutoConfiguration {
} }
} }
public static <T> T instantiate(Class<T> type) {
return BeanUtils.instantiateClass(type);
}
private QueryOptions getQueryOptions() { private QueryOptions getQueryOptions() {
PropertyMapper map = PropertyMapper.get();
QueryOptions options = new QueryOptions(); QueryOptions options = new QueryOptions();
CassandraProperties properties = this.properties; CassandraProperties properties = this.properties;
if (properties.getConsistencyLevel() != null) { map.from(properties::getConsistencyLevel).whenNonNull()
options.setConsistencyLevel(properties.getConsistencyLevel()); .to(options::setConsistencyLevel);
} map.from(properties::getSerialConsistencyLevel).whenNonNull()
if (properties.getSerialConsistencyLevel() != null) { .to(options::setSerialConsistencyLevel);
options.setSerialConsistencyLevel(properties.getSerialConsistencyLevel()); map.from(properties::getFetchSize).to(options::setFetchSize);
}
options.setFetchSize(properties.getFetchSize());
return options; return options;
} }
private SocketOptions getSocketOptions() { private SocketOptions getSocketOptions() {
PropertyMapper map = PropertyMapper.get();
SocketOptions options = new SocketOptions(); SocketOptions options = new SocketOptions();
if (this.properties.getConnectTimeout() != null) { map.from(this.properties::getConnectTimeout).whenNonNull()
options.setConnectTimeoutMillis( .asInt(Duration::toMillis).to(options::setConnectTimeoutMillis);
(int) this.properties.getConnectTimeout().toMillis()); map.from(this.properties::getReadTimeout).whenNonNull().asInt(Duration::toMillis)
} .to(options::setReadTimeoutMillis);
if (this.properties.getReadTimeout() != null) {
options.setReadTimeoutMillis(
(int) this.properties.getReadTimeout().toMillis());
}
return options; return options;
} }
private PoolingOptions getPoolingOptions() { private PoolingOptions getPoolingOptions() {
CassandraProperties.Pool pool = this.properties.getPool(); PropertyMapper map = PropertyMapper.get();
CassandraProperties.Pool properties = this.properties.getPool();
PoolingOptions options = new PoolingOptions(); PoolingOptions options = new PoolingOptions();
if (pool.getIdleTimeout() != null) { map.from(properties::getIdleTimeout).whenNonNull().asInt(Duration::getSeconds)
options.setIdleTimeoutSeconds((int) pool.getIdleTimeout().getSeconds()); .to(options::setIdleTimeoutSeconds);
} map.from(properties::getPoolTimeout).whenNonNull().asInt(Duration::toMillis)
if (pool.getPoolTimeout() != null) { .to(options::setPoolTimeoutMillis);
options.setPoolTimeoutMillis((int) pool.getPoolTimeout().toMillis()); map.from(properties::getHeartbeatInterval).whenNonNull()
} .asInt(Duration::getSeconds).to(options::setHeartbeatIntervalSeconds);
if (pool.getHeartbeatInterval() != null) { map.from(properties::getMaxQueueSize).to(options::setMaxQueueSize);
options.setHeartbeatIntervalSeconds(
(int) pool.getHeartbeatInterval().getSeconds());
}
options.setMaxQueueSize(pool.getMaxQueueSize());
return options; return options;
} }
......
...@@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; ...@@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScanPackages; import org.springframework.boot.autoconfigure.domain.EntityScanPackages;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -46,7 +47,6 @@ import org.springframework.data.cassandra.core.convert.CassandraCustomConversion ...@@ -46,7 +47,6 @@ import org.springframework.data.cassandra.core.convert.CassandraCustomConversion
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter; import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext; import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver; import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
import org.springframework.util.StringUtils;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Cassandra support. * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Cassandra support.
...@@ -92,14 +92,16 @@ public class CassandraDataAutoConfiguration { ...@@ -92,14 +92,16 @@ public class CassandraDataAutoConfiguration {
if (!packages.isEmpty()) { if (!packages.isEmpty()) {
context.setInitialEntitySet(CassandraEntityClassScanner.scan(packages)); context.setInitialEntitySet(CassandraEntityClassScanner.scan(packages));
} }
if (StringUtils.hasText(this.properties.getKeyspaceName())) { PropertyMapper.get().from(this.properties::getKeyspaceName).whenHasText()
context.setUserTypeResolver(new SimpleUserTypeResolver(this.cluster, .as(this::createSimpleUserTypeResolver).to(context::setUserTypeResolver);
this.properties.getKeyspaceName()));
}
context.setCustomConversions(conversions); context.setCustomConversions(conversions);
return context; return context;
} }
private SimpleUserTypeResolver createSimpleUserTypeResolver(String keyspaceName) {
return new SimpleUserTypeResolver(this.cluster, keyspaceName);
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public CassandraConverter cassandraConverter(CassandraMappingContext mapping, public CassandraConverter cassandraConverter(CassandraMappingContext mapping,
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.data.rest; package org.springframework.boot.autoconfigure.data.rest;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies; import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -176,40 +177,19 @@ public class RepositoryRestProperties { ...@@ -176,40 +177,19 @@ public class RepositoryRestProperties {
this.enableEnumTranslation = enableEnumTranslation; this.enableEnumTranslation = enableEnumTranslation;
} }
public void applyTo(RepositoryRestConfiguration configuration) { public void applyTo(RepositoryRestConfiguration rest) {
if (this.basePath != null) { PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
configuration.setBasePath(this.basePath); map.from(this::getBasePath).to(rest::setBasePath);
} map.from(this::getDefaultPageSize).to(rest::setDefaultPageSize);
if (this.defaultPageSize != null) { map.from(this::getMaxPageSize).to(rest::setMaxPageSize);
configuration.setDefaultPageSize(this.defaultPageSize); map.from(this::getPageParamName).to(rest::setPageParamName);
} map.from(this::getLimitParamName).to(rest::setLimitParamName);
if (this.maxPageSize != null) { map.from(this::getSortParamName).to(rest::setSortParamName);
configuration.setMaxPageSize(this.maxPageSize); map.from(this::getDetectionStrategy).to(rest::setRepositoryDetectionStrategy);
} map.from(this::getDefaultMediaType).to(rest::setDefaultMediaType);
if (this.pageParamName != null) { map.from(this::getReturnBodyOnCreate).to(rest::setReturnBodyOnCreate);
configuration.setPageParamName(this.pageParamName); map.from(this::getReturnBodyOnUpdate).to(rest::setReturnBodyOnUpdate);
} map.from(this::getEnableEnumTranslation).to(rest::setEnableEnumTranslation);
if (this.limitParamName != null) {
configuration.setLimitParamName(this.limitParamName);
}
if (this.sortParamName != null) {
configuration.setSortParamName(this.sortParamName);
}
if (this.detectionStrategy != null) {
configuration.setRepositoryDetectionStrategy(this.detectionStrategy);
}
if (this.defaultMediaType != null) {
configuration.setDefaultMediaType(this.defaultMediaType);
}
if (this.returnBodyOnCreate != null) {
configuration.setReturnBodyOnCreate(this.returnBodyOnCreate);
}
if (this.returnBodyOnUpdate != null) {
configuration.setReturnBodyOnUpdate(this.returnBodyOnUpdate);
}
if (this.enableEnumTranslation != null) {
configuration.setEnableEnumTranslation(this.enableEnumTranslation);
}
} }
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.elasticsearch.jest; package org.springframework.boot.autoconfigure.elasticsearch.jest;
import java.time.Duration;
import java.util.List; import java.util.List;
import com.google.gson.Gson; import com.google.gson.Gson;
...@@ -29,12 +30,13 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter; ...@@ -29,12 +30,13 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.elasticsearch.jest.JestProperties.Proxy;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* {@link EnableAutoConfiguration Auto-Configuration} for Jest. * {@link EnableAutoConfiguration Auto-Configuration} for Jest.
...@@ -72,27 +74,20 @@ public class JestAutoConfiguration { ...@@ -72,27 +74,20 @@ public class JestAutoConfiguration {
protected HttpClientConfig createHttpClientConfig() { protected HttpClientConfig createHttpClientConfig() {
HttpClientConfig.Builder builder = new HttpClientConfig.Builder( HttpClientConfig.Builder builder = new HttpClientConfig.Builder(
this.properties.getUris()); this.properties.getUris());
if (StringUtils.hasText(this.properties.getUsername())) { PropertyMapper map = PropertyMapper.get();
builder.defaultCredentials(this.properties.getUsername(), map.from(this.properties::getUsername).whenHasText().to((username) -> builder
this.properties.getPassword()); .defaultCredentials(username, this.properties.getPassword()));
} Proxy proxy = this.properties.getProxy();
String proxyHost = this.properties.getProxy().getHost(); map.from(proxy::getHost).whenHasText().to((host) -> {
if (StringUtils.hasText(proxyHost)) { Assert.notNull(proxy.getPort(), "Proxy port must not be null");
Integer proxyPort = this.properties.getProxy().getPort(); builder.proxy(new HttpHost(host, proxy.getPort()));
Assert.notNull(proxyPort, "Proxy port must not be null"); });
builder.proxy(new HttpHost(proxyHost, proxyPort)); map.from(this.gsonProvider::getIfUnique).whenNonNull().to(builder::gson);
} map.from(this.properties::isMultiThreaded).to(builder::multiThreaded);
Gson gson = this.gsonProvider.getIfUnique(); map.from(this.properties::getConnectionTimeout).whenNonNull()
if (gson != null) { .asInt(Duration::toMillis).to(builder::connTimeout);
builder.gson(gson); map.from(this.properties::getReadTimeout).whenNonNull().asInt(Duration::toMillis)
} .to(builder::readTimeout);
builder.multiThreaded(this.properties.isMultiThreaded());
if (this.properties.getConnectionTimeout() != null) {
builder.connTimeout((int) this.properties.getConnectionTimeout().toMillis());
}
if (this.properties.getReadTimeout() != null) {
builder.readTimeout((int) this.properties.getReadTimeout().toMillis());
}
customize(builder); customize(builder);
return builder.build(); return builder.build();
} }
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.jms; package org.springframework.boot.autoconfigure.jms;
import java.time.Duration;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import javax.jms.Message; import javax.jms.Message;
...@@ -25,7 +27,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; ...@@ -25,7 +27,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jms.JmsProperties.DeliveryMode;
import org.springframework.boot.autoconfigure.jms.JmsProperties.Template;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
...@@ -68,38 +73,31 @@ public class JmsAutoConfiguration { ...@@ -68,38 +73,31 @@ public class JmsAutoConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnSingleCandidate(ConnectionFactory.class) @ConditionalOnSingleCandidate(ConnectionFactory.class)
public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) { public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory); PropertyMapper map = PropertyMapper.get();
jmsTemplate.setPubSubDomain(this.properties.isPubSubDomain()); JmsTemplate template = new JmsTemplate(connectionFactory);
DestinationResolver destinationResolver = this.destinationResolver template.setPubSubDomain(this.properties.isPubSubDomain());
.getIfUnique(); map.from(this.destinationResolver::getIfUnique).whenNonNull()
if (destinationResolver != null) { .to(template::setDestinationResolver);
jmsTemplate.setDestinationResolver(destinationResolver); map.from(this.messageConverter::getIfUnique).whenNonNull()
} .to(template::setMessageConverter);
MessageConverter messageConverter = this.messageConverter.getIfUnique(); mapTemplateProperties(this.properties.getTemplate(), template);
if (messageConverter != null) { return template;
jmsTemplate.setMessageConverter(messageConverter); }
}
JmsProperties.Template template = this.properties.getTemplate(); private void mapTemplateProperties(Template properties, JmsTemplate template) {
if (template.getDefaultDestination() != null) { PropertyMapper map = PropertyMapper.get();
jmsTemplate.setDefaultDestinationName(template.getDefaultDestination()); map.from(properties::getDefaultDestination).whenNonNull()
} .to(template::setDefaultDestinationName);
if (template.getDeliveryDelay() != null) { map.from(properties::getDeliveryDelay).whenNonNull().as(Duration::toMillis)
jmsTemplate.setDeliveryDelay(template.getDeliveryDelay().toMillis()); .to(template::setDeliveryDelay);
} map.from(properties::determineQosEnabled).to(template::setExplicitQosEnabled);
jmsTemplate.setExplicitQosEnabled(template.determineQosEnabled()); map.from(properties::getDeliveryMode).whenNonNull().as(DeliveryMode::getValue)
if (template.getDeliveryMode() != null) { .to(template::setDeliveryMode);
jmsTemplate.setDeliveryMode(template.getDeliveryMode().getValue()); map.from(properties::getPriority).whenNonNull().to(template::setPriority);
} map.from(properties::getTimeToLive).whenNonNull().as(Duration::toMillis)
if (template.getPriority() != null) { .to(template::setTimeToLive);
jmsTemplate.setPriority(template.getPriority()); map.from(properties::getReceiveTimeout).whenNonNull().as(Duration::toMillis)
} .to(template::setReceiveTimeout);
if (template.getTimeToLive() != null) {
jmsTemplate.setTimeToLive(template.getTimeToLive().toMillis());
}
if (template.getReceiveTimeout() != null) {
jmsTemplate.setReceiveTimeout(template.getReceiveTimeout().toMillis());
}
return jmsTemplate;
} }
} }
......
...@@ -16,7 +16,10 @@ ...@@ -16,7 +16,10 @@
package org.springframework.boot.autoconfigure.kafka; package org.springframework.boot.autoconfigure.kafka;
import java.time.Duration;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Listener; import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Listener;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory; import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.KafkaTemplate;
...@@ -65,41 +68,39 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer { ...@@ -65,41 +68,39 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {
/** /**
* Configure the specified Kafka listener container factory. The factory can be * Configure the specified Kafka listener container factory. The factory can be
* further tuned and default settings can be overridden. * further tuned and default settings can be overridden.
* @param listenerContainerFactory the {@link ConcurrentKafkaListenerContainerFactory} * @param listenerFactory the {@link ConcurrentKafkaListenerContainerFactory} instance
* instance to configure * to configure
* @param consumerFactory the {@link ConsumerFactory} to use * @param consumerFactory the {@link ConsumerFactory} to use
*/ */
public void configure( public void configure(
ConcurrentKafkaListenerContainerFactory<Object, Object> listenerContainerFactory, ConcurrentKafkaListenerContainerFactory<Object, Object> listenerFactory,
ConsumerFactory<Object, Object> consumerFactory) { ConsumerFactory<Object, Object> consumerFactory) {
listenerContainerFactory.setConsumerFactory(consumerFactory); listenerFactory.setConsumerFactory(consumerFactory);
if (this.messageConverter != null) { configureListenerFactory(listenerFactory);
listenerContainerFactory.setMessageConverter(this.messageConverter); configureContainer(listenerFactory.getContainerProperties());
} }
if (this.replyTemplate != null) {
listenerContainerFactory.setReplyTemplate(this.replyTemplate); private void configureListenerFactory(
} ConcurrentKafkaListenerContainerFactory<Object, Object> factory) {
Listener container = this.properties.getListener(); PropertyMapper map = PropertyMapper.get();
ContainerProperties containerProperties = listenerContainerFactory Listener properties = this.properties.getListener();
.getContainerProperties(); map.from(properties::getConcurrency).whenNonNull().to(factory::setConcurrency);
if (container.getAckMode() != null) { map.from(() -> this.messageConverter).whenNonNull()
containerProperties.setAckMode(container.getAckMode()); .to(factory::setMessageConverter);
} map.from(() -> this.replyTemplate).whenNonNull().to(factory::setReplyTemplate);
if (container.getAckCount() != null) { map.from(properties::getType).whenEqualTo(Listener.Type.BATCH)
containerProperties.setAckCount(container.getAckCount()); .toCall(() -> factory.setBatchListener(true));
} }
if (container.getAckTime() != null) {
containerProperties.setAckTime(container.getAckTime().toMillis()); private void configureContainer(ContainerProperties container) {
} PropertyMapper map = PropertyMapper.get();
if (container.getPollTimeout() != null) { Listener properties = this.properties.getListener();
containerProperties.setPollTimeout(container.getPollTimeout().toMillis()); map.from(properties::getAckMode).whenNonNull().to(container::setAckMode);
} map.from(properties::getAckCount).whenNonNull().to(container::setAckCount);
if (container.getConcurrency() != null) { map.from(properties::getAckTime).whenNonNull().as(Duration::toMillis)
listenerContainerFactory.setConcurrency(container.getConcurrency()); .to(container::setAckTime);
} map.from(properties::getPollTimeout).whenNonNull().as(Duration::toMillis)
if (container.getType() == Listener.Type.BATCH) { .to(container::setPollTimeout);
listenerContainerFactory.setBatchListener(true);
}
} }
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.mail; package org.springframework.boot.autoconfigure.mail;
import java.nio.charset.Charset;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
...@@ -31,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -31,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration.MailSenderCondition; import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration.MailSenderCondition;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -77,19 +79,16 @@ public class MailSenderAutoConfiguration { ...@@ -77,19 +79,16 @@ public class MailSenderAutoConfiguration {
} }
private void applyProperties(JavaMailSenderImpl sender) { private void applyProperties(JavaMailSenderImpl sender) {
sender.setHost(this.properties.getHost()); PropertyMapper map = PropertyMapper.get();
if (this.properties.getPort() != null) { map.from(this.properties::getHost).to(sender::setHost);
sender.setPort(this.properties.getPort()); map.from(this.properties::getPort).whenNonNull().to(sender::setPort);
} map.from(this.properties::getUsername).to(sender::setUsername);
sender.setUsername(this.properties.getUsername()); map.from(this.properties::getPassword).to(sender::setPassword);
sender.setPassword(this.properties.getPassword()); map.from(this.properties::getProtocol).to(sender::setProtocol);
sender.setProtocol(this.properties.getProtocol()); map.from(this.properties::getDefaultEncoding).whenNonNull().as(Charset::name)
if (this.properties.getDefaultEncoding() != null) { .to(sender::setDefaultEncoding);
sender.setDefaultEncoding(this.properties.getDefaultEncoding().name()); map.from(this.properties::getProperties).whenNot(Map::isEmpty)
} .as(this::asProperties).to(sender::setJavaMailProperties);
if (!this.properties.getProperties().isEmpty()) {
sender.setJavaMailProperties(asProperties(this.properties.getProperties()));
}
} }
private Properties asProperties(Map<String, String> source) { private Properties asProperties(Map<String, String> source) {
......
...@@ -48,13 +48,10 @@ class RedisReactiveSessionConfiguration { ...@@ -48,13 +48,10 @@ class RedisReactiveSessionConfiguration {
static class SpringBootRedisWebSessionConfiguration static class SpringBootRedisWebSessionConfiguration
extends RedisWebSessionConfiguration { extends RedisWebSessionConfiguration {
private SessionProperties sessionProperties;
@Autowired @Autowired
public void customize(SessionProperties sessionProperties, public void customize(SessionProperties sessionProperties,
RedisSessionProperties redisSessionProperties) { RedisSessionProperties redisSessionProperties) {
this.sessionProperties = sessionProperties; Duration timeout = sessionProperties.getTimeout();
Duration timeout = this.sessionProperties.getTimeout();
if (timeout != null) { if (timeout != null) {
setMaxInactiveIntervalInSeconds((int) timeout.getSeconds()); setMaxInactiveIntervalInSeconds((int) timeout.getSeconds());
} }
......
...@@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.thymeleaf; ...@@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.thymeleaf;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
...@@ -48,15 +47,16 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; ...@@ -48,15 +47,16 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.template.TemplateLocation; import org.springframework.boot.autoconfigure.template.TemplateLocation;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Reactive;
import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain; import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
...@@ -263,27 +263,34 @@ public class ThymeleafAutoConfiguration { ...@@ -263,27 +263,34 @@ public class ThymeleafAutoConfiguration {
ISpringWebFluxTemplateEngine templateEngine) { ISpringWebFluxTemplateEngine templateEngine) {
ThymeleafReactiveViewResolver resolver = new ThymeleafReactiveViewResolver(); ThymeleafReactiveViewResolver resolver = new ThymeleafReactiveViewResolver();
resolver.setTemplateEngine(templateEngine); resolver.setTemplateEngine(templateEngine);
resolver.setDefaultCharset(this.properties.getEncoding()); mapProperties(this.properties, resolver);
List<MediaType> mediaTypes = this.properties.getReactive().getMediaTypes(); mapReactiveProperties(this.properties.getReactive(), resolver);
if (mediaTypes != null) {
resolver.setSupportedMediaTypes(mediaTypes);
}
resolver.setExcludedViewNames(this.properties.getExcludedViewNames());
resolver.setViewNames(this.properties.getViewNames());
if (this.properties.getReactive().getMaxChunkSize() > 0) {
resolver.setResponseMaxChunkSizeBytes(
this.properties.getReactive().getMaxChunkSize());
}
resolver.setFullModeViewNames(
this.properties.getReactive().getFullModeViewNames());
resolver.setChunkedModeViewNames(
this.properties.getReactive().getChunkedModeViewNames());
// This resolver acts as a fallback resolver (e.g. like a // This resolver acts as a fallback resolver (e.g. like a
// InternalResourceViewResolver) so it needs to have low precedence // InternalResourceViewResolver) so it needs to have low precedence
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5); resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
return resolver; return resolver;
} }
private void mapProperties(ThymeleafProperties properties,
ThymeleafReactiveViewResolver resolver) {
PropertyMapper map = PropertyMapper.get();
map.from(properties::getEncoding).to(resolver::setDefaultCharset);
resolver.setExcludedViewNames(properties.getExcludedViewNames());
resolver.setViewNames(properties.getViewNames());
}
private void mapReactiveProperties(Reactive properties,
ThymeleafReactiveViewResolver resolver) {
PropertyMapper map = PropertyMapper.get();
map.from(properties::getMediaTypes).whenNonNull()
.to(resolver::setSupportedMediaTypes);
map.from(properties::getMaxChunkSize).when((size) -> size > 0)
.to(resolver::setResponseMaxChunkSizeBytes);
map.from(properties::getFullModeViewNames).to(resolver::setFullModeViewNames);
map.from(properties::getChunkedModeViewNames)
.to(resolver::setChunkedModeViewNames);
}
} }
@Configuration @Configuration
......
...@@ -19,9 +19,9 @@ package org.springframework.boot.autoconfigure.web; ...@@ -19,9 +19,9 @@ package org.springframework.boot.autoconfigure.web;
import java.time.Duration; import java.time.Duration;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.context.properties.bind.convert.DefaultDurationUnit; import org.springframework.boot.context.properties.bind.convert.DefaultDurationUnit;
/** /**
...@@ -459,29 +459,23 @@ public class ResourceProperties { ...@@ -459,29 +459,23 @@ public class ResourceProperties {
} }
public org.springframework.http.CacheControl toHttpCacheControl() { public org.springframework.http.CacheControl toHttpCacheControl() {
org.springframework.http.CacheControl cacheControl = createCacheControl(); PropertyMapper map = PropertyMapper.get();
callIfTrue(this.mustRevalidate, cacheControl, org.springframework.http.CacheControl control = createCacheControl();
org.springframework.http.CacheControl::mustRevalidate); map.from(this::getMustRevalidate).whenTrue()
callIfTrue(this.noTransform, cacheControl, .toCall(control::mustRevalidate);
org.springframework.http.CacheControl::noTransform); map.from(this::getNoTransform).whenTrue().toCall(control::noTransform);
callIfTrue(this.cachePublic, cacheControl, map.from(this::getCachePublic).whenTrue().toCall(control::cachePublic);
org.springframework.http.CacheControl::cachePublic); map.from(this::getCachePrivate).whenTrue().toCall(control::cachePrivate);
callIfTrue(this.cachePrivate, cacheControl, map.from(this::getProxyRevalidate).whenTrue()
org.springframework.http.CacheControl::cachePrivate); .toCall(control::proxyRevalidate);
callIfTrue(this.proxyRevalidate, cacheControl, map.from(this::getStaleWhileRevalidate).whenNonNull().to(
org.springframework.http.CacheControl::proxyRevalidate); (duration) -> control.staleWhileRevalidate(duration.getSeconds(),
if (this.staleWhileRevalidate != null) { TimeUnit.SECONDS));
cacheControl.staleWhileRevalidate( map.from(this::getStaleIfError).whenNonNull().to((duration) -> control
this.staleWhileRevalidate.getSeconds(), TimeUnit.SECONDS); .staleIfError(duration.getSeconds(), TimeUnit.SECONDS));
} map.from(this::getSMaxAge).whenNonNull().to((duration) -> control
if (this.staleIfError != null) { .sMaxAge(duration.getSeconds(), TimeUnit.SECONDS));
cacheControl.staleIfError(this.staleIfError.getSeconds(), return control;
TimeUnit.SECONDS);
}
if (this.sMaxAge != null) {
cacheControl.sMaxAge(this.sMaxAge.getSeconds(), TimeUnit.SECONDS);
}
return cacheControl;
} }
private org.springframework.http.CacheControl createCacheControl() { private org.springframework.http.CacheControl createCacheControl() {
...@@ -498,12 +492,6 @@ public class ResourceProperties { ...@@ -498,12 +492,6 @@ public class ResourceProperties {
return org.springframework.http.CacheControl.empty(); return org.springframework.http.CacheControl.empty();
} }
private <T> void callIfTrue(Boolean property, T instance, Consumer<T> call) {
if (Boolean.TRUE.equals(property)) {
call.accept(instance);
}
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment