1

I want to generate objects based on the yaml file and inject it into the Spring container (like instantiating multiple DataSourceTransactionManager). The @Bean annotation does not meet the requirements. I can't get the configuration information to implement ImportBeanDefinitionRegistrar because he needs a parameterless constructor. What should I do?

    @Bean("aaaTransactionManager")
public DataSourceTransactionManager get (    @Autowired @Qualifier("aaaDataSource")DataSource build){
    return new DataSourceTransactionManager(build);
}
@Bean("cccTransactionManager")
public DataSourceTransactionManager get2 (    @Autowired @Qualifier("cccDataSource")DataSource build){
    return new DataSourceTransactionManager(build);
}

How do I dynamically generate the above DataSourceTransactionManager, don't care about the number

munan
  • 113
  • 1
  • 6
  • Hi @munan, welcome to Stackoverflow. Can this https://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources help solve your problem? – artfulbeest Jun 05 '20 at 16:01
  • Hi @artfulbeest ,These examples are correct, but not very useful to me. Because I am not sure how many datasources there are, so I may not be able to use Bean Annotation – munan Jun 08 '20 at 06:33
  • So your need is to create datasources dynamically, which might not be know at build time? – artfulbeest Jun 08 '20 at 09:30
  • Yes,maybe I need to implement ImportBeanDefinitionRegistrar or BeanDefinitionRegistryPostProcessor,what do you think? – munan Jun 08 '20 at 12:12
  • But you need to know the datasource before using it, meaning datasources should be known at build time..only configurations can change. What is your use case for dynamic datasource? – artfulbeest Jun 08 '20 at 12:51
  • A starter for mybatis. I try to implement ImportBeanDefinitionRegistrar like this .https://github.com/munan56/mutidata-boot-mybatis Now the headache is configuration convert to Java Bean。 – munan Jun 09 '20 at 02:11

0 Answers0