I need to create a DataSource at runtime. I want to have default properties like Hikaru connection properties and upon that be able to specify username, password, driver and url when creating the DataSource bean.
That's what I'm trying now:
@Configuration
public class DataSourceFactory{
@Bean(autowireCandidate = false)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource createDataSource(Properties properties) {
DataSource dataSource = DataSourceBuilder
.create()
.url(properties.getUrl())
.driverClassName(properties.getDriverClassName())
.username(properties.getUsername())
.password(properties.getPassword())
.build();
return dataSource;
}
}
And I'd like to use it like that:
@Component
public class A {
@Autowire
DataSourceFactory dataSourceFactory;
void newDatasource(Properties properties) {
dataSourceFactory.createDataSource(properties);
// do sth else here
}
}
Unfortunately this gives me an error:
Parameter 0 of method createDataSource in DataSourceFactory required a bean of type Properties that could not be found.
I think autowireCandidate = false
should deal with that but unfortunately not.
Any ideas?