All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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) object).putAll(properties);
        }
      }
    }

    @SuppressWarnings("unchecked")
    private DataSource dataSource(DataSourceBuilder builder, List> proxys, ClassLoader classLoader) {
      DataSource dataSource = builder.build();

      List> dataSourceProxys = proxys;
      if (CollectionUtils.isEmpty(dataSourceProxys)) {
        dataSourceProxys = new ArrayList>();
        for (String name : new String[] { "com.p6spy.engine.spy.P6DataSource", "net.ttddyy.dsproxy.support.ProxyDataSource", "com.vladmihalcea.flexypool.FlexyPoolDataSource",
            "org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" }) {
          try {
            dataSourceProxys.add((Class) ClassUtils.forName(name, classLoader));
          }
          catch (Exception ex) {
            // ignore
          }
        }
      }
      for (Class proxy : dataSourceProxys) {
        try {
          Constructor constructor = ClassUtils.getConstructorIfAvailable(proxy, DataSource.class);
          if (constructor == null) {
            throw new IllegalStateException("No constructor accepting real DataSource in proxy class " + proxy);
          }
          dataSource = BeanUtils.instantiateClass(constructor, dataSource);
        }
        catch (RuntimeException e) {
          if (proxys.contains(proxy)) {
            throw e;
          }
          logger.warn("Can't wrap data source: " + dataSource + " in proxy data source of type: " + dataSource.getClass().getName() + " due to thrown exception: " + e + ". "
              + "Please consider fix exception in data source implementation" + " or explicitly set 'spring.datasource.proxyType' with" + " appropriate list of proxy data source providers.");
        }
      }
      return dataSource;
    }
 * 
 * 
 * @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;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy