org.springframework.boot.autoconfigure.jdbc.DataSourceMultipleAutoConfiguration Maven / Gradle / Ivy
package org.springframework.boot.autoconfigure.jdbc;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
/**
*
@SuppressWarnings("unchecked")
private void properties(DataSourceBuilder builder, Map properties) {
if (CollectionUtils.isEmpty(properties)) {
return;
}
Field field = ReflectionUtils.findField(builder.getClass(), "properties");
if (field != null) {
ReflectionUtils.makeAccessible(field);
Object object = ReflectionUtils.getField(field, builder);
if (object instanceof Map) {
((Map
*
* @see org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
* @see org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
* @see org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration#FlywayInitializerJpaDependencyConfiguration
*/
@Configuration
@ConditionalOnBean(DataSource.class)
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class DataSourceMultipleAutoConfiguration {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSourceMultipleListener dataSourceMultipleListener() {
return new DataSourceMultipleListener();
}
/**
* @see org.springframework.context.ApplicationContextInitializer
* @see org.springframework.boot.context.event.ApplicationReadyEvent
* @see org.springframework.boot.context.event.ApplicationPreparedEvent
* @see org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
*/
protected static class DataSourceMultipleListener implements ApplicationListener {
private List multiple = new ArrayList();
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
for (DataSourceMultipleProperties dataSourceMultipleProperties : this.multiple) {
event.getApplicationContext().getBeanFactory().registerSingleton(dataSourceMultipleProperties.getName(), dataSourceMultipleProperties.initializeDataSourceBuilder().build());
}
}
public List getMultiple() {
return multiple;
}
public void setMultiple(List multiple) {
this.multiple = multiple;
}
}
}