I. Spring configuring multiple data sources
Two. Spring Configuration data source
Three. Implementation of Multipledatasource
1:package com.wbl.modal;
2:
3:import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
4:
5:/**
6: * Created with Simple_love
7: * date:2016/4/26.
8: * Time:11:12
9: * /
:publicclassextends abstractroutingdatasource{
11:
: Private Static Final New Inheritablethreadlocal<string> ();
13:
: Public Static void Setdatasourcekey (String dataSource) {
: datasourcekey.set (DataSource);
: }
17:
: @Override
: protected Object Determinecurrentlookupkey () {
: return datasourcekey.get ();
: }
: }
Multipledatasource implements the Abstractroutingdatasource abstract class, then implements the Determinecurrentlookupkey method, This method is used to select which data source in the targetdatasources is used specifically
Four. Automatic switching of data sources using spring's AOP
(1) Configuring Pointcuts
Tangent setdatasource indicates that a slice is executed when all methods of all classes under the Com.wbl.service package are executed. Because all operations on the DAO layer are invoked at the service, the data source needs to be replaced before entering the DAO layer.
(2) Configuring facets
When the service that corresponds to the external data source is called, the switch data source is Otherdatasource and the other service is switched to datasource.
MyBatis Configuring multiple data sources