Skip to content

Commit 5e9cfea

Browse files
committed
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
1 parent 241a708 commit 5e9cfea

16 files changed

+295
-362
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/DirectRabbitListenerContainerFactoryConfigurer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory;
2020
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
21+
import org.springframework.boot.context.properties.PropertyMapper;
2122

2223
/**
2324
* Configure {@link DirectRabbitListenerContainerFactoryConfigurer} with sensible
@@ -33,12 +34,12 @@ public final class DirectRabbitListenerContainerFactoryConfigurer extends
3334
@Override
3435
public void configure(DirectRabbitListenerContainerFactory factory,
3536
ConnectionFactory connectionFactory) {
37+
PropertyMapper map = PropertyMapper.get();
3638
RabbitProperties.DirectContainer config = getRabbitProperties().getListener()
3739
.getDirect();
3840
configure(factory, connectionFactory, config);
39-
if (config.getConsumersPerQueue() != null) {
40-
factory.setConsumersPerQueue(config.getConsumersPerQueue());
41-
}
41+
map.from(config::getConsumersPerQueue).whenNonNull()
42+
.to(factory::setConsumersPerQueue);
4243
}
4344

4445
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java

Lines changed: 68 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.amqp;
1818

19+
import java.time.Duration;
20+
1921
import com.rabbitmq.client.Channel;
2022

2123
import org.springframework.amqp.core.AmqpAdmin;
@@ -33,6 +35,7 @@
3335
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3436
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
3537
import org.springframework.boot.context.properties.EnableConfigurationProperties;
38+
import org.springframework.boot.context.properties.PropertyMapper;
3639
import org.springframework.context.annotation.Bean;
3740
import org.springframework.context.annotation.Configuration;
3841
import org.springframework.context.annotation.Import;
@@ -76,6 +79,7 @@
7679
* @author Josh Long
7780
* @author Stephane Nicoll
7881
* @author Gary Russell
82+
* @author Phillip Webb
7983
*/
8084
@Configuration
8185
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@@ -88,66 +92,55 @@ public class RabbitAutoConfiguration {
8892
protected static class RabbitConnectionFactoryCreator {
8993

9094
@Bean
91-
public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config)
92-
throws Exception {
95+
public CachingConnectionFactory rabbitConnectionFactory(
96+
RabbitProperties properties) throws Exception {
97+
PropertyMapper map = PropertyMapper.get();
98+
CachingConnectionFactory factory = new CachingConnectionFactory(
99+
getRabbitConnectionFactoryBean(properties).getObject());
100+
map.from(properties::determineAddresses).to(factory::setAddresses);
101+
map.from(properties::isPublisherConfirms).to(factory::setPublisherConfirms);
102+
map.from(properties::isPublisherReturns).to(factory::setPublisherReturns);
103+
RabbitProperties.Cache.Channel channel = properties.getCache().getChannel();
104+
map.from(channel::getSize).whenNonNull().to(factory::setChannelCacheSize);
105+
map.from(channel::getCheckoutTimeout).whenNonNull()
106+
.to(factory::setChannelCheckoutTimeout);
107+
RabbitProperties.Cache.Connection connection = properties.getCache()
108+
.getConnection();
109+
map.from(connection::getMode).whenNonNull().to(factory::setCacheMode);
110+
map.from(connection::getSize).whenNonNull()
111+
.to(factory::setConnectionCacheSize);
112+
return factory;
113+
}
114+
115+
private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean(
116+
RabbitProperties properties) throws Exception {
117+
PropertyMapper map = PropertyMapper.get();
93118
RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean();
94-
if (config.determineHost() != null) {
95-
factory.setHost(config.determineHost());
96-
}
97-
factory.setPort(config.determinePort());
98-
if (config.determineUsername() != null) {
99-
factory.setUsername(config.determineUsername());
100-
}
101-
if (config.determinePassword() != null) {
102-
factory.setPassword(config.determinePassword());
103-
}
104-
if (config.determineVirtualHost() != null) {
105-
factory.setVirtualHost(config.determineVirtualHost());
106-
}
107-
if (config.getRequestedHeartbeat() != null) {
108-
factory.setRequestedHeartbeat(
109-
(int) config.getRequestedHeartbeat().getSeconds());
110-
}
111-
RabbitProperties.Ssl ssl = config.getSsl();
119+
map.from(properties::determineHost).whenNonNull().to(factory::setHost);
120+
map.from(properties::determinePort).to(factory::setPort);
121+
map.from(properties::determineUsername).whenNonNull()
122+
.to(factory::setUsername);
123+
map.from(properties::determinePassword).whenNonNull()
124+
.to(factory::setPassword);
125+
map.from(properties::determineVirtualHost).whenNonNull()
126+
.to(factory::setVirtualHost);
127+
map.from(properties::getRequestedHeartbeat).whenNonNull()
128+
.asInt(Duration::getSeconds).to(factory::setRequestedHeartbeat);
129+
RabbitProperties.Ssl ssl = properties.getSsl();
112130
if (ssl.isEnabled()) {
113131
factory.setUseSSL(true);
114-
if (ssl.getAlgorithm() != null) {
115-
factory.setSslAlgorithm(ssl.getAlgorithm());
116-
}
117-
factory.setKeyStoreType(ssl.getKeyStoreType());
118-
factory.setKeyStore(ssl.getKeyStore());
119-
factory.setKeyStorePassphrase(ssl.getKeyStorePassword());
120-
factory.setTrustStoreType(ssl.getTrustStoreType());
121-
factory.setTrustStore(ssl.getTrustStore());
122-
factory.setTrustStorePassphrase(ssl.getTrustStorePassword());
123-
}
124-
if (config.getConnectionTimeout() != null) {
125-
factory.setConnectionTimeout(
126-
(int) config.getConnectionTimeout().toMillis());
132+
map.from(ssl::getAlgorithm).whenNonNull().to(factory::setSslAlgorithm);
133+
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);
134+
map.from(ssl::getKeyStore).to(factory::setKeyStore);
135+
map.from(ssl::getKeyStorePassword).to(factory::setKeyStorePassphrase);
136+
map.from(ssl::getTrustStoreType).to(factory::setTrustStoreType);
137+
map.from(ssl::getTrustStore).to(factory::setTrustStore);
138+
map.from(ssl::getTrustStorePassword).to(factory::setTrustStorePassphrase);
127139
}
140+
map.from(properties::getConnectionTimeout).whenNonNull()
141+
.asInt(Duration::toMillis).to(factory::setConnectionTimeout);
128142
factory.afterPropertiesSet();
129-
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
130-
factory.getObject());
131-
connectionFactory.setAddresses(config.determineAddresses());
132-
connectionFactory.setPublisherConfirms(config.isPublisherConfirms());
133-
connectionFactory.setPublisherReturns(config.isPublisherReturns());
134-
if (config.getCache().getChannel().getSize() != null) {
135-
connectionFactory
136-
.setChannelCacheSize(config.getCache().getChannel().getSize());
137-
}
138-
if (config.getCache().getConnection().getMode() != null) {
139-
connectionFactory
140-
.setCacheMode(config.getCache().getConnection().getMode());
141-
}
142-
if (config.getCache().getConnection().getSize() != null) {
143-
connectionFactory.setConnectionCacheSize(
144-
config.getCache().getConnection().getSize());
145-
}
146-
if (config.getCache().getChannel().getCheckoutTimeout() != null) {
147-
connectionFactory.setChannelCheckoutTimeout(
148-
config.getCache().getChannel().getCheckoutTimeout());
149-
}
150-
return connectionFactory;
143+
return factory;
151144
}
152145

