Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. <!-- Property file located in the legacy application's folder -->
  2. <context:property-placeholder location="file:///D:/config.properties" />
  3.  
  4. <!-- Shared data source properties are read from the config.properties file -->
  5. <bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" abstract="true">
  6. <property name="driverClassName" value="${db.driver}" />
  7. <property name="username" value="${db.user}" />
  8. <property name="password" value="${db.password}" />
  9. </bean>
  10.  
  11. <!-- Database urls by year -->
  12. <bean id="Year2012DataSource" parent="parentDataSource">
  13. <property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2012" />
  14. </bean>
  15. <bean id="Year2011DataSource" parent="parentDataSource">
  16. <property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2011" />
  17. </bean>
  18. <bean id="Year2010DataSource" parent="parentDataSource">
  19. <property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2010" />
  20. </bean>
  21. <!-- ... and so on, these should instead be populated dynamically ... -->
  22.  
  23. <!-- DbConnectionRoutingDataSource extends AbstractRoutingDataSource -->
  24. <bean id="dataSource" class="someProject.DbConnectionRoutingDataSource">
  25. <property name="targetDataSources">
  26. <map key-type="int">
  27. <entry key="2011" value-ref="Year2011DataSource" />
  28. <entry key="2010" value-ref="Year2010DataSource" />
  29. <!-- ... and so on, these also should instead be populated dynamically ... -->
  30. </map>
  31. </property>
  32. <property name="defaultTargetDataSource" ref="Year2012DataSource" />
  33. </bean>
  34.  
  35. class MyDatasourceRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  36.  
  37. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  38. //Read in details from legacy properties.. build custom bean definitions and register with bean factory
  39. //for each legacy property...
  40. BeanDefinitionBuilder datasourceDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(BasicDataSource.class).addPropertyValue("url", "jdbc..");
  41. beanFactory.registerBeanDefinition(datasourceDefinitionBuilder.getBeanDefinition());
  42. }
  43. }
  44.  
  45. @Bean(name="dataSourceMap")
  46. public Map<String, DataSource> dataSourceMap(DataSource dataSource2011, DataSource dataSource2012) {
  47. // read properties from properties file and create map of datasource
  48.  
  49. Map<DataSource> map = new HashMap<>();
  50. map.put("dataSource2011",dataSource2011);
  51. map.put("dataSource2012",dataSource2012);
  52. //map.put("dataSource2013",dataSource2013);
  53. return map;
  54. }
  55.  
  56. @Bean(name="dataSource2011",destroyMethod="close")
  57. public DataSource dataSource() {
  58. // read properties from properties file and create map of
  59.  
  60. BasicDataSource dataSource = new BasicDataSource();
  61. dataSource.setDriverClassName(driverClassName);
  62. dataSource.setUrl(url2011);
  63. dataSource.setUsername(username2011);
  64. dataSource.setPassword(password2011);
  65. return dataSource;
  66. }
  67.  
  68. @Bean(name="dataSource2012",destroyMethod="close")
  69. public DataSource dataSource() {
  70. // read properties from properties file and create map of
  71.  
  72. BasicDataSource dataSource = new BasicDataSource();
  73. dataSource.setDriverClassName(driverClassName);
  74. dataSource.setUrl(url2012);
  75. dataSource.setUsername(username2012);
  76. dataSource.setPassword(password2012);
  77. return dataSource;
  78. }
  79.  
  80. <bean id="dataSource" class="someProject.DbConnectionRoutingDataSource">
  81. <property name="targetDataSources">
  82. <map key-type="int">
  83. <entry key="0" value-ref="placeholderDs" />
  84. </map>
  85. </property>
  86. <property name="defaultTargetDataSource" ref="placeholderDs" />
  87. </bean>
  88.  
  89. @Component
  90. class DatasourceRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  91. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  92.  
  93. // example data, will be read from the legacy configuration
  94. HashMap<Integer, String> connectionsList = new HashMap<>();
  95. connectionsList.put(2011, "jdbc:sqlserver://localhost;databaseName=DbName_v570_2011");
  96. connectionsList.put(2012, "jdbc:sqlserver://localhost;databaseName=DbName_v570_2012");
  97. int endYear = 0;
  98.  
  99. BeanDefinitionRegistry factory = (BeanDefinitionRegistry) beanFactory;
  100. BeanDefinitionBuilder datasourceDefinitionBuilder;
  101.  
  102. // Create new beans
  103. for (Entry<Integer, String> e : connectionsList.entrySet()) {
  104. datasourceDefinitionBuilder = BeanDefinitionBuilder.childBeanDefinition("parentDataSource")
  105. .addPropertyValue("url", e.getValue());
  106.  
  107. factory.registerBeanDefinition(String.format("Year%sDataSource", e.getKey()),
  108. datasourceDefinitionBuilder.getBeanDefinition());
  109.  
  110. endYear = e.getKey();
  111. }
  112.  
  113. if (endYear == 0)
  114. return;
  115.  
  116. // Configure the dataSource bean properties
  117. MutablePropertyValues mpv = factory.getBeanDefinition("dataSource").getPropertyValues();
  118.  
  119. // Set the default dataSource
  120. mpv.removePropertyValue("defaultTargetDataSource");
  121. mpv.addPropertyValue("defaultTargetDataSource", new RuntimeBeanReference(String.format("Year%sDataSource", endYear)));
  122.  
  123. // Set the targetDataSource properties map
  124. ManagedMap<Integer, RuntimeBeanReference> mm = (ManagedMap<Integer, RuntimeBeanReference>) mpv
  125. .getPropertyValue("targetDataSources").getValue();
  126. mm.clear(); // clear the placeholder entry
  127. // Fill the map with bean references to the newly created beans
  128. for (Entry<Integer, String> e : connectionsList.entrySet()) {
  129. mm.put(e.getKey(), new RuntimeBeanReference(String.format("Godina%sDataSource", e.getKey())));
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement