Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- Property file located in the legacy application's folder -->
- <context:property-placeholder location="file:///D:/config.properties" />
- <!-- Shared data source properties are read from the config.properties file -->
- <bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" abstract="true">
- <property name="driverClassName" value="${db.driver}" />
- <property name="username" value="${db.user}" />
- <property name="password" value="${db.password}" />
- </bean>
- <!-- Database urls by year -->
- <bean id="Year2012DataSource" parent="parentDataSource">
- <property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2012" />
- </bean>
- <bean id="Year2011DataSource" parent="parentDataSource">
- <property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2011" />
- </bean>
- <bean id="Year2010DataSource" parent="parentDataSource">
- <property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2010" />
- </bean>
- <!-- ... and so on, these should instead be populated dynamically ... -->
- <!-- DbConnectionRoutingDataSource extends AbstractRoutingDataSource -->
- <bean id="dataSource" class="someProject.DbConnectionRoutingDataSource">
- <property name="targetDataSources">
- <map key-type="int">
- <entry key="2011" value-ref="Year2011DataSource" />
- <entry key="2010" value-ref="Year2010DataSource" />
- <!-- ... and so on, these also should instead be populated dynamically ... -->
- </map>
- </property>
- <property name="defaultTargetDataSource" ref="Year2012DataSource" />
- </bean>
- class MyDatasourceRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
- //Read in details from legacy properties.. build custom bean definitions and register with bean factory
- //for each legacy property...
- BeanDefinitionBuilder datasourceDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(BasicDataSource.class).addPropertyValue("url", "jdbc..");
- beanFactory.registerBeanDefinition(datasourceDefinitionBuilder.getBeanDefinition());
- }
- }
- @Bean(name="dataSourceMap")
- public Map<String, DataSource> dataSourceMap(DataSource dataSource2011, DataSource dataSource2012) {
- // read properties from properties file and create map of datasource
- Map<DataSource> map = new HashMap<>();
- map.put("dataSource2011",dataSource2011);
- map.put("dataSource2012",dataSource2012);
- //map.put("dataSource2013",dataSource2013);
- return map;
- }
- @Bean(name="dataSource2011",destroyMethod="close")
- public DataSource dataSource() {
- // read properties from properties file and create map of
- BasicDataSource dataSource = new BasicDataSource();
- dataSource.setDriverClassName(driverClassName);
- dataSource.setUrl(url2011);
- dataSource.setUsername(username2011);
- dataSource.setPassword(password2011);
- return dataSource;
- }
- @Bean(name="dataSource2012",destroyMethod="close")
- public DataSource dataSource() {
- // read properties from properties file and create map of
- BasicDataSource dataSource = new BasicDataSource();
- dataSource.setDriverClassName(driverClassName);
- dataSource.setUrl(url2012);
- dataSource.setUsername(username2012);
- dataSource.setPassword(password2012);
- return dataSource;
- }
- <bean id="dataSource" class="someProject.DbConnectionRoutingDataSource">
- <property name="targetDataSources">
- <map key-type="int">
- <entry key="0" value-ref="placeholderDs" />
- </map>
- </property>
- <property name="defaultTargetDataSource" ref="placeholderDs" />
- </bean>
- @Component
- class DatasourceRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
- // example data, will be read from the legacy configuration
- HashMap<Integer, String> connectionsList = new HashMap<>();
- connectionsList.put(2011, "jdbc:sqlserver://localhost;databaseName=DbName_v570_2011");
- connectionsList.put(2012, "jdbc:sqlserver://localhost;databaseName=DbName_v570_2012");
- int endYear = 0;
- BeanDefinitionRegistry factory = (BeanDefinitionRegistry) beanFactory;
- BeanDefinitionBuilder datasourceDefinitionBuilder;
- // Create new beans
- for (Entry<Integer, String> e : connectionsList.entrySet()) {
- datasourceDefinitionBuilder = BeanDefinitionBuilder.childBeanDefinition("parentDataSource")
- .addPropertyValue("url", e.getValue());
- factory.registerBeanDefinition(String.format("Year%sDataSource", e.getKey()),
- datasourceDefinitionBuilder.getBeanDefinition());
- endYear = e.getKey();
- }
- if (endYear == 0)
- return;
- // Configure the dataSource bean properties
- MutablePropertyValues mpv = factory.getBeanDefinition("dataSource").getPropertyValues();
- // Set the default dataSource
- mpv.removePropertyValue("defaultTargetDataSource");
- mpv.addPropertyValue("defaultTargetDataSource", new RuntimeBeanReference(String.format("Year%sDataSource", endYear)));
- // Set the targetDataSource properties map
- ManagedMap<Integer, RuntimeBeanReference> mm = (ManagedMap<Integer, RuntimeBeanReference>) mpv
- .getPropertyValue("targetDataSources").getValue();
- mm.clear(); // clear the placeholder entry
- // Fill the map with bean references to the newly created beans
- for (Entry<Integer, String> e : connectionsList.entrySet()) {
- mm.put(e.getKey(), new RuntimeBeanReference(String.format("Godina%sDataSource", e.getKey())));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement