This article excerpts, reprint need to indicate the source http://blog.csdn.net/shadowsick/article/details/8878448 many of our projects require multiple data sources to be involved in the business. The simplest way is to directly in the Java code to lookup the required data source, but it is obvious that the coupling is too high, and when the logic process is not rigorous, there will be a variety of people do not want to see the problem, because most of our current projects are inseparable from spring, Spring also offers a variety of powerful features that are clearly included in this Dynamic Data source feature, Concrete implementation class See org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource Here I demonstrate how to use the Dynamic Data source provided by spring to write an interface first, Default is set to NULL because a source that is not associated in the program is invoked to call the source [Java] View plaincopyprint?01./** 02. * Data source Switching interface 03. * 04. * @author Shadow 05. * @create 2013.04.03 06. */07.public interface Datasourceentry {08. 09.//Default data source 10. Public final static String default_source = null; 11.12. /** 13. * Restore data source 14. * 15. * @param joinpoint 16. */17. public void Restore (joinpoint join); 18.19. /** 20. * Set data source 21. * 22. * @param dataSource 23. */24. public void Set (String source); 25.26. /** 27. * Get data source 28. * 29. * @return String 30. */31. Public String get (); 32.33. /** 34. * Empty data source 35. */36. public void clear (); 37.}/** * Data source Switch Interface * * @author shadow * @create 2013.04.03 */public InterfaCe datasourceentry {//default data source public final static String Default_source = null;/** * Restore Data source * * @param joinpoint */public voi D Restore (joinpoint join);/** * Set Data source * * @param dataSource */public void Set (String source);/** * Get Data source * * @return Strin G */public String get ();/** * Empties the data source */public void clear (); Then write an implementation class that pulls the corresponding data source name from the current line thread [Java] View plaincopyprint?01./** 02. * Data source switching implements Class 03. * 04. * @author Shadow 05. * @create 2013.04.03 06. */07.public class Datasourceentryimpl implements Dynamictypeentry {08. Private final static ThreadLocal Local = new ThreadLocal (); 10.11. public void Clear () {local.remove (); 13.} 14. . Public String get () {. return local.get (); 17.} 18. public void Restore (Joinpoint join) {local.set (Default_source); 21.} 22. . public void Set (String source) {local.set (source); 25.} 26. 27.}/** * Data source Switch Implementation Class class * * @author Shadow * @create 2013.04.03 */public class Datasourceentryimpl implements DYNAMICTYPEENTR Y {private final static ThreadLocal Local = new ThreadLocal ();p ublic void Clear () {Local.remove ();} Public String get () {return local.get ();} public void Restore (Joinpoint join) {local.set (default_source);} public void Set (String source) {local.set (source);}} Then write a class that inherits Abstractroutingdatasource and inject Datasourceentry, overriding the Determinecurrentlookupkey template method [Java] View plaincopyprint? 01./** 02. * Get data source (dependent on spring framework) 03. * 04. * @author Shadow 05. * @create 2013.04.03 06. */07.public class Dynamicdatasource extends Abstractroutingdatasource {08. Datasourceentry private datasourceentry; 10.11. @Override 12. Protected Object Determinecurrentlookupkey () {. return this.dataSourceEntry.get (); 14.} 15. 17. @Resource. public void Setdatasourceentry (Datasourceentry datasourceentry) {this.datasourceentry = Datasourceentry; 19.} 20. 21.}/** * Get data source (dependent on spring framework) * * @author shadow * @create 2013.04.03 */public class Dynamicdatasource extends Abstractrout Ingdatasource {private datasourceentry datasourceentry; @Overrideprotected Object DETERMINECURRENTLOOKUPKEY () {return this.dataSourceEntry.get ();} @Resourcepublic void Setdatasourceentry (Datasourceentry datasourceentry) {this.datasourceentry = Datasourceentry;}} Finally, the configuration of the next XML file, only need to directly manage Dynamicdatasource This interface can be, as to his internal is which data source is not need to pay attention to, write the slice restore is to ensure that the other data source after each call will be restored to the default data source, Prevent some people from forgetting to set back the default, causing other code problems [Java] View plaincopyprint?01. 02. 05 . 06.07. 08. Ten. 11. classpath:properties/jdbc.properties 12. 13. 14.15. 16. 19. 21. 23 . 24. 25. . 27.28. 29. 32. false 33. 34. ${jndi.template} 36. 37. 38.39. 40. 43. false 44. 45. ${jndi.test} 47. 48. 49.50. 51. 53.54. 55.56. 58.60.61. classpath:properties/jdbc.properties false ${jndi.template} false ${jndi.test} As for how to change the data source in the program, you can detect which methods are added to the Before method on the slice, or call the Set method directly using Datasourceentry in the program.
Spring dynamic switching multi-data source solution