153146
}
@@ -171,26 +164,24 @@ public RabbitTemplateConfiguration(
171164
@ConditionalOnSingleCandidate(ConnectionFactory.class)
172165
@ConditionalOnMissingBean(RabbitTemplate.class)
173166
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
174-
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
167+
PropertyMapper map = PropertyMapper.get();
168+
RabbitTemplate template = new RabbitTemplate(connectionFactory);
175169
MessageConverter messageConverter = this.messageConverter.getIfUnique();
176170
if (messageConverter != null) {
177-
rabbitTemplate.setMessageConverter(messageConverter);
178-
}
179-
rabbitTemplate.setMandatory(determineMandatoryFlag());
180-
RabbitProperties.Template templateProperties = this.properties.getTemplate();
181-
RabbitProperties.Retry retryProperties = templateProperties.getRetry();
182-
if (retryProperties.isEnabled()) {
183-
rabbitTemplate.setRetryTemplate(createRetryTemplate(retryProperties));
171+
template.setMessageConverter(messageConverter);
184172
}
185-
if (templateProperties.getReceiveTimeout() != null) {
186-
rabbitTemplate.setReceiveTimeout(templateProperties.getReceiveTimeout());
173+
template.setMandatory(determineMandatoryFlag());
174+
RabbitProperties.Template properties = this.properties.getTemplate();
175+
if (properties.getRetry().isEnabled()) {
176+
template.setRetryTemplate(createRetryTemplate(properties.getRetry()));
187177
}
188-
if (templateProperties.getReplyTimeout() != null) {
189-
rabbitTemplate.setReplyTimeout(templateProperties.getReplyTimeout());
190-
}
191-
rabbitTemplate.setExchange(templateProperties.getExchange());
192-
rabbitTemplate.setRoutingKey(templateProperties.getRoutingKey());
193-
return rabbitTemplate;
178+
map.from(properties::getReceiveTimeout).whenNonNull()
179+
.to(template::setReceiveTimeout);
180+
map.from(properties::getReplyTimeout).whenNonNull()
181+
.to(template::setReplyTimeout);
182+
map.from(properties::getExchange).to(template::setExchange);
183+
map.from(properties::getRoutingKey).to(template::setRoutingKey);
184+
return template;
194185
}
195186

196187
private boolean determineMandatoryFlag() {
@@ -199,14 +190,16 @@ private boolean determineMandatoryFlag() {
199190
}
200191

201192
private RetryTemplate createRetryTemplate(RabbitProperties.Retry properties) {
193+
PropertyMapper map = PropertyMapper.get();
202194
RetryTemplate template = new RetryTemplate();
203195
SimpleRetryPolicy policy = new SimpleRetryPolicy();
204-
policy.setMaxAttempts(properties.getMaxAttempts());
196+
map.from(properties::getMaxAttempts).to(policy::setMaxAttempts);
205197
template.setRetryPolicy(policy);
206198
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
207-
backOffPolicy.setInitialInterval(properties.getInitialInterval());
208-
backOffPolicy.setMultiplier(properties.getMultiplier());
209-
backOffPolicy.setMaxInterval(properties.getMaxInterval());
199+
map.from(properties::getInitialInterval)
200+
.to(backOffPolicy::setInitialInterval);
201+
map.from(properties::getMultiplier).to(backOffPolicy::setMultiplier);
202+
map.from(properties::getMaxInterval).to(backOffPolicy::setMaxInterval);
210203
template.setBackOffPolicy(backOffPolicy);
211204
return template;
212205
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/SimpleRabbitListenerContainerFactoryConfigurer.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
2020
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
21+
import org.springframework.boot.context.properties.PropertyMapper;
2122

2223
/**
2324
* Configure {@link SimpleRabbitListenerContainerFactoryConfigurer} with sensible
@@ -33,18 +34,15 @@ public final class SimpleRabbitListenerContainerFactoryConfigurer extends
3334
@Override
3435
public void configure(SimpleRabbitListenerContainerFactory factory,
3536
ConnectionFactory connectionFactory) {
37+
PropertyMapper map = PropertyMapper.get();
3638
RabbitProperties.SimpleContainer config = getRabbitProperties().getListener()
3739
.getSimple();
3840
configure(factory, connectionFactory, config);
39-
if (config.getConcurrency() != null) {
40-
factory.setConcurrentConsumers(config.getConcurrency());
41-
}
42-
if (config.getMaxConcurrency() != null) {
43-
factory.setMaxConcurrentConsumers(config.getMaxConcurrency());
44-
}
45-
if (config.getTransactionSize() != null) {
46-
factory.setTxSize(config.getTransactionSize());
47-
}
41+
map.from(config::getConcurrency).whenNonNull()
42+
.to(factory::setConcurrentConsumers);
43+
map.from(config::getMaxConcurrency).whenNonNull()
44+
.to(factory::setMaxConcurrentConsumers);
45+
map.from(config::getTransactionSize).whenNonNull().to(factory::setTxSize);
4846
}
4947

5048
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import org.springframework.batch.core.repository.JobRepository;
2828
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
2929
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
30+
import org.springframework.boot.context.properties.PropertyMapper;
3031
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3132
import org.springframework.transaction.PlatformTransactionManager;
32-
import org.springframework.util.StringUtils;
3333

3434
/**
3535
* Basic {@link BatchConfigurer} implementation.
@@ -103,14 +103,13 @@ public void initialize() {
103103
}
104104

105105
protected JobExplorer createJobExplorer() throws Exception {
106-
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
107-
jobExplorerFactoryBean.setDataSource(this.dataSource);
108-
String tablePrefix = this.properties.getTablePrefix();
109-
if (StringUtils.hasText(tablePrefix)) {
110-
jobExplorerFactoryBean.setTablePrefix(tablePrefix);
111-
}
112-
jobExplorerFactoryBean.afterPropertiesSet();
113-
return jobExplorerFactoryBean.getObject();
106+
PropertyMapper map = PropertyMapper.get();
107+
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
108+
factory.setDataSource(this.dataSource);
109+
map.from(this.properties::getTablePrefix).whenHasText()
110+
.to(factory::setTablePrefix);
111+
factory.afterPropertiesSet();
112+
return factory.getObject();
114113
}
115114

116115
protected JobLauncher createJobLauncher() throws Exception {
@@ -122,16 +121,13 @@ protected JobLauncher createJobLauncher() throws Exception {
122121

123122
protected JobRepository createJobRepository() throws Exception {
124123
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
125-
factory.setDataSource(this.dataSource);
126-
String isolationLevel = determineIsolationLevel();
127-
if (isolationLevel != null) {
128-
factory.setIsolationLevelForCreate(isolationLevel);
129-
}
130-
String tablePrefix = this.properties.getTablePrefix();
131-
if (StringUtils.hasText(tablePrefix)) {
132-
factory.setTablePrefix(tablePrefix);
133-
}
134-
factory.setTransactionManager(getTransactionManager());
124+
PropertyMapper map = PropertyMapper.get();
125+
map.from(() -> this.dataSource).to(factory::setDataSource);
126+
map.from(this::determineIsolationLevel).whenNonNull()
127+
.to(factory::setIsolationLevelForCreate);
128+
map.from(this.properties::getTablePrefix).whenHasText()
129+
.to(factory::setTablePrefix);
130+
map.from(this::getTransactionManager).to(factory::setTransactionManager);
135131
factory.afterPropertiesSet();
136132
return factory.getObject();
137133
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CouchbaseCacheConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import com.couchbase.client.spring.cache.CacheBuilder;
2424
import com.couchbase.client.spring.cache.CouchbaseCacheManager;
2525

26+
import org.springframework.boot.autoconfigure.cache.CacheProperties.Couchbase;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2728
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2829
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
30+
import org.springframework.boot.context.properties.PropertyMapper;
2931
import org.springframework.cache.CacheManager;
3032
import org.springframework.context.annotation.Bean;
3133
import org.springframework.context.annotation.Conditional;
@@ -61,10 +63,9 @@ public CouchbaseCacheConfiguration(CacheProperties cacheProperties,
6163
public CouchbaseCacheManager cacheManager() {
6264
List<String> cacheNames = this.cacheProperties.getCacheNames();
6365
CacheBuilder builder = CacheBuilder.newInstance(this.bucket);
64-
Duration expiration = this.cacheProperties.getCouchbase().getExpiration();
65-
if (expiration != null) {
66-
builder = builder.withExpiration((int) expiration.getSeconds());
67-
}
66+
Couchbase couchbase = this.cacheProperties.getCouchbase();
67+
PropertyMapper.get().from(couchbase::getExpiration).whenNonNull()
68+
.asInt(Duration::getSeconds).to(builder::withExpiration);
6869
String[] names = cacheNames.toArray(new String[cacheNames.size()]);
6970
CouchbaseCacheManager cacheManager = new CouchbaseCacheManager(builder, names);
7071
return this.customizers.customize(cacheManager);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ public EmbeddedCacheManager infinispanCacheManager() throws IOException {
7777
EmbeddedCacheManager cacheManager = createEmbeddedCacheManager();
7878
List<String> cacheNames = this.cacheProperties.getCacheNames();
7979
if (!CollectionUtils.isEmpty(cacheNames)) {
80-
for (String cacheName : cacheNames) {
81-
cacheManager.defineConfiguration(cacheName,
82-
getDefaultCacheConfiguration());
83-
}
80+
cacheNames.forEach((cacheName) -> cacheManager.defineConfiguration(cacheName,
81+
getDefaultCacheConfiguration()));
8482
}
8583
return cacheManager;
8684
}

0 commit comments

Comments
 (0)