I have a Spring Boot (batch-oriented) application that uses a datasource in order to finalize a batch job and write stuff to the database.
I have the datasource(s) defined inside the application.yml like:
spring:
datasource:
url: jdbc:h2:mem:JavaSpringBootBatch
username: sa
password:
profiles: # default, development, production
active: default, development
---
spring:
h2:
# ...config/settings here
profiles: development
---
spring:
datasource:
# ...datasource config here
profiles: production
The issue is when I try to inject the datasource into one of the Spring config files:
@Configuration
public class PlayerBatchConfig {
...
@Bean
public ItemWriter<Player> writer(final DataSource dataSource) {
final JdbcBatchItemWriter<Player> jdbcItemWriter = new JdbcBatchItemWriter<>();
...
jdbcItemWriter.setDataSource(dataSource);
jdbcItemWriter.setSql(sql.toString());
return jdbcItemWriter;
}
}
...it tells me that:
Could not autowire. There is more than one bean of 'DataSource' type.
Beans: dataSource (DataSourceConfiguration.class) dataSource (EmbeddedDataSourceConfiguration.class)
I also tried to inject the datasource like:
@Configuration
public class PlayerBatchConfig {
@Bean
@ConfigurationProperties(prefix = "datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
...
}
...but no luck :( , although the issue with the two datasources goes away eventually.
Any clues how to "circumvent" that?
defaultprofile chunk. That only let you access the H2 console on whendevelopmentis used by browsinghttp://.../h2-console- x80486