Springboot multiple data sources, the simplest way to integrate


brief introduction

I believe that if you have configured too many data sources or are about to configure multiple data sources, you will find that there are probably the following schemes on the Internet:

  • 1. Use AOP slice for dynamic data source switching
  • 2. UseMapperScanofbasePackagesConfigure different mapper directories and templates
  • 3. Database agent middleware

    Both methods can realize multiple data sources, but each has its own disadvantages:
  • 1. Multiple data sources cannot be realizedXAThings (Global things Management)|JTA)This disadvantage is very fatal. It is useless to have multiple data sources but no global things. There are many posts and tutorials on the Internet. Although the configuration is slightly simple, it is useless if you want to use global things.
  • 2. This method can be combinedJTARealize the overall situation. At present, I also run online in this way in my business. After many debugging and online operation and maintenance, there is no doubt that there is a problem is trouble! You will encounter the need for integrationdruidConnection pool and global things. If you find online posts and follow the process, there may still be some small problems after matching.
  • 3. Data source proxy may be a good way. Most giant companies adopt this method, and developers do not have to consider these problems. They can use proxy for cutting and connection pool. However, there is a very embarrassing problem that open source is not easy to use, because many businesses are not suitable for their own business, And their own development is unrealistic and the cost is too high.

At this time, I’m thinking about why we can’t directly introduce a dependency like other language frameworksymlJust configure it in the file, and all have the simplest scheme now.

Source address

I hope you can support star and add other dependent simple integration in the future.





Import dependency:
  • Maven
  • Gradle
    compile group: 'com.louislivi.fastdep', name: 'fastdep-datasource', version: '1.0.0'

Is this over?

Yes, it’s that simple.

Is it very simple? If you go to the online poster, you will find that it is similar. It teaches you to add files for configuration all the time. If it isMapperScanAdd a data source, and add another one every timejavaClass.


useImportBeanDefinitionRegistrar BeanDefinitionBuilder.genericBeanDefinitionDynamic injectionBeanIn fact, it is very simple. If you are interested, you can see the source code.


Prevent other questions from posting me completelypom.xmlDependencies introduced under:


I hope you can support open source and give me a little star. In the future, we will continue to develop other dependent integration and current multi data optimization supportHibernateAnd even compatible with other frameworks.fastdepGive WayjavaIntegration dependency is simpler. We also recruit like-minded people herecoderWork together to improve the project.

This work adoptsCC agreement, reprint must indicate the author and the link to this article