I have some code that works properly on spring boot prior to version 2 and I find it hard to convert it to work with spring boot 2.
Can somebody assist?
yml file:
spring:
datasource:
type: org.apache.tomcat.jdbc.pool.DataSource
tomcat:
initial-size: 10
max-active: 20
max-idle: 10
max-wait: 10000
Old code:
DataSource dataSource = builder.build();
Map<String, Object> properties = null;
try {
RelaxedPropertyResolver relaxedProperty = new RelaxedPropertyResolver(environment);
properties = relaxedProperty
.getSubProperties("spring.datasource.tomcat.");
} catch (Exception e) {
// No need to log an error
}
MutablePropertyValues mutableProperties = new MutablePropertyValues(properties);
if(properties == null || properties.isEmpty()) {
return dataSource;
}
new RelaxedDataBinder(dataSource).bind(mutableProperties);
return dataSource;
New Code with version 2:
DataSourceBuilder<?> dataSourceBuilder =
DataSourceBuilder.create(this.getClass().getClassLoader());
javax.sql.DataSource dataSource = dataSourceBuilder.build();
Map<String, Object> properties = null;
try {
properties = Binder.get(environment)
.bind("spring.datasource.tomcat", Bindable.mapOf(String.class, Object.class)).orElseGet(Collections::emptyMap);
} catch (Exception e) {
// No need to log an error
}
if(properties == null || properties.isEmpty()) {
return dataSource;
}
return Binder.get(environment)
.bind("spring.datasource.tomcat", DataSource.class).get();
I am getting below error while binding data to DataSource.class. When I replace datasource to poolProperties class, it will able to bind but I am not able to bind this datasource.
Binder.get(environment)
.bind("spring.datasource.tomcat", PoolProperties.class).get();:
Error:
java.util.NoSuchElementException: No value bound
at org.springframework.boot.context.properties.bind.BindResult.get(BindResult.java:56)