spring 多数据源解决方案

来源:互联网

2。在有多个数据源的时 候,需要为每个数据源生成一个 beanfactory 。这个目前可能无法通过 spring 提供的 servlet 来自动实现,需要自己手工来操作。这些 beanfactory 使用同样的 spring xml 配置文件,但是使用不同的数据库配置文件。

对应每一个数据源,在程序启动的时候生成一个 factory ,然后可以为每个 factory 和数据源标识对应起来保存在 hashmap 之类的列表中,以方便后面使用。

3。如果使用 webwork,web 层的 action 不能再在 spring 的配置文件中出现了。在 web 层的 action 里面都实现一个 BeanFactoryAware 的 Interface 。再写一个 interceptor 来为 action 做 IoC 的操作,这个 BeanFactoryAware 可以根本不需要有任何方法,只是作为一个标识接口就可以了。

interceptor 的代码类似

Java代码
  1. public class RegistryInterceptor implements Interceptor {
  2. public String intercept(ActionInvocation invocation) throws Exception {
  3. if( invocation.getAction() instanceof BeanFactoryAware ) {
  4. Map session = invocation.getInvocationContext().getSession();
  5. Registry.getInstance().getContext( session.get( "dsKey" ) )
  6. .getBeanFactory().autowireBeanProperties( invocation.getAction(),
  7. AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false );
  8. }
  9. }
  10. }
Java代码
  1. package test;
  2. import org.springframework.context.support.AbstractApplicationContext;
  3. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.util.Properties;
  9. import java.util.Map;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. public final class Registry {
  13. static Registry instance = new Registry();
  14. public static Registry getInstance() {
  15. return instance;
  16. }
  17. private Map contextMap;
  18. private Registry() {
  19. contextMap = new HashMap();
  20. }
  21. public Object getBean( final String dsKey, final String beanName ) {
  22. AbstractApplicationContext context =
  23. (AbstractApplicationContext)contextMap.get( dsKey );
  24. if( null != context ) {
  25. return context.getBean( beanName );
  26. } else {
  27. return null;
  28. }
  29. }
  30. public AbstractApplicationContext getContext( final String dsKey ) {
  31. return (AbstractApplicationContext)contextMap.get( dsKey );
  32. }
  33. public void init( Map dsPropertiesMap, String [] contextFiles ) throws Exception {
  34. Iterator iter = dsPropertiesMap.keySet().iterator();
  35. while( iter.hasNext() ) {
  36. String dsKey = (String)iter.next();
  37. String [] propertiesFiles = (String[])dsPropertiesMap.get( dsKey );
  38. Properties allProps = new Properties();
  39. for( int i = 0; i < propertiesFiles.length; i++ ) {
  40. Properties props = new Properties();
  41. props.load( new FileInputStream( propertiesFiles[i] ) );
  42. allProps.putAll( props );
  43. }
  44. PropertyPlaceholderConfigurer configurer =
  45. new  PropertyPlaceholderConfigurer();
  46. configurer.setProperties(  allProps );
  47. ClassPathXmlApplicationContext context =
  48. new ClassPathXmlApplicationContext( contextFiles, false );
  49. context.addBeanFactoryPostProcessor( configurer );
  50. context.refresh();
  51. contextMap.put( dsKey, context );
  52. }
  53. }
  54. }
Java代码
  1. package test;
  2. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
  3. import java.util.Map;
  4. import java.util.HashMap;
  5. import test.Registry;
  6. class UserService {
  7. UserService(){}
  8. // omit other useful methods
  9. }
  10. class UserAction {
  11. UserService userService;
  12. UserAction() {}
  13. void setUserService( UserService userService ) {
  14. userService = userService;
  15. }
  16. // omit other useful methods
  17. }
  18. public class TestRegistry {
  19. static public void main( String [] args ) throws Exception {
  20. Map propertiesMap = new HashMap();
  21. propertiesMap.put( "ds1",
  22. new String[] { "database1.properties", "common.properties" } );
  23. propertiesMap.put( "ds2",
  24. new String[] { "database2.properties", "common.properties" } );
  25. Registry.getInstance().init( propertiesMap,
  26. new String[] { "ApplicationContext.xml", "DataAccessContext.xml" } );
  27. UserAction action = new UserAction();
  28. Registry.getInstance().getContext( "ds1" ).getBeanFactory()
  29. .autowireBeanProperties( action,
  30. AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
  31. UserService service =
  32. (UserService)Registry.getInstance().getBean( "ds2", "UserService" );
  33. }
  34. }

发表评论