I want add new datasource in the runtime,but now ,it's so hard
Comment From: wilkinsona
@crazyweeds There's not much we can do to help you without more information. What, exactly, did you try to do and why was it hard? Have you read the documentation about configuring a DataSource, configuring a custom DataSource, and configuring two DataSources?
Comment From: ztomic
@crazyweeds You can try something like this with prototype beans (maybe this is not best approach but it should work):
```java @Bean(name = "runtimeDataSource") @ConfigurationProperties("runtime.datasource") @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Lazy public HikariDataSource runtimeDataSource(String url, String username, String password) { DataSourceBuilder factory = DataSourceBuilder .create() .type(HikariDataSource.class) .url(url) .username(username) .password(password); return (HikariDataSource) factory.build(); }
Then, to retrieve new instance of DataSource with specified url, username and password (and also with Spring Boot's binding of configuration properties prefixed with `runtime.datasource`)
````java
ApplicationContext context = ...
DataSource dataSource = (DataSource) context.getBean("runtimeDataSource", url, username, password);
But with this method you should not use actuator, or you should do some "fine tuning" because DataSourceHealthIndicatorAutoConfiguration
will also try to instantiate this DataSource and it will fail with exception:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Comment From: crazyweeds
What I mean is that after the project has started, I can add or modify data sources, in addition to the default data source. I have tried many ways and none of them succeeded, although some people did succeed.
Comment From: wilkinsona
@crazyweeds Thanks for the clarification. Unfortunately this isn't the right place to ask questions such as this. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Please feel free to update this issue with a link to the re-posted question (so that other people can find it). You may also want to chat with the Spring Boot community on Gitter.
Comment From: crazyweeds
Ths,I am so sorry. @wilkinsona
Comment From: ananbeike
@crazyweeds Is there any progress on this issue? Can you share the solution with me? Although it's been a long time。TKS